Ejemplo n.º 1
0
    static void loadPreviewTiles()
    {
        try
        {
            string path = YuTools_Utils.getAssetPath(availableTileSets[currentTileSetIndex]);

            currentTileSetObjects = YuTools_Utils.loadDirectoryContents(path, "*.prefab");
            currentTile           = currentTileSetObjects[0];
        }
        catch
        {
            Debug.Log("Tile Sets seem to be missing. Please reload the tile sets");
        }
    }
    public static void createCustomBrush()
    {
        GameObject tileParentObject = new GameObject();

        if (YuME_mapEditor.selectedTiles.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 = YuME_mapEditor.selectedTiles[0];

            foreach (GameObject checkObjects in YuME_mapEditor.selectedTiles)
            {
                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;

            // Build the brush by finding the parent prefab, creating an instance and then setting it to the transform of the original scene prefab
            foreach (GameObject tile in YuME_mapEditor.selectedTiles)
            {
                GameObject newBrushObject = (GameObject)PrefabUtility.InstantiatePrefab(PrefabUtility.GetPrefabParent(tile) as GameObject);

                newBrushObject.transform.position    = tile.transform.position;
                newBrushObject.transform.eulerAngles = tile.transform.eulerAngles;
                newBrushObject.transform.localScale  = tile.transform.localScale;
                newBrushObject.transform.parent      = tileParentObject.transform;
            }

            // reset the parents position so it is zero when dropped in the scene
            tileParentObject.transform.position = Vector3.zero;

            // Add the prefab to the project
            tileParentObject.name = "CustomBrush" + YuTools_Utils.numberOfFilesInFolder(YuME_mapEditor.availableTileSets[YuME_mapEditor.currentTileSetIndex].customBrushDestinationFolder, "*_YuME.prefab") + "_YuME.prefab";
            string destinationPath = YuTools_Utils.getAssetPath(YuME_mapEditor.availableTileSets[YuME_mapEditor.currentTileSetIndex]) + "CustomBrushes/";
            PrefabUtility.CreatePrefab(destinationPath + tileParentObject.name, tileParentObject);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh(); // refesh the asset database to tell unity changes have been made

            // remove our temporary builder object
            DestroyImmediate(tileParentObject);
            YuME_mapEditor.selectedTiles.Clear();

            // reload the custom brushes
            YuME_mapEditor.loadCustomBrushes();
        }
    }
Ejemplo n.º 3
0
    static void swapTileSet()
    {
        string path = YuTools_Utils.getAssetPath(YuME_mapEditor.availableTileSets[swapTileSetIndex]);

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

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

        if (swapTileSetObjects != null)
        {
            GameObject swapTile;

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

                foreach (Transform layer in YuME_mapEditor.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;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
Ejemplo n.º 4
0
    public static void loadCustomBrushes()
    {
        try
        {
            string path = YuTools_Utils.getAssetPath(availableTileSets[currentTileSetIndex]) + "CustomBrushes";

            if (path != null)
            {
                currentCustomBrushes = YuTools_Utils.loadDirectoryContents(path, "*_YuME.prefab");

                if (currentCustomBrushes == null)
                {
                    createCustomBrushFolder(path);
                }
            }
        }
        catch
        {
            Debug.Log("Custom Brush Folder missing");
        }
    }
Ejemplo n.º 5
0
    // ----------------------------------------------------------------------------------------------------
    // ----- Tileset functions for preview in the editor window
    // ----------------------------------------------------------------------------------------------------

    public static void importTileSets(bool fullRescan)
    {
        // find all assest of type YuME_tileSetData
        string[] guids = AssetDatabase.FindAssets("t:YuME_tileSetData");

        if (guids.Length > 0)
        {
            availableTileSets = new List <YuME_tilesetData>();

            foreach (string guid in guids)
            {
                YuME_tilesetData tempData = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), typeof(YuME_tilesetData)) as YuME_tilesetData;

                if (fullRescan)
                {
                    EditorUtility.DisplayProgressBar("Reloading Tile Set: " + tempData.name, "Note: Reimport can take some time to complete", 0f);
                    string   path             = YuTools_Utils.getAssetPath(tempData);
                    string[] containedPrefabs = YuTools_Utils.getDirectoryContents(YuTools_Utils.getAssetPath(tempData), "*.prefab");

                    if (containedPrefabs != null)
                    {
                        foreach (string prefab in containedPrefabs)
                        {
                            AssetDatabase.ImportAsset(path + prefab);
                        }
                    }
                    if (tempData != null)
                    {
                        path             = YuTools_Utils.getAssetPath(tempData) + "CustomBrushes/";
                        containedPrefabs = YuTools_Utils.getDirectoryContents(YuTools_Utils.getAssetPath(tempData) + "CustomBrushes/", "*.prefab");

                        if (containedPrefabs != null)
                        {
                            foreach (string prefab in containedPrefabs)
                            {
                                AssetDatabase.ImportAsset(path + prefab);
                            }
                        }
                    }
                }

                availableTileSets.Add(tempData);
            }

            if (fullRescan)
            {
                EditorUtility.ClearProgressBar();
            }

            tileSetNames = new string[availableTileSets.Count];

            for (int i = 0; i < availableTileSets.Count; i++)
            {
                tileSetNames[i] = availableTileSets[i].tileSetName;
            }

            loadPreviewTiles();
        }
        else
        {
            Debug.Log("No tile sets have been created");
        }
    }
    public static void createCustomBrush(string customBrushName)
    {
        subMeshCount = 0;

        YuME_tileFunctions.checkTileSelectionStatus();

        GameObject tileParentObject = new GameObject();

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

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

        tileParentObject.name = customBrushName + "_YuME.prefab";

        string destinationPath = YuTools_Utils.getAssetPath(YuME_mapEditor.availableTileSets[YuME_mapEditor.currentTileSetIndex]) + "CustomBrushes/";

        if (YuME_mapEditor.selectedTiles.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 = YuME_mapEditor.selectedTiles[0];

            foreach (GameObject checkObjects in YuME_mapEditor.selectedTiles)
            {
                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 YuME_mapEditor.selectedTiles)
            {
                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);
            YuME_mapEditor.selectedTiles.Clear();

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