Unity3d:场景加载 GameObejct上脚本执行顺序
场景1脚本
public class LoadScene1 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
StartCoroutine(StartScene());
}
IEnumerator StartScene()
{
AsyncOperation aync = SceneManager.LoadSceneAsync("LoadScene2", LoadSceneMode.Additive);
while (aync.isDone == false)
{
Debug.Log(aync.progress);
yield return null;
}
Debug.Log("LoadOK");
yield return null;
}
}
场景2某个物体上挂载
public class LoadScene2 : MonoBehaviour
{
private void Awake()
{
Debug.Log("Awake");
}
// Start is called before the first frame update
void Start()
{
Debug.Log("Start");
}
private void OnEnable()
{
Debug.Log("OnEnable");
}
private void Update()
{
Debug.Log("Update");
}
}
场景2截图

执行后输出


可以看到,加载LoadScene2完成,会执行LoadScene2里所有的GameObject的Awake,OnEnable,Start,和update会执行一次,再真正加载完场景
所以,在优化场景加载时,代码层面关注新场景里固有GameObject上脚本里执行逻辑复杂度
浙公网安备 33010602011771号