kingBook

导航

unity 获取DontDestroyOnLoad的游戏对象

private GameObject[] getDontDestroyOnLoadGameObjects(){
	var allGameObjects=new List<GameObject>();
	allGameObjects.AddRange(FindObjectsOfType<GameObject>());
	//移除所有场景包含的对象
	for(var i=0;i<SceneManager.sceneCount;i++){
		var scene=SceneManager.GetSceneAt(i);
		var objs=scene.GetRootGameObjects();
		for(var j=0;j<objs.Length;j++){
			allGameObjects.Remove(objs[j]);
		}
	}
	//移除父级不为null的对象
	int k=allGameObjects.Count;
	while(--k>=0){
		if(allGameObjects[k].transform.parent!=null){
			allGameObjects.RemoveAt(k);
		}
	}
	return allGameObjects.ToArray();
}

posted on 2019-06-10 22:15  kingBook  阅读(1981)  评论(0编辑  收藏  举报