public static GameObject CreateNewPalette(string folder_path, string name, Grid.CellLayout layout, GridPalette.CellSizing cell_sizing, Vector3 cell_size, Grid.CellSwizzle swizzle)
        {
            var tmp_go = new GameObject(name);
            var grid   = tmp_go.AddComponent <Grid>();

            // We set size to kEpsilon to mark this as new uninitialized palette
            // Nice default size can be decided when first asset is dragged in
            grid.cellSize    = cell_size;
            grid.cellLayout  = layout;
            grid.cellSwizzle = swizzle;
            CreateNewLayer(tmp_go, "Layer1", layout);

            var path = AssetDatabase.GenerateUniqueAssetPath(folder_path + "/" + name + ".prefab");

            Object prefab  = PrefabUtility.SaveAsPrefabAssetAndConnect(tmp_go, path, InteractionMode.AutomatedAction);
            var    palette = GridPalette.CreateInstance <GridPalette>();

            palette.name       = "Palette Settings";
            palette.cellSizing = cell_sizing;
            AssetDatabase.AddObjectToAsset(palette, prefab);
            PrefabUtility.ApplyPrefabInstance(tmp_go, InteractionMode.AutomatedAction);
            AssetDatabase.Refresh();

            GameObject.DestroyImmediate(tmp_go);
            return(AssetDatabase.LoadAssetAtPath <GameObject>(path));
        }
    private void createTestPalette()
    {
        GameObject testObj = new GameObject();

        testObj.AddComponent(typeof(Grid));
        GameObject testChild = new GameObject();

        testChild.AddComponent(typeof(Tilemap));
        testChild.AddComponent(typeof(TilemapRenderer));
        testChild.name             = "Layer1";
        testChild.transform.parent = testObj.transform;

        Tilemap tilemap = testChild.GetComponent(typeof(Tilemap)) as Tilemap;

        tilemap.SetTile(Vector3Int.zero, debugTile);


        GridPalette testPallet = ScriptableObject.CreateInstance <GridPalette>();

        testPallet.name = "palette Settings";


        //string pathUntrimmed = EditorUtility.SaveFolderPanel("Save Tile", "", "");
        // string path = pathUntrimmed.Substring(pathUntrimmed.LastIndexOf("Unity Editor Test"));
        Object prefab = PrefabUtility.SaveAsPrefabAsset(testObj, "Assets/tiles/AATesto.prefab");

        AssetDatabase.AddObjectToAsset(testPallet, prefab);
        AssetDatabase.SaveAssets();
    }
Esempio n. 3
0
        internal static GridPalette GetGridPaletteFromPaletteAsset(Object palette)
        {
            string      assetPath    = AssetDatabase.GetAssetPath(palette);
            GridPalette paletteAsset = AssetDatabase.LoadAssetAtPath <GridPalette>(assetPath);

            return(paletteAsset);
        }
    //Get Sprites
    //string pathstr = AssetDatabase.GetAssetPath(debugSprite);
    //Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(pathstr).OfType<Sprite>().ToArray();


    private void createTilePallet()
    {
        GameObject baseObj = new GameObject();

        baseObj.AddComponent(typeof(Grid));
        GameObject layer1 = new GameObject();

        layer1.AddComponent(typeof(Tilemap));
        layer1.AddComponent(typeof(TilemapRenderer));
        layer1.name             = "Layer1";
        layer1.transform.parent = baseObj.transform;

        Tilemap tilemap = layer1.GetComponent(typeof(Tilemap)) as Tilemap;

        for (int x = 0; x < tilePosTable.GetLength(0); x++)
        {
            for (int y = 0; y < tilePosTable.GetLength(1); y++)
            {
                Tile tile = AssetDatabase.LoadAssetAtPath(path + tilePosTable[x, y] + ".asset", typeof(Tile)) as Tile;
                tilemap.SetTile(new Vector3Int(x, y, 0), tile);
            }
        }
        GridPalette pallet = ScriptableObject.CreateInstance <GridPalette>();

        pallet.name = "Pallet Settings";

        Object prefab = PrefabUtility.SaveAsPrefabAsset(baseObj, "Assets/tiles/AATesto.prefab");//change later

        AssetDatabase.AddObjectToAsset(pallet, prefab);
        AssetDatabase.SaveAssets();
    }
