Ejemplo n.º 1
0
        private void OnValidate()
        {
            if (Object != null)
            {
                if (_objectCurrent == null)
                {
                    // if sprite already exists (e.g. at startup), then load it, otherwise create a new one
                    EditorApplication.delayCall += () => { PreviewSprite = PreviewSpriteBuilder.LoadSprite(Object) ?? PreviewSpriteBuilder.Create(Object); };
                }
                else if (Object != _objectCurrent)
                {
                    // from one object -> other (overwrite current sprite)
                    EditorApplication.delayCall += () => { PreviewSprite = PreviewSpriteBuilder.Create(Object); };
                }
            }
            else if (_objectCurrent != null)
            {
                // setting to None object (delete current sprite)
                var obj = _objectCurrent;
                EditorApplication.delayCall += () => { PreviewSpriteBuilder.DeleteSprite(obj); };
            }

            _objectCurrent = Object;

            if (_objectCurrent != null)
            {
                IsItem = _objectCurrent.GetComponentInChildren <RegisterItem>() != null;
            }
        }
Ejemplo n.º 2
0
    private void OnValidate()
    {
        CheckTileType(ref Structure, LayerType.Walls, LayerType.Windows);
        CheckTileType(ref Object, LayerType.Objects);
        CheckTileType(ref Floor, LayerType.Floors);
        CheckTileType(ref Base, LayerType.Base);

        if (Structure != _structureCurrent || Object != _objectCurrent || Floor != _floorCurrent ||
            Base != _baseCurrent)
        {
            if (_structureCurrent == null && _objectCurrent == null && _floorCurrent == null &&
                _baseCurrent == null)
            {
                // if everything is null, it could be that it's loading on startup, so there already should be an preview sprite to load
                EditorApplication.delayCall += () =>
                {
                    PreviewSprite = PreviewSpriteBuilder.LoadSprite(this) ?? PreviewSpriteBuilder.Create(this);
                    ;
                };
            }
            else
            {
                // something changed, so create a new preview sprite
                EditorApplication.delayCall += () => { PreviewSprite = PreviewSpriteBuilder.Create(this); };
            }
        }

        _structureCurrent = Structure;
        _objectCurrent    = Object;
        _floorCurrent     = Floor;
        _baseCurrent      = Base;
    }
Ejemplo n.º 3
0
    public static void GenerateTiles(string subject)
    {
        //Moving old tiles
        string basePath  = Application.dataPath + "/Resources/Prefabs/";
        string basePath2 = basePath + subject + "/";
        //			Debug.Log (basePath2);
        int counter = 0;
        int created = 0;

        string[] scan = Directory.GetFiles(basePath2, "*.prefab", SearchOption.AllDirectories);
        foreach (string file in scan)
        {
            counter++;
            int t = scan.Length;
            EditorUtility.DisplayProgressBar(counter + "/" + scan.Length + " Generating Tiles for " + subject, "Tile: " + counter,
                                             counter / (float)scan.Length);
            //			Debug.Log ("Longpath data: " + file);

            //Get the filename without extention and path
            string name = Path.GetFileNameWithoutExtension(file);
            //			Debug.Log ("Creating tile for prefab: " + name);

            //Generating the path needed to hook onto for selecting the game object
            string smallPath = file.Substring(file.IndexOf("Assets") + 0);
            //			Debug.Log ("smallpath data: " + smallPath);


            //Generating the path needed to chose the right tile output sub-folder
            string subPath = smallPath.Substring(smallPath.IndexOf(subject) + 7);
            //			Debug.Log ("subPath data: " + subPath);
            string barePath = subPath.Substring(0, subPath.LastIndexOf(Path.DirectorySeparatorChar));
            //			Debug.Log ("barePath data: " + barePath);

            //Check if tile already exists
            if (File.Exists(Application.dataPath + "/Tilemaps/Tiles/" + subject + "/" + barePath + "/" + name + ".asset"))
            {
                //		Debug.Log ("A tile for " + name + " already exists... Skipping...");
            }
            else
            {
                //setup building the tile//
                ObjectTile tile = TileBuilder.CreateTile <ObjectTile>(LayerType.Objects);

                //Cast the gameobject
                GameObject cast = AssetDatabase.LoadAssetAtPath(smallPath, typeof(GameObject)) as GameObject;
                if (barePath == "/WallMounts")
                {
                    tile.Rotatable = true;
                    tile.Offset    = true;
                }
                else
                {
                    tile.Rotatable = false;
                    tile.Offset    = false;
                }
                tile.Object = cast;
                //Create the tile
                TileBuilder.CreateAsset(tile, name, "Assets/Tilemaps/Tiles/" + subject + "/" + barePath);
                PreviewSpriteBuilder.Create(cast);
                created++;
            }
        }
        EditorUtility.ClearProgressBar();
        Debug.Log(created + " / " + counter + " Tiles created for prefabs");
    }
