コード例 #1
0
ファイル: bigmap_editor.cs プロジェクト: gangzi4494/bigmap
    /**/

    static void ClassifyGameObject(GameObject obj)
    {
        Vector3 pos = obj.transform.position;

        //int targetChunkX = (int)(pos.x / MapConfig.width) + 1;
        //int targetChunkZ = (int)(pos.z / MapConfig.width) + 1;

        string chunkName = MapIndexConfig.GetMapName(pos);//  ChunkRootNamePrefix + string.Format("{0}_{1}", targetChunkX, targetChunkZ);


        GameObject chunkRoot = GameObject.Find(chunkName);

        if (chunkRoot == null)
        {
            chunkRoot = new GameObject(chunkName);
        }


        //复制层次关系到Chunk的节点下面
        GameObject        tempObj   = obj;
        List <GameObject> objs2Copy = new List <GameObject>();

        while (tempObj.transform.parent)
        {
            objs2Copy.Add(tempObj.transform.parent.gameObject);
            tempObj = tempObj.transform.parent.gameObject;
        }

        tempObj = chunkRoot;

        for (int i = objs2Copy.Count - 1; i > -1; --i)
        {
            GameObject targetObj = objs2Copy[i];

            // 对于符合Chunk命名规则的父节点不进行拷贝过程。
            if (targetObj.name.StartsWith(ChunkRootNamePrefix))
            {
                continue;
            }

            Transform parent = tempObj.transform.Find(targetObj.name);
            if (parent == null)
            {
                parent = new GameObject(targetObj.name).transform;
                //CopyComponents(targetObj, parent.gameObject);
                parent.parent = tempObj.transform;
                targetObj     = parent.gameObject;
            }
            tempObj = parent.gameObject;
        }

        Transform tempParent = obj.transform.parent;

        obj.transform.parent = tempObj.transform;

        // 移动完毕之后发现父节点没有孩子节点的情况下,向上遍历将无用节点删除。
        while (tempParent != null && tempParent.childCount == 0)
        {
            Transform temp = tempParent.parent;
            //EngineUtils.Destroy(tempParent.gameObject);
            tempParent = temp;
        }
    }