Esempio n. 5
0
        /// <summary>
        /// Creates a Palette Asset at the given folder path.
        /// </summary>
        /// <param name="folderPath">Folder Path of the Palette Asset.</param>
        /// <param name="name">Name of the Palette Asset.</param>
        /// <param name="layout">Grid Layout of the Palette Asset.</param>
        /// <param name="cellSizing">Cell Sizing of the Palette Asset.</param>
        /// <param name="cellSize">Cell Size of the Palette Asset.</param>
        /// <param name="swizzle">Cell Swizzle of the Palette.</param>
        /// <param name="sortMode">Transparency Sort Mode for the Palette</param>
        /// <param name="sortAxis">Transparency Sort Axis for the Palette</param>
        /// <returns>The created Palette Asset if successful.</returns>
        public static GameObject CreateNewPalette(string folderPath
                                                  , string name
                                                  , GridLayout.CellLayout layout
                                                  , GridPalette.CellSizing cellSizing
                                                  , Vector3 cellSize
                                                  , GridLayout.CellSwizzle swizzle
                                                  , TransparencySortMode sortMode
                                                  , Vector3 sortAxis)
        {
            GameObject temporaryGO = new GameObject(name);
            Grid       grid        = temporaryGO.AddComponent <Grid>();

            // We set size to kEpsilon to mark this as new uninitialized palette
            // Nice default size can be decided when first asset is dragged in
            grid.cellSize    = cellSize;
            grid.cellLayout  = layout;
            grid.cellSwizzle = swizzle;
            CreateNewLayer(temporaryGO, "Layer1", layout);

            string path = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + name + ".prefab");

            Object      prefab  = PrefabUtility.SaveAsPrefabAssetAndConnect(temporaryGO, path, InteractionMode.AutomatedAction);
            GridPalette palette = CreateGridPalette(cellSizing, sortMode, sortAxis);

            AssetDatabase.AddObjectToAsset(palette, prefab);
            PrefabUtility.ApplyPrefabInstance(temporaryGO, InteractionMode.AutomatedAction);
            AssetDatabase.Refresh();

            Object.DestroyImmediate(temporaryGO);
            return(AssetDatabase.LoadAssetAtPath <GameObject>(path));
        }
Esempio n. 6
0
        static void CreatePalette(string tilesetName, string tilesetTileTargetDir, ImportedTile[] tileAssets, bool singleImageTileset, int tileWidth, int tileHeight, int columns, int cellWidth, int cellHeight)
        {
            GameObject newPaletteGO = new GameObject(tilesetName, typeof(Grid));

            newPaletteGO.GetComponent <Grid>().cellSize = new Vector3(1.0f, 1.0f, 0.0f);
            GameObject paletteTilemapGO = new GameObject("Layer1", typeof(Tilemap), typeof(TilemapRenderer));

            paletteTilemapGO.transform.SetParent(newPaletteGO.transform);

            paletteTilemapGO.GetComponent <TilemapRenderer>().enabled = false;

            Tilemap paletteTilemap = paletteTilemapGO.GetComponent <Tilemap>();

            paletteTilemap.tileAnchor = TiledTSXImporter.GetPivot(tileWidth, tileHeight, cellWidth, cellHeight);
            if (columns <= 0)
            {
                columns = 5;
            }

            if (singleImageTileset)
            {
                for (int i = 0; i < tileAssets.Length; i++)
                {
                    Sprite sprite = tileAssets[i].tile.sprite;
                    Rect   rect   = sprite.rect;
                    int    x      = (int)rect.x / tileWidth;
                    int    y      = (int)rect.y / tileHeight;
                    paletteTilemap.SetTile(new Vector3Int(x, y, 0), tileAssets[i].tile);
                }
            }
            else
            {
                int x = 0;
                int y = 0;
                for (int i = 0; i < tileAssets.Length; i++)
                {
                    paletteTilemap.SetTile(new Vector3Int(x, y, 0), tileAssets[i].tile);
                    x++;
                    if (x >= columns)
                    {
                        x = 0;
                        y--;
                    }
                }
            }
            string palettePath = tilesetTileTargetDir + Path.DirectorySeparatorChar + tilesetName + ".prefab";

            palettePath = palettePath.Replace('\\', '/');
            UnityEngine.Object newPrefab = PrefabUtility.CreateEmptyPrefab(palettePath);
            PrefabUtility.ReplacePrefab(newPaletteGO, newPrefab, ReplacePrefabOptions.Default);
            GameObject.DestroyImmediate(newPaletteGO);

            GridPalette gridPalette = ScriptableObject.CreateInstance <GridPalette>();

            gridPalette.cellSizing = GridPalette.CellSizing.Automatic;
            gridPalette.name       = "Palette Settings";
            AssetDatabase.AddObjectToAsset(gridPalette, palettePath);
            AssetDatabase.ImportAsset(palettePath);
        }