Ejemplo n.º 4
0
    public static void GenerateTileMapPalette()
    {
        var basePath = Application.dataPath + "/Resources/Prefabs/";

        var generatedFolder = Path.GetDirectoryName($"{ Application.dataPath }/Tilemaps/Palettes/Generated/");

        if (generatedFolder != null)
        {
            Directory.CreateDirectory(generatedFolder);
        }

        foreach (var palette in _palettes)
        {
            var    title = palette[0];
            string subPath;
            string assetPath;
            string filePath;

            // Create palette asset
            var go = new GameObject();
            go.AddComponent <Grid>();

            var layerOne = new GameObject
            {
                name = "Layer1"
            };
            layerOne.transform.SetParent(go.transform);
            var tilemap         = layerOne.AddComponent <Tilemap>();
            var tilemapRenderer = layerOne.AddComponent <TilemapRenderer>();
            tilemapRenderer.enabled = false;

            var y = 0;
            for (var i = 1; i < palette.Length; i++)
            {
                var x      = 0;
                var folder = palette[i];
                var files  = Directory.GetFiles(basePath + folder, "*.prefab", SearchOption.AllDirectories);
                foreach (var file in files)
                {
                    var name = Path.GetFileNameWithoutExtension(file);

                    subPath   = $"Tilemaps/Resources/Tiles/Generated/{ title }/{ name }";
                    assetPath = $"Assets/{ subPath }";
                    filePath  = $"{ Application.dataPath }/{ subPath }/{ name }.asset";

                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }

                    // Build the tile
                    var tile = TileBuilder.CreateTile <ObjectTile>(LayerType.Objects);
                    var cast = AssetDatabase.LoadAssetAtPath(file.Substring(file.IndexOf("Assets", StringComparison.Ordinal) + 0), typeof(GameObject)) as GameObject;
                    tile.Rotatable     = false;
                    tile.Offset        = false;
                    tile.Object        = cast;
                    tile.PreviewSprite = PreviewSpriteBuilder.GetSpriteWithoutSaving(cast);

                    TileBuilder.CreateAsset(tile, name, assetPath);

                    var pos = new Vector3Int(x, y, 0);

                    tilemap.SetEditorPreviewTile(pos, tile);
                    tilemap.SetTile(pos, tile);

                    EditorUtility.SetDirty(tile);

                    x++;
                }

                y--;
            }

            var paletteInstance = ScriptableObject.CreateInstance <GridPalette>();
            paletteInstance.name = "Palette Settings";

            subPath   = $"/Tilemaps/Palettes/Generated/{ title }.prefab";
            assetPath = $"Assets{ subPath }";
            filePath  = $"{ Application.dataPath }{ subPath }";

            if (File.Exists(filePath))
            {
                AssetDatabase.DeleteAsset(assetPath);
                AssetDatabase.SaveAssets();
            }

            var prefab = PrefabUtility.SaveAsPrefabAsset(go, assetPath);
            AssetDatabase.AddObjectToAsset(paletteInstance, prefab);
            AssetDatabase.SaveAssets();

            GameObject.DestroyImmediate(go);
        }
    }