summaryrefslogtreecommitdiff
path: root/GameCode/BackgroundHandler.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2023-10-27 11:05:14 +0800
committerchai <215380520@qq.com>2023-10-27 11:05:14 +0800
commit766cdff5ffa72b65d7f106658d1603f47739b2ba (patch)
tree34d7799a94dfa9be182825577583c0fa6dc935f7 /GameCode/BackgroundHandler.cs
+ init
Diffstat (limited to 'GameCode/BackgroundHandler.cs')
-rw-r--r--GameCode/BackgroundHandler.cs39
1 files changed, 39 insertions, 0 deletions
diff --git a/GameCode/BackgroundHandler.cs b/GameCode/BackgroundHandler.cs
new file mode 100644
index 0000000..90823de
--- /dev/null
+++ b/GameCode/BackgroundHandler.cs
@@ -0,0 +1,39 @@
+using System.Collections;
+using UnityEngine;
+
+public class BackgroundHandler : MonoBehaviour
+{
+ private Background[] backgrounds;
+
+ private float untilSwitch;
+
+ private void Start()
+ {
+ backgrounds = GetComponentsInChildren<Background>(includeInactive: true);
+ }
+
+ private void Update()
+ {
+ untilSwitch -= TimeHandler.deltaTime;
+ if (untilSwitch < 0f)
+ {
+ SwitchBackground();
+ }
+ }
+
+ private void SwitchBackground()
+ {
+ untilSwitch = Random.Range(30, 60);
+ for (int i = 0; i < backgrounds.Length; i++)
+ {
+ backgrounds[i].ToggleBackground(on: false);
+ }
+ StartCoroutine(StartBackGroundSoon());
+ }
+
+ private IEnumerator StartBackGroundSoon()
+ {
+ yield return new WaitForSeconds(5f);
+ backgrounds[Random.Range(0, backgrounds.Length)].ToggleBackground(on: true);
+ }
+}