Man*_*lik -4 c# unity-game-engine
如何为3D角色模型定义模型描述列表(如颈部,脊柱,下唇等)?我试着编写一个脚本:
using UnityEngine;
using System.Collections;
public class Arrays : MonoBehaviour
{
public GameObject[] players;
void Start()
{
players = GameObject.FindGameObjectsWithTag("avatar_5");
for (int i = 0; i < players.Length; i++)
{
Debug.Log(players[i].name);
Debug.Log("Player Number " + i + " is named " + players[i].name);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但结果我收到此错误: UnityException:标签:avatar_5未定义
Sma*_*tis 13
首先,你在Unity3D中描述的model description很简单GameObjects.而且您的例外情况告诉我们您GameObject的标签上没有正确的标签.
另外还有一个差异巨大GameObject的名称和他的标签.
因此,如果您想要打印特定的所有孩子,GameObject您必须首先找到它,然后访问它的孩子GameObject.GetChild().
正如所提到的评论,GameObject.Find()只会返回第一个GameObject具有确切名称,而不是全部.因此,我们必须遍历所有GO,以找到具有正确名称的GO.
为了完成你的问题,我想我们必须打印出的层次结构GameObject.因此,GameObject如果有父对象,我们只需检查所有的对象,并在列表中收集它们.然后我们可以遍历它们并阅读他们的孩子.
为了检查a GameObject是父项还是具有Child,我们总是要查看Transform给定GameObject 的Component.
要注意的是,这种循环是非常重要的性能任务.
下面是一些示例代码,以便更好地理解我的意思以及Unity3D中的这种行为如何工作:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ReadAllGOs : MonoBehaviour
{
void Start()
{
var parents = FindParentGameObjects();
for (int i = 0; i < parents.Count; i++)
{
Debug.Log("--> Parent Number " + i + " is named: " + parents[i].name);
ReadChildren(parents[i]);
}
}
List<GameObject> FindParentGameObjects()
{
List<GameObject> goList = new List<GameObject>();
foreach (GameObject go in GameObject.FindObjectsOfType(typeof(GameObject)))
{
if (go.transform.parent == null)
{
goList.Add(go);
}
}
return goList;
}
void ReadChildren(GameObject parent)
{
for (int i = 0; i < parent.transform.childCount; i++)
{
GameObject child = parent.transform.GetChild(i).gameObject;
Debug.Log(string.Format("{0} has Child: {1}", parent.name, child.name));
// inner Loop
ReadChildren(child);
}
}
}
Run Code Online (Sandbox Code Playgroud)