unity加载场景不删除


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DontDestroyOnLoad : MonoBehaviour {

//加载场景时不销毁的物体
public GameObject[] DontDestroyObjects;

//是否已经存在DontDestroy的物体
private static bool isExist;

//-------------------------------------------------------------------------------

void Awake()
{
if (!isExist)
{
for (int i = 0; i < DontDestroyObjects.Length; i++)
{
//如果第一次加载,将这些物体设为DontDestroy
DontDestroyOnLoad(DontDestroyObjects[i]);
}

isExist = true;
}
else
{
for (int i = 0; i < DontDestroyObjects.Length; i++)
{
//如果已经存在,则删除重复的物体
Destroy(DontDestroyObjects[i]);
}
}
}

//-------------------------------------------------------------------------------
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM