加载场景不销毁物体

发布时间 2023-11-20 20:17:36作者: gatran

加载场景不销毁物体

挂在不想销毁的物体上

using UnityEngine;

public class DontDestroyObject : MonoBehaviour
{
    private static DontDestroyObject instance;

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