예제 #1
0
    static void swapTileSet()
    {
        string path = MAPTools_Utils.getAssetsPath(MAP_Editor.availableTileSets[swapTileSetIndex]);

        swapTileSetObjects = MAPTools_Utils.loadDirectoryContents(path, "*.prefab");

        List <GameObject> layerTiles = new List <GameObject>();

        if (swapTileSetObjects != null)
        {
            GameObject swapTile;

            if (MAP_Editor.findTileMapParent())
            {
                Undo.RegisterFullObjectHierarchyUndo(MAP_Editor.tileMapParent, "Swap Tiles");

                foreach (Transform layer in MAP_Editor.tileMapParent.transform)
                {
                    if (layer.gameObject.name.Contains("layer"))
                    {
                        layerTiles.Clear();

                        foreach (Transform tile in layer)
                        {
                            layerTiles.Add(tile.gameObject);
                        }

                        for (int i = 0; i < layerTiles.Count; i++)
                        {
                            for (int swap = 0; swap < swapTileSetObjects.Length; swap++)
                            {
                                if (layerTiles[i].name == swapTileSetObjects[swap].name)
                                {
                                    EditorUtility.DisplayProgressBar("Swapping Tileset", layerTiles[i].name, (float)i / (float)layerTiles.Count);
                                    swapTile = PrefabUtility.InstantiatePrefab(swapTileSetObjects[swap] as GameObject) as GameObject;
                                    swapTile.transform.parent      = layer;
                                    swapTile.transform.position    = layerTiles[i].transform.position;
                                    swapTile.transform.eulerAngles = layerTiles[i].transform.eulerAngles;
                                    swapTile.transform.GetChild(0).transform.position = layerTiles[i].transform.GetChild(0).transform.position;
                                    DestroyImmediate(layerTiles[i]);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
예제 #2
0
    public static void saveFrozenMesh(string path)
    {
        path = MAPTools_Utils.shortenAssetPath(path);
        int counter = 1;

        if (MAP_Editor.findTileMapParent())
        {
            foreach (Transform child in MAP_Editor.tileMapParent.transform)
            {
                if (child.gameObject.name == "frozenMap")
                {
                    GameObject saveMap = Instantiate(child.gameObject);
                    saveMap.name = MAP_Editor.tileMapParent.name;

                    if (!AssetDatabase.IsValidFolder(path + "/" + saveMap.name + "Meshes"))
                    {
                        AssetDatabase.CreateFolder(path, saveMap.name + "Meshes");
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh();
                    }

                    EditorUtility.ClearProgressBar();

                    foreach (Transform frozenMesh in saveMap.transform)
                    {
                        EditorUtility.DisplayProgressBar("Saving Meshes", "Saving Mesh " + (counter) + ". This might take some time", 1);
                        Mesh saveMesh = Object.Instantiate(frozenMesh.GetComponent <MeshFilter>().sharedMesh) as Mesh;
                        //Unwrapping.GenerateSecondaryUVSet(saveMesh);
                        try
                        {
                            AssetDatabase.CreateAsset(saveMesh, path + "/" + saveMap.name + "Meshes/" + frozenMesh.name + counter + ".asset");
                        }
                        catch
                        {
                            Debug.LogWarning("Failed to create saved map. This is likely due to a new folder being created and Unity not refreshing the asset database. Please retry saving the map.");
                            EditorUtility.ClearProgressBar();
                            return;
                        }
                        frozenMesh.GetComponent <MeshFilter>().mesh = saveMesh;
                        counter++;
                    }

                    EditorUtility.ClearProgressBar();

                    Object prefabAlreadyCreated = AssetDatabase.LoadAssetAtPath(path + "/" + saveMap.name + ".prefab", typeof(GameObject));

                    if (prefabAlreadyCreated != null)
                    {
                        PrefabUtility.SaveAsPrefabAssetAndConnect(saveMap, path + "/" + saveMap.name + ".prefab", InteractionMode.AutomatedAction);
                    }
                    else
                    {
                        PrefabUtility.SaveAsPrefabAsset(saveMap, path + "/" + saveMap.name + ".prefab");
                    }
                    AssetDatabase.SaveAssets();
                    if (saveMap != null)
                    {
                        DestroyImmediate(saveMap);
                    }
                }
            }

            AssetDatabase.Refresh();
        }
    }
예제 #3
0
    public static void createCustomBrush(string customBrushName)
    {
        subMeshCount = 0;

        MAP_tileFunctions.checkTileSelectionStatus();

        GameObject tileParentObject = new GameObject();

        tileParentObject.AddComponent <MAP_tileGizmo>();
        tileParentObject.GetComponent <MAP_tileGizmo>().customBrushMeshName = new List <string>();

        string customBrushGUID = Guid.NewGuid().ToString("N");

        tileParentObject.name = customBrushName + Define.MAP_PREFAB;

        string destinationPath = MAPTools_Utils.getAssetsPath(MAP_Editor.availableTileSets[MAP_Editor.currentTileSetIndex]) + Define.CUSTOM_BRUSHFES + "/";

        if (MAP_Editor.selectTiles.Count > 0)
        {
            // When creating a custom brush we need to find the lowest Z transform in the selection to become the pivot transform
            GameObject bottomLevelOfSelection = MAP_Editor.selectTiles[0];

            foreach (GameObject checkObjects in MAP_Editor.selectTiles)
            {
                if (checkObjects.transform.position.z < bottomLevelOfSelection.transform.position.z)
                {
                    bottomLevelOfSelection = checkObjects;
                }
            }

            // center the parent object around the lowest block to make sure all the selected brushes are centered around the parent
            tileParentObject.transform.position = bottomLevelOfSelection.transform.position;

            // New Custom Brush implementation. Uses a technique similar to the freeze map, except for selected tils

            List <GameObject> tilesToCombine = new List <GameObject>();
            List <Transform>  _freezeTiles   = new List <Transform>();
            List <Transform>  freezeTiles    = new List <Transform>();

            foreach (GameObject tile in MAP_Editor.selectTiles)
            {
                tile.GetComponentsInChildren <Transform>(false, _freezeTiles);
                freezeTiles.AddRange(_freezeTiles);
            }

            foreach (Transform tile in freezeTiles)
            {
                if (tile.GetComponent <MeshRenderer>())
                {
                    tilesToCombine.Add(tile.gameObject);
                }
            }

            tilesToCombine = tilesToCombine.OrderBy(x => x.GetComponent <MeshRenderer>().sharedMaterial.name).ToList();

            Material previousMaterial = tilesToCombine[0].GetComponent <MeshRenderer>().sharedMaterial;

            List <CombineInstance> combine     = new List <CombineInstance>();
            CombineInstance        tempCombine = new CombineInstance();

            int vertexCount = 0;

            foreach (GameObject mesh in tilesToCombine)
            {
                vertexCount += mesh.GetComponent <MeshFilter>().sharedMesh.vertexCount;

                if (vertexCount > 60000)
                {
                    vertexCount = 0;
                    newSubMesh(combine, mesh.GetComponent <MeshRenderer>().sharedMaterial, tileParentObject, destinationPath, customBrushGUID);
                    combine = new List <CombineInstance>();
                }

                if (mesh.GetComponent <MeshRenderer>().sharedMaterial.name != previousMaterial.name)
                {
                    newSubMesh(combine, previousMaterial, tileParentObject, destinationPath, customBrushGUID);
                    combine = new List <CombineInstance>();
                }

                tempCombine.mesh      = mesh.GetComponent <MeshFilter>().sharedMesh;
                tempCombine.transform = mesh.GetComponent <MeshFilter>().transform.localToWorldMatrix;
                combine.Add(tempCombine);
                previousMaterial = mesh.GetComponent <MeshRenderer>().sharedMaterial;
            }

            newSubMesh(combine, previousMaterial, tileParentObject, destinationPath, customBrushGUID);

            tileParentObject.transform.position = Vector3.zero;

            // Add the prefab to the project

#if UNITY_2018_3_OR_NEWER
            PrefabUtility.SaveAsPrefabAsset(tileParentObject, destinationPath + tileParentObject.name);
#else
            PrefabUtility.CreatePrefab(destinationPath + tileParentObject.name, tileParentObject);
#endif
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh(); // refesh the asset database to tell unity changes have been made

            // remove our temporary builder object

            Debug.Log("Custom Brush " + tileParentObject.name + " created.");

            DestroyImmediate(tileParentObject);
            MAP_Editor.selectTiles.Clear();

            // reload the custom brushes
            MAP_Editor.loadCustomBrushes();
        }
    }