如何让占领基地后5秒内跳转到主菜单?这是我的鳕鱼。
private void Update()
{
StartCoroutine(Timer(5f));
}
IEnumerator Timer(float time)
{
yield return new WaitForSeconds(time);
if (ImagePoint.fillAmount == 1f)
{
SceneManager.LoadScene("MainMenu");
}
}
}
将类似的内容添加到您的游戏场景中:
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class GameManager: MonoBehaviour
{
// ... imagine the rest of your game management logic is here
///<summary>has the base been captured yet?</summary>
private bool _baseCaptured = false;
///<summary>name of main menu scene</summary>
[SerializeField]
private string _mainMenuSceneName = "MainMenu";
///<summary> delay before going to main menu once base has been captured</summary>
[SerializeField]
private float mainMenuDelaySeconds = 5f;
///<summary>Call this method when the base has been captured</summary>
public void BaseHasBeenCaptured()
{
if(_baseCaptured){ return; }
_baseCaptured = true;
Debug.Log($"the base has been captured, will go to main menu in {mainMenuDelaySeconds } seconds");
StartCoroutine(GoToMainMenuCoroutine(mainMenuDelaySeconds);
}
///<summary>Goes to the main menu scene after delay seconds</summary>
private IEnumerator GoToMainMenuCoroutine(float delay)
{
yield return new WaitForSeconds(delay);
SceneManager.LoadScene(_mainMenuSceneName);
}
}
然后,一旦占领了基地(无论如何都会发生),您只需调用此函数即可在延迟 5 秒后返回主菜单:
FindObjectOfType<GameManager>.BaseHasBeenCaptured();
当然,有更优雅的方法可以做到这一点(例如通过调用 GameManager 订阅的事件来处理事情),但是,因为这些更精致的答案首先需要有关整体系统架构的信息您的游戏,这个快速但肮脏的解决方案应该同时起作用。