加载场景不销毁物体

加载场景不销毁物体

挂在不想销毁的物体上

using UnityEngine;

public class DontDestroyObject : MonoBehaviour
{
    private static DontDestroyObject instance;

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

  

posted @ 2023-11-20 20:11  gatran  阅读(16)  评论(0编辑  收藏  举报