using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class ExampleScript : MonoBehaviour
{
// 獲取場景中所有目標對象(包括不激活的對象)不包括Prefabs:
List<T> FindSceneObject<T>(string _SceneName)where T:UnityEngine.Component {
List<T> objectsInScene = new List<T>();
foreach (var go in Resources.FindObjectsOfTypeAll<T>())
{
if (go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave)
continue;
if (EditorUtility.IsPersistent(go.transform.root.gameObject))// 如果對象位於Scene中,則返回false
continue;
if (_SceneName != go.gameObject.scene.name)
continue;
Debug.LogFormat("gameObject:{0},scene:{1}", go.gameObject.name, go.gameObject.scene.name);
objectsInScene.Add(go);
}
return objectsInScene;
}
}
參考:Resources.FindObjectsOfTypeAll