luoyikun

导航

统计

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上脚本里执行逻辑复杂度

posted on   luoyikun  阅读(30)  评论(0编辑  收藏  举报  

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示