/// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Samples.SceneManagers { using Opsive.BehaviorDesigner.Runtime; using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; /// /// Switches to the next scene after a random amount of time. /// public class PersistentScenesManager : MonoBehaviour { [Tooltip("A reference to the behavior tree agent prefab.")] [SerializeField] protected GameObject m_Agent; [Tooltip("The waypoints within the scene.")] [SerializeField] protected GameObject[] m_Waypoints; [Tooltip("The name of the next scene.")] [SerializeField] protected string m_NextSceneName; [Tooltip("The minimum seconds for changing to the next scene.")] [SerializeField] protected int m_MinimumNextSceneChangeRange = 5; [Tooltip("The maximum seconds for changing to the next scene.")] [SerializeField] protected int m_MaximumNextSceneChangeRange = 9; [Tooltip("A reference to the visual countdown until the scene is changed.")] [SerializeField] protected UnityEngine.UI.Text m_CountdownText; /// /// Initializes the default values. /// public void Awake() { #if UNITY_2022 var behaviorTree = Object.FindObjectOfType(); #else var behaviorTree = Object.FindFirstObjectByType(); #endif // The behavior tree will be found if it has already been marked as DontDestroyOnLoad. if (behaviorTree == null) { var agent = Object.Instantiate(m_Agent); behaviorTree = agent.GetComponent(); DontDestroyOnLoad(agent); } // Update the waypoints to the new scene. The behavior tree will stay active across scene changes. var waypoints = behaviorTree.GetVariable("Waypoints"); if (waypoints == null) { Debug.LogError("Error: Unable to find the Waypoints variable."); return; } waypoints.Value = m_Waypoints; StartCoroutine(LoadNextScene()); } /// /// Loads the next scene. /// private IEnumerator LoadNextScene() { var duration = Random.Range(m_MinimumNextSceneChangeRange, m_MaximumNextSceneChangeRange); var startTime = Time.time; while (startTime + duration > Time.time) { m_CountdownText.text = $"Changing scenes in {Mathf.RoundToInt(startTime + duration - Time.time)} seconds"; yield return new WaitForEndOfFrame(); } m_CountdownText.text = "Changing scenes"; SceneManager.LoadScene(m_NextSceneName); } } }