在gameObject中创建一个gameObject

时间:2013-07-28 14:07:21

标签: c# unity3d

如何在gameObject中创建gameObject? 各位大家好,我无法解决这个解决方案的问题,不知道为什么但是它不起作用,我是团结3d的新手,所以也许我写错了代码并感谢所有给我时间的人。

我想要一个建议,如果不使用大量的cpu,我该怎么做呢。

我试过了:

GameObject.Find(..),GameObject.Instantiate,GameObject.Find("TerrainData").GetComponentInChildren

它不起作用。 这是我的代码:

if (parentObj == 1)
{   
    GameObject newParentObject = new GameObject();

    newParentObject.active = true;
    newParentObject.name = "TerrainData";
    //newObjs.transform.position = loc*chunkSize;
    //newObjs.SendMessage("regenerateMesh");
    parentObj++;
}

while (m_terrainToCreate.Count > 0 && Time.realtimeSinceStartup - curTime < 0.016)
{
    Vector3 loc = m_terrainToCreate.Dequeue();
    int[] pos = getCachedChunkPos(Mathf.RoundToInt(loc.x), Mathf.RoundToInt(loc.y), Mathf.RoundToInt(loc.z));

    GameObject newObj = m_meshCache[pos[0], pos[1], pos[2]];// = m_freePool.Dequeue();

    newObj.active = true;
    newObj.name = "TerrainChunk (" + loc.x.ToString() + ", " + loc.y.ToString() + ", " + loc.z.ToString() + ")";
    newObj.transform.position = loc*chunkSize;
    newObj.SendMessage("regenerateMesh");
}

“”链接“,设置孩子的transform.parent”是的,就是这样。 我怎样才能捕捉到“TerrainData”游戏对象?

尝试:

    GameObject newObj = m_meshCache[pos[0], pos[1], pos[2]];// = m_freePool.Dequeue();

    newObj.transform.parent = GameObject.Find("TerrainData");

newObj.gameObject.transform.parent = gameObject.transform.Find ("TerrainData");
newObj.transform.parent = transform.Find("TerrainData");

但我认为它不会改变对象?它会改变'变换'吗?

1 个答案:

答案 0 :(得分:3)

试试这个......

创建一个包含对象的属性:

public Transform Terrain;

然后你可以这样做......

terrain = Instantiate(Terrain) as Transform;
terrain.parent = gameObject.transform;
terrain.position = new Vector3(0, 0, 0);
terrain.name = "My Terrain";