Unity踩坑一

退出游戏时报错: Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?) 原因 正

退出游戏时报错: Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)

原因

正常情况下,在OnDisable或者OnDestroy生命周期中实例化GameObject是可以的。但是退出游戏时也会调用OnDisable或者OnDestroy,就造成了退出游戏时实例化GameObject

解决

思路很简单,就是在OnDisable或者OnDestroy生命周期中判断目前是否是正在退出游戏

按照Unity官方提供的生命周期执行顺序

会发现最后一阶段生命周期函数执行顺序为OnApplicationQuit -> OnDisable -> OnDestroy

所以第一种解决方法如下

using UnityEngine;

public class Example : MonoBehaviour
{
    private bool _isQuitting = false;

    private void OnApplicationQuit()
    {
      _isQuitting = true;
    }

    private void OnDisable()
    {
      if(_is

发布者:admin,转转请注明出处:http://www.yc00.com/web/1754578183a5177733.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信