【发布时间】:2025-12-25 13:45:06
【问题描述】:
我想测试一个游戏对象是否是另一个游戏对象的实例,但我所拥有的似乎不起作用。我正在尝试制作一个池脚本来池项目而不是创建/销毁它们。
这是我目前正在做的事情:
// Holds all the items that can be instantiated
private List<PoolItems> poolItems = new List<PoolItems>();
// Holds all the pooled items (displayed in the hierarchy active/inactive)
private List<GameObject> gameObjects = new List<GameObject>();
// Here is how I initiate pool items:
void Start(){
for(int i = 0; i < poolItems.Count; i++){
GameObject obj = poolItems[i].prefab;
for (int j = 0; j < poolItems[i].startItems; j++){
GameObject newObj = Create(obj);
newObj.SetActive(false);
gameObjects.Add(newObj);
}
}
}
// The issue is in this method I believe:
public GameObject Instantiate(GameObject obj, Vector3 position, Quaternion rotation){
// Find an inactive object
GameObject foundObject = (
from i in gameObjects
where
i.GetType().IsAssignableFrom(obj.GetType()) &&
i.activeInHierarchy == false
select i
).FirstOrDefault();
}
发生的情况是它只选择了列表中的第一项。
以这个层次结构为例:
Circle (Clone)
Circle (Clone)
Circle (Clone)
Square (Clone)
Square (Clone)
Square (Clone)
Square (Clone)
当我将“方形”游戏对象传递给该方法时,它仍然选择“圆形”项目。
【问题讨论】:
-
Circle 和 Square 都是 GameObject 吗?
-
是的。它们都是预制件