Esempio n. 7
0
        public static GridPalette CreateGridPalette(GridPalette.CellSizing cellSizing)
        {
            var palette = GridPalette.CreateInstance <GridPalette>();

            palette.name       = "Palette Settings";
            palette.cellSizing = cellSizing;
            return(palette);
        }
Esempio n. 8
0
    public static void PopulatePalette(string assetPath)
    {
        bool dirty = false;

        GameObject  paletteObject = AssetDatabase.LoadAssetAtPath <GameObject>(assetPath);
        GridPalette settings      = null;
        Tilemap     map           = paletteObject.GetComponentInChildren <Tilemap>();

        foreach (Object obj in AssetDatabase.LoadAllAssetsAtPath(assetPath))
        {
            if (obj is GridPalette)
            {
                settings = (GridPalette)obj;
            }
        }
        if (settings == null)
        {
            settings            = ScriptableObject.CreateInstance <GridPalette>();
            settings.name       = EditorUtils.NameFromPath(assetPath);
            settings.cellSizing = GridPalette.CellSizing.Automatic;
            AssetDatabase.AddObjectToAsset(settings, paletteObject);
            dirty = true;
        }

        string    name           = EditorUtils.NameFromPath(assetPath);
        string    localDirectory = EditorUtils.LocalDirectoryFromPath(assetPath);
        string    pngDirectory   = localDirectory.Substring(0, localDirectory.LastIndexOf('/'));
        string    tileDirectory  = pngDirectory + "/Tiles/" + name;
        string    texturePath    = pngDirectory + "/" + name + ".png";
        Texture2D tex            = AssetDatabase.LoadAssetAtPath <Texture2D>(texturePath);

        for (int y = 0; y < tex.height / Map.TileSizePx; y += 1)
        {
            for (int x = 0; x < tex.width / Map.TileSizePx; x += 1)
            {
                Vector3Int pos          = new Vector3Int(x, (tex.height / Map.TileSizePx - y - 1), 0);
                TileBase   existingTile = map.GetTile <TileBase>(pos);

                string    tileName = NameForTile(name, x, y);
                Map2DTile newTile  = AssetDatabase.LoadAssetAtPath <Map2DTile>(tileDirectory + "/" + tileName + ".asset");

                if (!newTile.EqualsTile(existingTile))
                {
                    dirty = true;
                    map.SetTile(pos, newTile);
                }
            }
        }

        if (dirty)
        {
            AssetDatabase.SaveAssets();
        }
    }
