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]);
}
}
}

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

posted @ 2019-03-28 14:28  WalkingSnail  阅读(1096)  评论(0)    收藏  举报