using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class GameObjectInfo : MonoBehaviour
{
[System.Serializable]
public class GameObjectstInfo
{
public GameObject parent;
public int childrenCount;
public List<Transform> children = new List<Transform>();
}
public string gameObjectsInfo = "";
public string results = "";
public bool addChildren = false;
public GameObjectstInfo[] objectsInfo;
private string previousGameObjectsInfo = "";
// Start is called before the first frame update
void Start()
{
Search();
}
private void Update()
{
if (gameObjectsInfo != "" && gameObjectsInfo != previousGameObjectsInfo)
{
Search();
}
previousGameObjectsInfo = gameObjectsInfo;
Reset();
}
public void Search()
{
if (gameObjectsInfo != "")
{
var foundObjects = FindGameObjectsWithName(gameObjectsInfo);
objectsInfo = new GameObjectstInfo[foundObjects.Length];
if (foundObjects.Length > 0)
{
results = "Found Results";
for (int i = 0; i < foundObjects.Length; i++)
{
objectsInfo[i] = new GameObjectstInfo();
objectsInfo[i].parent = foundObjects[i];
if (addChildren == true)
{
foreach (Transform t in foundObjects[i].GetComponentsInChildren<Transform>(true))
{
objectsInfo[i].childrenCount += 1;
objectsInfo[i].children.Add(t);
}
}
else
{
}
}
}
else
{
results = "No Results";
}
}
}
private void Reset()
{
if (gameObjectsInfo == "")
{
results = "No Results";
objectsInfo = new GameObjectstInfo[0];
}
}
GameObject[] FindGameObjectsWithName(string nameIt)
{
int it = 0;
GameObject[] objArr;
bool b = false;
while (!b)
{
if (GameObject.Find(nameIt))
{
GameObject.Find(nameIt).name = nameIt + it;
it++;
}
else
{
b = true;
}
}
objArr = new GameObject[it];
while (it > 0)
{
it--;
objArr[it] = GameObject.Find(nameIt + it);
objArr[it].name = nameIt;
}
return objArr;
}
}
这是我添加或不添加孩子的部分:
if (addChildren == true)
{
foreach (Transform t in foundObjects[i].GetComponentsInChildren<Transform>(true))
{
objectsInfo[i].childrenCount += 1;
objectsInfo[i].children.Add(t);
}
}
else
{
}
但是如果标记addChildren为true并更改为false或false为true时,我也想在Update中进行更改,并实时添加/删除子项。
现在我需要删除变量gameObjectsInfo中的文本,然后再次键入以更改子代。但我希望这种效果不会再次输入就可以实现。仅在更改标志addChildren的状态时。
如果我要更改addChildren状态,请重新搜索有无孩子。
答案 0 :(得分:1)
您可以通过多种方式编写自己的类编辑器,以使其响应单击该布尔值的复选框,如果您仅通过代码设置using Constructor = Ptr<Layer>(*)(LayerParams ¶ms);
的值,则可以使用属性addChildren
函数,或者,最直接的方法(如果您不介意在更改值后等到下一帧),只是多余的布尔值跟踪先前的值:
set