Esempio n. 9
0
    private void UpdateWithPalette(GridPalette newPalette)
    {
        tileSelectRect    = new Rect(0, 0, 1, 1);
        paletteBufferSize = Vector2Int.zero;
        TacticsTerrainMesh terrain = (TacticsTerrainMesh)target;

        palette             = newPalette;
        terrain.paletteName = palette.name;
        string     palettePath   = "Assets/Tilesets/Palettes/" + palette.name + ".prefab";
        GameObject tilesetObject = AssetDatabase.LoadAssetAtPath <GameObject>(palettePath);

        tileset = tilesetObject.transform.GetChild(0).GetComponent <Tilemap>();
    }
Esempio n. 10
0
        public void SetUp()
        {
            AssetDatabase.CreateFolder("Assets", "Temp");

            AssetDatabase.CopyAsset(defaultPalettePath, defaultTemporaryPalettePath);
            AssetDatabase.Refresh();

            var gridPalette = AssetDatabase.LoadAssetAtPath(defaultTemporaryPalettePath, typeof(GridPalette));

            if (gridPalette == null)
            {
                GridPalette palette = GridPalette.CreateInstance <GridPalette>();
                palette.name       = "Palette Settings";
                palette.cellSizing = GridPalette.CellSizing.Automatic;
                AssetDatabase.AddObjectToAsset(palette, defaultTemporaryPalettePath);
                AssetDatabase.ForceReserializeAssets(new string[] { defaultTemporaryPalettePath });
            }

            AssetDatabase.Refresh();

            SessionState.EraseInt(GridPaletteBrushes.s_SessionStateLastUsedBrush);
            GridPaintingState.gridBrush = GridPaletteBrushes.brushes[0];
        }
Esempio n. 11
0
        private void RefreshPalettesCache()
        {
            if (instance.m_PalettesCache == null)
            {
                instance.m_PalettesCache = new List <GameObject>();
            }

            string[] guids = AssetDatabase.FindAssets("t:GridPalette");
            foreach (string guid in guids)
            {
                string      path         = AssetDatabase.GUIDToAssetPath(guid);
                GridPalette paletteAsset = AssetDatabase.LoadAssetAtPath(path, typeof(GridPalette)) as GridPalette;
                if (paletteAsset != null)
                {
                    string     assetPath = AssetDatabase.GetAssetPath(paletteAsset);
                    GameObject palette   = AssetDatabase.LoadMainAssetAtPath(assetPath) as GameObject;
                    if (palette != null)
                    {
                        m_PalettesCache.Add(palette);
                    }
                }
            }
            m_PalettesCache.Sort((x, y) => String.Compare(x.name, y.name, StringComparison.OrdinalIgnoreCase));
        }
