unity FindObjectsOfType 遍历对象


1、获取GameObject的所有子物体;
2、获取场景所有的物体/指定类型的物体;
3、获取项目中Resource目录下的所有文件/指定类型文件;
4、获取项目中任意目录下的所有文件/指定类型文件; 


//查找指定对象的子物体
GameObject go;
//返回TransfTransform[]
go.GetComponentsInChildren(typeof(Transform), true);
go.transform.GetComponentsInChildren();
go.GetComponentsInChildren();


  //遍历场景中所有物体,获取泛型T类型物体
object[] gameObjects;
gameObjects = GameObject.FindSceneObjectsOfType(typeof(Transform));
foreach (Transform go in gameObjects)
{
if (go.GetComponent() != null)
{
//获取成功,添加相关操作
}
}


//获取Resource目录下的文件
Resources.LoadAll(path, typeof(MonoScript));//获取path目录xiade下的脚本文件
//获取Project任意目录下的文件
string[] filePaths = Directory.GetFiles(Application.dataPath EventPath, "*.cs", SearchOption.AllDirectories);
for (int i = 0; i < filePaths.Length; i )
{
MonoScript fileItem;
int lastIndex = filePaths[i].LastIndexOf("/");
string fileName = filePaths[i].Remove(0, lastIndex 1);
fileItem = (MonoScript)AssetDatabase.LoadAssetAtPath("Assets" EventPath fileName, typeof(MonoScript));
if (fileItem != null && fileItem.GetClass().IsSubclassOf(typeof(BaseEvent)) && !events.Contains(fileItem))
{
events.Add(fileItem);
}
}

你可能感兴趣的:(unity3D)