我有三个子游戏对象(如下图所示,红色、粉色和蓝色)。它们是父游戏对象 Green 的子对象。
我不知道,如何计算 Parent(Green) GameObject 的大小?
我正在使用以下代码在运行时创建所有这些游戏对象:
GameObject CreatePattern(int difficultyLevel)
{
GameObject gameObjectTemp = new GameObject();
SinglePattern singlePattern;
gameObjectTemp = (GameObject) Instantiate(gameObjectTemp);
singlePattern = freeRunDataHandler.GetSinglePatternRandom(1);
GameObject gameObjectSingleObject = null;
foreach (SingleObject singleObject in singlePattern.singleObjectList)
{
gameObjectSingleObject = GetGameObjectByCategory(singleObject.catergory, singleObject.type);
if (gameObjectSingleObject != null)
{
gameObjectSingleObject = (GameObject) Instantiate(gameObjectSingleObject, new Vector3(singleObject.positionX, singleObject.positionY, singleObject.positionZ), Quaternion.identity);
gameObjectSingleObject.transform.localScale = new Vector3(singleObject.length, 1, singleObject.width);
gameObjectSingleObject.transform.parent = gameObjectTemp.transform;
}
}
return gameObjectTemp;
}
添加所有孩子后,此函数返回父(绿色)游戏对象。我的父级(绿色)没有任何附加内容,甚至没有任何组件(BoxCollider、MeshFilter、MeshRenderer 等)。
我已附加 BoxCollider、MeshRenderer 和 MeshFilter(仅用于测试)并且我在父级上尝试过:
parent.collider.bounds.size.x ----- > box collider
parent.renderer.bounds.size.x ----- > mesh renderer
但没有任何效果。在某些情况下返回 1 或零。请帮助我如何获取父(绿色)游戏对象的大小?