Esempio n. 12
0
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        TacticsTerrainMesh terrain = (TacticsTerrainMesh)target;

        GUILayout.Space(20.0f);
        if (GUILayout.Button("Rebuild"))
        {
            Rebuild(true);
        }
        Vector2Int newSize = EditorGUILayout.Vector2IntField("Size", terrain.size);

        if (newSize != terrain.size)
        {
            terrain.Resize(newSize);
            Rebuild(true);
        }
        GUILayout.Space(20.0f);
        Vector2Int flooredSelection = new Vector2Int(Mathf.FloorToInt(selectionSize.x), Mathf.FloorToInt(selectionSize.y));
        Vector2Int newSelectionSize = EditorGUILayout.Vector2IntField("Brush size", flooredSelection);

        if (newSelectionSize != flooredSelection)
        {
            selectionSize = new Vector2(Mathf.Max(1.0f, newSelectionSize.x), Mathf.Max(1.0f, newSelectionSize.y));
        }

        if (palette == null)
        {
            if (terrain.paletteName != null && terrain.paletteName.Length > 0)
            {
                string paletteName = "Assets/Tilesets/Palettes/" + terrain.paletteName + ".prefab";
                UpdateWithPalette(AssetDatabase.LoadAssetAtPath <GridPalette>(paletteName));
            }
        }
        GridPalette newPalette = (GridPalette)EditorGUILayout.ObjectField("Tileset", palette, typeof(GridPalette), false);

        if (newPalette != palette)
        {
            UpdateWithPalette(newPalette);
        }

        SelectionTool[] ordinals = new SelectionTool[] {
            SelectionTool.Select, SelectionTool.Paint, SelectionTool.HeightAdjust
        };
        string[] names          = new string[] { "Select", "Paint", "HeightAdjust" };
        int      selectionIndex = GUILayout.SelectionGrid(ArrayUtility.IndexOf(ordinals, tool), names, names.Length);

        tool = ordinals[selectionIndex];

        int       controlId      = GUIUtility.GetControlID(FocusType.Passive);
        EventType typeForControl = Event.current.GetTypeForControl(controlId);
        Vector2   mousePos       = Event.current.mousePosition;

        GUIStyle style = new GUIStyle();

        style.padding = new RectOffset(0, 0, 0, 0);

        if (tileset != null && tool == SelectionTool.Paint)
        {
            wraparoundPaintMode = EditorGUILayout.Toggle("Paint all faces", wraparoundPaintMode);
            Texture2D backer = AssetDatabase.LoadAssetAtPath <Texture2D>("Assets/Resources/Textures/White.png");
            for (int y = tileset.size.y - 1; y >= 0; y -= 1)
            {
                EditorGUILayout.BeginHorizontal();

                for (int x = 0; x < tileset.size.x; x += 1)
                {
                    Rect selectRect = EditorGUILayout.BeginHorizontal(GUILayout.Width(Map.TileSizePx), GUILayout.Height(Map.TileSizePx));
                    Tile tile       = tileset.GetTile <Tile>(new Vector3Int(x, y, 0));

                    GUILayout.Box("", style, GUILayout.Width(Map.TileSizePx), GUILayout.Height(Map.TileSizePx));
                    Rect r = GUILayoutUtility.GetLastRect();

                    if (r.Contains(Event.current.mousePosition))
                    {
                        switch (Event.current.type)
                        {
                        case EventType.MouseDown:
                            mode = EditMode.PaletteTileDrag;
                            selectedTileStart = new Vector2(x, y);
                            tileSelectRect    = new Rect(x, y, 1, 1);
                            paletteBufferSize = Vector2Int.zero;
                            break;

                        case EventType.MouseDrag:
                            if (mode == EditMode.PaletteTileDrag)
                            {
                                int minX = (int)Mathf.Min(selectedTileStart.x, x);
                                int minY = (int)Mathf.Min(selectedTileStart.y, y);
                                tileSelectRect = new Rect(minX, minY,
                                                          (int)Mathf.Max(selectedTileStart.x, x) + 1 - minX,
                                                          (int)Mathf.Max(selectedTileStart.y, y) + 1 - minY);
                                selectionSize = new Vector2(tileSelectRect.width, tileSelectRect.height);
                            }
                            break;

                        case EventType.MouseUp:
                            mode = EditMode.None;
                            break;
                        }
                    }

                    Rect rect = new Rect(tile.sprite.uv[0].x, tile.sprite.uv[3].y,
                                         tile.sprite.uv[3].x - tile.sprite.uv[0].x,
                                         tile.sprite.uv[0].y - tile.sprite.uv[3].y);

                    GUI.DrawTextureWithTexCoords(r, tile.sprite.texture, rect, true);
                    if (r.Contains(Event.current.mousePosition))
                    {
                        GUI.DrawTexture(r, backer, ScaleMode.StretchToFill, true, 0.0f, new Color(1, 0, 0, 0.5f), 0.0f, 0.0f);
                    }
                    else if (paletteBufferSize == Vector2Int.zero && tileSelectRect.Contains(new Vector2(x, y)))
                    {
                        GUI.DrawTexture(r, backer, ScaleMode.StretchToFill, true, 0.0f, new Color(1, 1, 1, 0.8f), 0.0f, 0.0f);
                    }

                    EditorGUILayout.EndHorizontal();
                }
                EditorGUILayout.EndHorizontal();
            }
        }
        if (tool == SelectionTool.Select)
        {
            EditorGUI.BeginDisabledGroup(mode != EditMode.Selected);
            if (GUILayout.Button("Create MapEvent3D"))
            {
                GameObject prefab   = AssetDatabase.LoadAssetAtPath <GameObject>(GenericPrefabPath);
                MapEvent3D mapEvent = Instantiate(prefab).GetComponent <MapEvent3D>();
                mapEvent.name = "Event" + Random.Range(1000000, 9999999);
                AddEvent(mapEvent);
            }
            if (GUILayout.Button("Create Tactics Doll"))
            {
                GameObject prefab   = AssetDatabase.LoadAssetAtPath <GameObject>(TacticsPrefabPath);
                MapEvent3D mapEvent = ((GameObject)PrefabUtility.InstantiatePrefab(prefab)).GetComponent <MapEvent3D>();
                mapEvent.name = "Doll" + Random.Range(1000000, 9999999);
                AddEvent(mapEvent);
            }
            EditorGUI.EndDisabledGroup();
        }
    }
