示例#1
0
    private static void CreateTilesPrefab()
    {
        foreach (KeyValuePair <string, MapTilesData> item in mapTilesDataDic)
        {
            MapTilesData tileData = item.Value;
            System.Collections.Generic.List <GameObject> textureGos = null;
            if (mapTextureTilesGos.TryGetValue(item.Key, out textureGos))
            {
                for (int index = 0; index < textureGos.Count; index++)
                {
                    textureGos[index].transform.SetParent(tileData.tilesGo.transform);
                }
            }

            int row = Mathf.FloorToInt(tileData.goPos.x / FileGridInterval);
            int col = Mathf.FloorToInt(tileData.goPos.z / FileGridInterval);
            tileData.tilesGo.transform.SetParent(null);
            string fileName = row + "_" + col;
            string filePath = string.Format(MapTilesPrefabSavePath, fileName);
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            //  EditorUtility.DisplayProgressBar("创建prefab", fileName + tileData.tilesKey, 0);
            PrefabUtility.CreatePrefab(filePath + "/" + tileData.tilesKey + ".prefab",
                                       tileData.tilesGo);
        }
    }
示例#2
0
    public static void MapTilesTPrefab()
    {
        mapTextureTilesGos.Clear();
        mapTilesDataDic.Clear();
        //地图瓦片
        GameObject mapTilesRoot = GameObject.Find("MapTilesRoot/MapTiles");

        if (mapTilesRoot == null)
        {
            return;
        }
        Renderer[] mapTilesTransform = mapTilesRoot.GetComponentsInChildren <Renderer>();
        Transform  go  = null;
        int        row = 0;
        int        col = 0;

        for (int index = 0; index < mapTilesTransform.Length; index++)
        {
            go  = mapTilesTransform[index].transform;
            row = Mathf.FloorToInt(go.position.x / TilesGridInterval);
            col = Mathf.FloorToInt(go.position.z / TilesGridInterval);
            string key = row + "_" + col;
            if (!mapTilesDataDic.ContainsKey(key))
            {
                mapTilesDataDic[key] = new MapTilesData()
                {
                    tilesKey = key,
                    goCol    = col,
                    goRow    = row,
                    tilesGo  = go.gameObject,
                    goPos    = go.position,
                }
            }
            ;
            else
            {
                Debug.LogError("地图瓦片重复Key:" + key);
            }
        }

        //地图纹理瓦片(草地、沙地...)
        GameObject mapTextureTilesRoot = GameObject.Find("MapTilesRoot/MapTextureTiles");

        if (mapTextureTilesRoot == null)
        {
            return;
        }
        mapTilesTransform = mapTextureTilesRoot.GetComponentsInChildren <Renderer>();
        for (int index = 0; index < mapTilesTransform.Length; index++)
        {
            go  = mapTilesTransform[index].transform;
            row = Mathf.FloorToInt(go.position.x / TilesGridInterval);
            col = Mathf.FloorToInt(go.position.z / TilesGridInterval);
            string key = row + "_" + col;
            if (!mapTextureTilesGos.ContainsKey(key))
            {
                mapTextureTilesGos[key] = new System.Collections.Generic.List <GameObject>();
            }
            GameObject tempGo = go.gameObject;// as GameObject;
            mapTextureTilesGos[key].Add(tempGo);
        }
        //生成预设
        CreateTilesPrefab();
        EditorUtility.ClearProgressBar();


        //销毁备份GO
        DestroyTempGo();
        AssetDatabase.Refresh();
        EditorSceneManager.OpenScene("Assets/Scences/SceneEditor.unity");
    }