Esempio n. 13
0
        static void CreatePalette(string tilesetName, string tilesetTileTargetDir, ImportedTile[] tileAssets, bool singleImageTileset, int tileWidth, int tileHeight, int columns, int cellWidth, int cellHeight)
        {
            GameObject newPaletteGO = new GameObject(tilesetName, typeof(Grid));

            newPaletteGO.GetComponent <Grid>().cellSize = new Vector3(1.0f, 1.0f, 0.0f);
            GameObject paletteTilemapGO = new GameObject("Layer1", typeof(Tilemap), typeof(TilemapRenderer));

            paletteTilemapGO.transform.SetParent(newPaletteGO.transform);

            paletteTilemapGO.GetComponent <TilemapRenderer>().enabled = false;

            Tilemap paletteTilemap = paletteTilemapGO.GetComponent <Tilemap>();

            paletteTilemap.tileAnchor = TiledTSXImporter.GetPivot(tileWidth, tileHeight, cellWidth, cellHeight);
            if (columns <= 0)
            {
                columns = 5;
            }

            if (singleImageTileset)
            {
                for (int i = 0; i < tileAssets.Length; i++)
                {
                    Sprite sprite = tileAssets[i].tile.sprite;
                    Rect   rect   = sprite.rect;
                    int    x      = (int)rect.x / tileWidth;
                    int    y      = (int)rect.y / tileHeight;
                    paletteTilemap.SetTile(new Vector3Int(x, y, 0), tileAssets[i].tile);
                }
            }
            else
            {
                int x = 0;
                int y = 0;
                for (int i = 0; i < tileAssets.Length; i++)
                {
                    paletteTilemap.SetTile(new Vector3Int(x, y, 0), tileAssets[i].tile);
                    x++;
                    if (x >= columns)
                    {
                        x = 0;
                        y--;
                    }
                }
            }
            string palettePath = tilesetTileTargetDir + Path.DirectorySeparatorChar + tilesetName + ".prefab";

            palettePath = palettePath.Replace('\\', '/');

            bool       createdPrefab = false;
            GameObject newPrefab     = PrefabUtility.SaveAsPrefabAsset(newPaletteGO, palettePath, out createdPrefab);

            Debug.Log("Created tile palette asset = " + createdPrefab);

            AssetDatabase.SaveAssets();

            UnityEngine.GameObject.DestroyImmediate(newPaletteGO);

            // Clear out any old subassets
            UnityEngine.Object[] assets = AssetDatabase.LoadAllAssetsAtPath(palettePath);
            for (int i = 0; i < assets.Length; i++)
            {
                UnityEngine.Object asset = assets[i];
                if (!AssetDatabase.IsMainAsset(asset) && asset is GridPalette)
                {
                    UnityEngine.Object.DestroyImmediate(asset, true);
                }
            }

            GridPalette gridPalette = ScriptableObject.CreateInstance <GridPalette>();

            gridPalette.cellSizing = GridPalette.CellSizing.Automatic;
            gridPalette.name       = "Palette Settings";
            AssetDatabase.AddObjectToAsset(gridPalette, newPrefab);
            AssetDatabase.SaveAssets();
        }