Beispiel #1
0
        private static void CreateIsometricTilemap(GridLayout.CellLayout isometricLayout, string undoMessage)
        {
            var root       = FindOrCreateRootGrid();
            var uniqueName = GameObjectUtility.GetUniqueNameForSibling(root.transform, "Tilemap");
            var tilemapGO  = ObjectFactory.CreateGameObject(uniqueName, typeof(Tilemap), typeof(TilemapRenderer));

            tilemapGO.transform.SetParent(root.transform);
            tilemapGO.transform.position = Vector3.zero;

            var grid = root.GetComponent <Grid>();

            // Case 1071703: Do not reset cell size if adding a new Tilemap to an existing Grid of the same layout
            if (isometricLayout != grid.cellLayout)
            {
                grid.cellLayout = isometricLayout;
                grid.cellSize   = new Vector3(1.0f, 0.5f, 1.0f);
            }

            var tilemapRenderer = tilemapGO.GetComponent <TilemapRenderer>();

            tilemapRenderer.sortOrder = TilemapRenderer.SortOrder.TopRight;

            Selection.activeGameObject = tilemapGO;
            Undo.RegisterCreatedObjectUndo(tilemapGO, undoMessage);
        }
Beispiel #2
0
        /// <summary>
        /// Rotates the brush by 90 degrees in the given direction.
        /// </summary>
        /// <param name="direction">Direction to rotate by.</param>
        /// <param name="layout">Cell Layout for rotating.</param>
        public override void Rotate(RotationDirection direction, GridLayout.CellLayout layout)
        {
            Vector3Int oldSize = m_Size;

            BrushCell[] oldCells = m_Cells.Clone() as BrushCell[];
            size = new Vector3Int(oldSize.y, oldSize.x, oldSize.z);
            BoundsInt oldBounds = new BoundsInt(Vector3Int.zero, oldSize);

            foreach (Vector3Int oldPos in oldBounds.allPositionsWithin)
            {
                int newX      = direction == RotationDirection.Clockwise ? oldSize.y - oldPos.y - 1 : oldPos.y;
                int newY      = direction == RotationDirection.Clockwise ? oldPos.x : oldSize.x - oldPos.x - 1;
                int toIndex   = GetCellIndex(newX, newY, oldPos.z);
                int fromIndex = GetCellIndex(oldPos.x, oldPos.y, oldPos.z, oldSize.x, oldSize.y, oldSize.z);
                m_Cells[toIndex] = oldCells[fromIndex];
            }

            int newPivotX = direction == RotationDirection.Clockwise ? oldSize.y - pivot.y - 1 : pivot.y;
            int newPivotY = direction == RotationDirection.Clockwise ? pivot.x : oldSize.x - pivot.x - 1;

            pivot = new Vector3Int(newPivotX, newPivotY, pivot.z);

            Matrix4x4  rotation    = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, direction == RotationDirection.Clockwise ? 90f : -90f), Vector3.one);
            Quaternion orientation = Quaternion.Euler(0f, 0f, direction == RotationDirection.Clockwise ? 90f : -90f);

            foreach (BrushCell cell in m_Cells)
            {
                cell.offset      = rotation * cell.offset;
                cell.orientation = cell.orientation * orientation;
            }
        }
Beispiel #3
0
 public override void Flip(FlipAxis flip, GridLayout.CellLayout layout)
 {
     if (UnityEngine.Event.current.character == '>')
     {
         // TODO flip?
     }
 }
Beispiel #4
0
        //-------------------------------------------------------------------------/
        // Ranges
        //-------------------------------------------------------------------------/
        /// <summary>
        /// Returns the cell range given an origin
        /// </summary>
        public static Dictionary <Vector3Int, float> GetRange(Vector3Int origin,
                                                              int n,
                                                              GridLayout.CellLayout layout,
                                                              Predicate <Vector3Int> predicate)
        {
            Dictionary <Vector3Int, float> result = null;

            switch (layout)
            {
            case GridLayout.CellLayout.Rectangle:
                result = GetRangeRectangle(origin, n, predicate);
                break;

            case GridLayout.CellLayout.Hexagon:
                result = GetRangeHexOffset(origin, n, predicate);
                break;

            case GridLayout.CellLayout.Isometric:
                break;

            case GridLayout.CellLayout.IsometricZAsY:
                break;

            default:
                break;
            }
            return(result);
        }
        static void CreateAssetObject(string defaultAssetName, GridLayout.CellLayout layout, GridLayout.CellSwizzle swizzle, GridPalette.CellSizing cellSizing, Vector3 cellSize)
        {
            var assetSelectionPath = AssetDatabase.GetAssetPath(Selection.activeObject);
            var isFolder           = false;

            if (!string.IsNullOrEmpty(assetSelectionPath))
            {
                isFolder = File.GetAttributes(assetSelectionPath).HasFlag(FileAttributes.Directory);
            }
            var path = ProjectWindowUtil.GetActiveFolderPath();

            if (isFolder)
            {
                path = assetSelectionPath;
            }

            var destName = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(path, defaultAssetName));
            var icon     = EditorGUIUtility.IconContent <GameObject>().image as Texture2D;
            CreateAssetEndNameEditAction action = ScriptableObject.CreateInstance <CreateAssetEndNameEditAction>();

            action.swizzle    = swizzle;
            action.layout     = layout;
            action.cellSize   = cellSize;
            action.cellSizing = cellSizing;
            StartNewAssetNameEditingDelegate(0, action, destName, icon, "");
        }
Beispiel #6
0
        public override void Rotate(GridBrushBase.RotationDirection direction, GridLayout.CellLayout layout)
        {
            Vector3Int size = this.m_Size;

            GridBrush.BrushCell[] array = this.m_Cells.Clone() as GridBrush.BrushCell[];
            this.size = new Vector3Int(size.y, size.x, size.z);
            BoundsInt boundsInt = new BoundsInt(Vector3Int.zero, size);

            foreach (Vector3Int current in boundsInt.allPositionsWithin)
            {
                int x          = (direction != GridBrushBase.RotationDirection.Clockwise) ? current.y : (size.y - current.y - 1);
                int y          = (direction != GridBrushBase.RotationDirection.Clockwise) ? (size.x - current.x - 1) : current.x;
                int cellIndex  = this.GetCellIndex(x, y, current.z);
                int cellIndex2 = this.GetCellIndex(current.x, current.y, current.z, size.x, size.y, size.z);
                this.m_Cells[cellIndex] = array[cellIndex2];
            }
            int x2 = (direction != GridBrushBase.RotationDirection.Clockwise) ? this.pivot.y : (size.y - this.pivot.y - 1);
            int y2 = (direction != GridBrushBase.RotationDirection.Clockwise) ? (size.x - this.pivot.x - 1) : this.pivot.x;

            this.pivot = new Vector3Int(x2, y2, this.pivot.z);
            Matrix4x4 rhs = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, (direction != GridBrushBase.RotationDirection.Clockwise) ? -90f : 90f), Vector3.one);

            GridBrush.BrushCell[] cells = this.m_Cells;
            for (int i = 0; i < cells.Length; i++)
            {
                GridBrush.BrushCell brushCell = cells[i];
                Matrix4x4           matrix    = brushCell.matrix;
                brushCell.matrix = matrix * rhs;
            }
        }
Beispiel #7
0
        private static GameObject CreateNewLayer(GameObject paletteGO, string name, GridLayout.CellLayout layout)
        {
            GameObject newLayerGO = new GameObject(name);
            var        tilemap    = newLayerGO.AddComponent <Tilemap>();
            var        renderer   = newLayerGO.AddComponent <TilemapRenderer>();

            newLayerGO.transform.parent = paletteGO.transform;
            newLayerGO.layer            = paletteGO.layer;

            // Set defaults for certain layouts
            switch (layout)
            {
            case GridLayout.CellLayout.Hexagon:
            {
                tilemap.tileAnchor = Vector3.zero;
                break;
            }

            case GridLayout.CellLayout.Isometric:
            {
                renderer.sortOrder = TilemapRenderer.SortOrder.TopRight;
                break;
            }

            case GridLayout.CellLayout.IsometricZAsY:
            {
                renderer.sortOrder = TilemapRenderer.SortOrder.TopRight;
                renderer.mode      = TilemapRenderer.Mode.Individual;
                break;
            }
            }

            return(newLayerGO);
        }
Beispiel #8
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));
        }
Beispiel #9
0
    public override void Rotate(RotationDirection direction, GridLayout.CellLayout layout)
    {
        LayerTile tile = cells[0].tile as LayerTile;

        if (tile != null)
        {
            cells[0].matrix = tile.Rotate(cells[0].matrix, direction == RotationDirection.Clockwise);
        }
    }
Beispiel #10
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>
 /// <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)
 {
     return(CreateNewPalette(folderPath, name, layout, cellSizing, cellSize, swizzle,
                             TransparencySortMode.Default, defaultSortAxis));
 }
Beispiel #11
0
 public override void Flip(GridBrushBase.FlipAxis flip, GridLayout.CellLayout layout)
 {
     if (flip == GridBrushBase.FlipAxis.X)
     {
         this.FlipX();
     }
     else
     {
         this.FlipY();
     }
 }
Beispiel #12
0
 /// <summary>Flips the brush in the given axis.</summary>
 /// <param name="flip">Axis to flip by.</param>
 /// <param name="layout">Cell Layout for flipping.</param>
 public override void Flip(FlipAxis flip, GridLayout.CellLayout layout)
 {
     if (flip == FlipAxis.X)
     {
         FlipX();
     }
     else
     {
         FlipY();
     }
 }
Beispiel #13
0
 public override void Flip(FlipAxis flip, GridLayout.CellLayout layout)
 {
     base.Flip(flip, layout);
     if (flip == FlipAxis.X)
     {
         AddToPrefabRotation(Vector3.up * 180);
     }
     else if (flip == FlipAxis.Y)
     {
         AddToPrefabRotation(Vector3.forward * 180);
     }
 }
Beispiel #14
0
        public override void Rotate(RotationDirection direction, GridLayout.CellLayout layout)
        {
            if (gridBrushes == null)
            {
                return;
            }

            foreach (var gridBrush in gridBrushes)
            {
                gridBrush.Rotate(direction, layout);
            }
        }
Beispiel #15
0
        public override void Rotate(RotationDirection direction, GridLayout.CellLayout layout)
        {
            if (Event.current.character == '<')
            {
                var tile = cells[0].tile as LayerTile;

                if (tile != null)
                {
                    cells[0].matrix = tile.Rotate(cells[0].matrix, direction == RotationDirection.Clockwise);
                }
            }
        }
Beispiel #16
0
        public override void Flip(FlipAxis flip, GridLayout.CellLayout layout)
        {
            if (gridBrushes == null)
            {
                return;
            }

            foreach (var gridBrush in gridBrushes)
            {
                gridBrush.Flip(flip, layout);
            }
        }
Beispiel #17
0
 public override void Rotate(RotationDirection direction, GridLayout.CellLayout layout)
 {
     base.Rotate(direction, layout);
     if (direction == RotationDirection.Clockwise)
     {
         AddToPrefabRotation(Vector3.forward * 90f);
     }
     else if (direction == RotationDirection.CounterClockwise)
     {
         AddToPrefabRotation(Vector3.forward * -90f);
     }
 }
Beispiel #18
0
        public static GameObject CreateNewPaletteNamed(string name, GridLayout.CellLayout layout, GridPalette.CellSizing cellSizing, Vector3 cellSize, GridLayout.CellSwizzle swizzle)
        {
            string defaultPath = ProjectBrowser.s_LastInteractedProjectBrowser ? ProjectBrowser.s_LastInteractedProjectBrowser.GetActiveFolderPath() : "Assets";
            string folderPath  = EditorUtility.SaveFolderPanel("Create palette into folder ", defaultPath, "");

            folderPath = FileUtil.GetProjectRelativePath(folderPath);

            if (string.IsNullOrEmpty(folderPath))
            {
                return(null);
            }

            return(CreateNewPalette(folderPath, name, layout, cellSizing, cellSize, swizzle));
        }
        public static GameObject CreateNewLayer(GameObject paletteGO, string name, GridLayout.CellLayout layout)
        {
            GameObject gameObject = new GameObject(name);

            gameObject.AddComponent <Tilemap>();
            gameObject.AddComponent <TilemapRenderer>();
            gameObject.transform.parent = paletteGO.transform;
            gameObject.layer            = paletteGO.layer;
            if (layout == GridLayout.CellLayout.Rectangle)
            {
                paletteGO.GetComponent <Grid>().cellSize = new Vector3(1f, 1f, 0f);
            }
            return(gameObject);
        }
        public static GameObject CreateNewPaletteNamed(string name, GridLayout.CellLayout layout, GridPalette.CellSizing cellSizing, Vector3 cellSize)
        {
            string folder = (!ProjectBrowser.s_LastInteractedProjectBrowser) ? "Assets" : ProjectBrowser.s_LastInteractedProjectBrowser.GetActiveFolderPath();
            string text   = EditorUtility.SaveFolderPanel("Create palette into folder ", folder, "");

            text = FileUtil.GetProjectRelativePath(text);
            GameObject result;

            if (string.IsNullOrEmpty(text))
            {
                result = null;
            }
            else
            {
                result = GridPaletteUtility.CreateNewPalette(text, name, layout, cellSizing, cellSize);
            }
            return(result);
        }
        private void FlipY(GridLayout.CellLayout layout)
        {
            BrushCell[] oldCells  = m_Cells.Clone() as BrushCell[];
            BoundsInt   oldBounds = new BoundsInt(Vector3Int.zero, m_Size);

            foreach (Vector3Int oldPos in oldBounds.allPositionsWithin)
            {
                int newY      = m_Size.y - oldPos.y - 1;
                int toIndex   = GetCellIndex(oldPos.x, newY, oldPos.z);
                int fromIndex = GetCellIndex(oldPos);
                m_Cells[toIndex] = oldCells[fromIndex];
            }

            int newPivotY = m_Size.y - pivot.y - 1;

            pivot = new Vector3Int(pivot.x, newPivotY, pivot.z);
            FlipCells(ref m_Cells, new Vector3(1f, -1f, 1f), layout == GridLayout.CellLayout.Hexagon);
        }
        /// <summary>Rotates the brush by 90 degrees in the given direction.</summary>
        /// <param name="direction">Direction to rotate by.</param>
        /// <param name="layout">Cell Layout for rotating.</param>
        public override void Rotate(RotationDirection direction, GridLayout.CellLayout layout)
        {
            switch (layout)
            {
            case GridLayout.CellLayout.Hexagon:
                RotateHexagon(direction);
                break;

            case GridLayout.CellLayout.Isometric:
            case GridLayout.CellLayout.IsometricZAsY:
            case GridLayout.CellLayout.Rectangle:
            {
                Vector3Int  oldSize  = m_Size;
                BrushCell[] oldCells = m_Cells.Clone() as BrushCell[];
                size = new Vector3Int(oldSize.y, oldSize.x, oldSize.z);
                BoundsInt oldBounds = new BoundsInt(Vector3Int.zero, oldSize);

                foreach (Vector3Int oldPos in oldBounds.allPositionsWithin)
                {
                    int newX      = direction == RotationDirection.Clockwise ? oldPos.y : oldSize.y - oldPos.y - 1;
                    int newY      = direction == RotationDirection.Clockwise ? oldSize.x - oldPos.x - 1 : oldPos.x;
                    int toIndex   = GetCellIndex(newX, newY, oldPos.z);
                    int fromIndex = GetCellIndex(oldPos.x, oldPos.y, oldPos.z, oldSize.x, oldSize.y, oldSize.z);
                    m_Cells[toIndex] = oldCells[fromIndex];
                }

                int newPivotX = direction == RotationDirection.Clockwise ? pivot.y : oldSize.y - pivot.y - 1;
                int newPivotY = direction == RotationDirection.Clockwise ? oldSize.x - pivot.x - 1 : pivot.x;
                pivot = new Vector3Int(newPivotX, newPivotY, pivot.z);

                Matrix4x4 rotation        = direction == RotationDirection.Clockwise ? s_Clockwise : s_CounterClockwise;
                Matrix4x4 counterRotation = direction != RotationDirection.Clockwise ? s_Clockwise : s_CounterClockwise;
                foreach (BrushCell cell in m_Cells)
                {
                    Matrix4x4 oldMatrix = cell.matrix;
                    bool      counter   = (oldMatrix.lossyScale.x < 0) ^ (oldMatrix.lossyScale.y < 0);
                    cell.matrix = oldMatrix * (counter ? counterRotation : rotation);
                }
            }
            break;
            }
        }
Beispiel #23
0
        public override void Rotate(RotationDirection direction, GridLayout.CellLayout layout)
        {
            if (m_RandomRotation)
            {
                return;
            }

            switch (direction)
            {
            case RotationDirection.Clockwise:
                m_rotationStep = Mod(m_rotationStep + 1, 4);
                break;

            case RotationDirection.CounterClockwise:
                m_rotationStep = Mod(m_rotationStep - 1, 4);
                break;
            }

            base.Rotate(direction, layout);
        }
        public static GameObject CreateNewPalette(string folderPath, string name, GridLayout.CellLayout layout, GridPalette.CellSizing cellSizing, Vector3 cellSize)
        {
            GameObject gameObject = new GameObject(name);
            Grid       grid       = gameObject.AddComponent <Grid>();

            grid.cellSize   = cellSize;
            grid.cellLayout = layout;
            GridPaletteUtility.CreateNewLayer(gameObject, "Layer1", layout);
            string text = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + name + ".prefab");

            UnityEngine.Object @object     = PrefabUtility.CreateEmptyPrefab(text);
            GridPalette        gridPalette = ScriptableObject.CreateInstance <GridPalette>();

            gridPalette.name       = "Palette Settings";
            gridPalette.cellSizing = cellSizing;
            AssetDatabase.AddObjectToAsset(gridPalette, @object);
            PrefabUtility.ReplacePrefab(gameObject, @object, ReplacePrefabOptions.Default);
            AssetDatabase.Refresh();
            UnityEngine.Object.DestroyImmediate(gameObject);
            return(AssetDatabase.LoadAssetAtPath <GameObject>(text));
        }
Beispiel #25
0
        public static Vector3Int[] FindNeighboringCells(Vector3Int element, GridLayout.CellLayout layout)
        {
            Vector3Int[] result = null;
            switch (layout)
            {
            case GridLayout.CellLayout.Rectangle:
                result = FindNeighboringCellsRectangle(element);
                break;

            case GridLayout.CellLayout.Hexagon:
                result = FindNeighboringCellsHexOffset(element);
                break;

            case GridLayout.CellLayout.Isometric:
                break;

            case GridLayout.CellLayout.IsometricZAsY:
                break;
            }
            return(result);
        }
Beispiel #26
0
        //-------------------------------------------------------------------------/
        // Path
        //-------------------------------------------------------------------------/
        public static Vector3Int[] FindPath(Vector3Int origin, Vector3Int target, GridLayout.CellLayout layout,
                                            Predicate <Vector3Int> traversablePredicate = null)
        {
            Vector3Int[] result = null;
            switch (layout)
            {
            case GridLayout.CellLayout.Rectangle:
                result = FindRectanglePath(origin, target, traversablePredicate);
                break;

            case GridLayout.CellLayout.Hexagon:
                result = FindHexOffsetPath(origin, target, traversablePredicate);
                break;

            case GridLayout.CellLayout.Isometric:
                break;

            case GridLayout.CellLayout.IsometricZAsY:
                break;
            }
            return(result);
        }
Beispiel #27
0
    private void Start()
    {
        grid        = GetComponent <Grid>();
        layout      = grid.cellLayout;
        grid_spaces = new GameObject[columns, rows];

        switch (layout)
        {
        case GridLayout.CellLayout.Rectangle:
            xstep = 1.0f;
            zstep = 1.0f;
            break;

        case GridLayout.CellLayout.Hexagon:
            xstep = 1.0f;
            zstep = 0.75f;
            break;
        }

        for (int x = 0; x < columns; x++)
        {
            for (int z = 0; z < rows; z++)
            {
                grid_spaces[x, z] = Instantiate(space_prefab);
                grid_spaces[x, z].transform.parent = transform;
                switch (layout)
                {
                case GridLayout.CellLayout.Rectangle:
                    grid_spaces[x, z].transform.position = new Vector3(0.5f + x * xstep, 0, 0.5f + z * zstep);
                    break;

                case GridLayout.CellLayout.Hexagon:
                    grid_spaces[x, z].transform.position = new Vector3((z % 2 * 0.5f) + x * xstep, 0, z * zstep);
                    break;
                }
            }
        }
    }
Beispiel #28
0
        /// <summary>
        /// Converts Objects that can be laid out in the Tile Palette and organises them for placement into a given CellLayout
        /// </summary>
        /// <param name="sheetTextures">Textures containing 2-N equal sized Sprites</param>
        /// <param name="singleSprites">All the leftover Sprites that were in same texture but different sizes or just dragged in as Sprite</param>
        /// <param name="tiles">Just plain tiles</param>
        /// <param name="cellLayout">Cell Layout to place objects on</param>
        /// <returns>Dictionary mapping the positions of the Objects on the Grid Layout with details of how to place the Objects</returns>
        public static Dictionary <Vector2Int, TileDragAndDropHoverData> CreateHoverData(List <Texture2D> sheetTextures, List <Sprite> singleSprites, List <TileBase> tiles, GridLayout.CellLayout cellLayout)
        {
            Dictionary <Vector2Int, TileDragAndDropHoverData> result = new Dictionary <Vector2Int, TileDragAndDropHoverData>();

            Vector2Int currentPosition = new Vector2Int(0, 0);
            int        width           = 0;

            if (sheetTextures != null)
            {
                foreach (Texture2D sheetTexture in sheetTextures)
                {
                    Dictionary <Vector2Int, TileDragAndDropHoverData> sheet = CreateHoverData(sheetTexture, cellLayout);
                    foreach (KeyValuePair <Vector2Int, TileDragAndDropHoverData> item in sheet)
                    {
                        result.Add(item.Key + currentPosition, item.Value);
                    }
                    Vector2Int min = GetMinMaxRect(sheet.Keys.ToList()).min;
                    currentPosition += new Vector2Int(0, min.y - 1);
                }
            }
            if (currentPosition.x > 0)
            {
                currentPosition = new Vector2Int(0, currentPosition.y - 1);
            }

            if (singleSprites != null)
            {
                width = Mathf.RoundToInt(Mathf.Sqrt(singleSprites.Count));
                foreach (Sprite sprite in singleSprites)
                {
                    result.Add(currentPosition, new TileDragAndDropHoverData(sprite));
                    currentPosition += new Vector2Int(1, 0);
                    if (currentPosition.x >= width)
                    {
                        currentPosition = new Vector2Int(0, currentPosition.y - 1);
                    }
                }
            }
            if (currentPosition.x > 0)
            {
                currentPosition = new Vector2Int(0, currentPosition.y - 1);
            }

            if (tiles != null)
            {
                width = Math.Max(Mathf.RoundToInt(Mathf.Sqrt(tiles.Count)), width);
                foreach (TileBase tile in tiles)
                {
                    result.Add(currentPosition, new TileDragAndDropHoverData(tile));
                    currentPosition += new Vector2Int(1, 0);
                    if (currentPosition.x >= width)
                    {
                        currentPosition = new Vector2Int(0, currentPosition.y - 1);
                    }
                }
            }

            return(result);
        }
Beispiel #29
0
        // Organizes all the sprites in a single texture nicely on a 2D "table" based on their original texture position
        // Only call this with spritesheet with all Sprites equal size
        public static Dictionary <Vector2Int, TileDragAndDropHoverData> CreateHoverData(Texture2D sheet, GridLayout.CellLayout cellLayout)
        {
            Dictionary <Vector2Int, TileDragAndDropHoverData> result = new Dictionary <Vector2Int, TileDragAndDropHoverData>();
            List <Sprite> sprites       = GetSpritesFromTexture(sheet);
            Vector2Int    cellPixelSize = EstimateGridPixelSize(sprites);

            // Get Offset
            Vector2Int offsetSize = EstimateGridOffsetSize(sprites);

            // Get Padding
            Vector2Int paddingSize = EstimateGridPaddingSize(sprites, cellPixelSize, offsetSize);

            if ((cellLayout == GridLayout.CellLayout.Isometric ||
                 cellLayout == GridLayout.CellLayout.IsometricZAsY) &&
                (HasSpriteRectOverlaps(sprites)))
            {
                foreach (Sprite sprite in sprites)
                {
                    GetIsometricGridPosition(sprite, cellPixelSize, offsetSize, out Vector2Int position);
                    result[position] = new TileDragAndDropHoverData(sprite, Vector3.zero, (Vector2)cellPixelSize / sprite.pixelsPerUnit, false);
                }
            }
            else
            {
                foreach (Sprite sprite in sprites)
                {
                    GetGridPosition(sprite, cellPixelSize, offsetSize, paddingSize, out Vector2Int position, out Vector3 offset);
                    result[position] = new TileDragAndDropHoverData(sprite, offset, (Vector2)cellPixelSize / sprite.pixelsPerUnit);
                }
            }

            return(result);
        }
Beispiel #30
0
        ///<summary>Decode, decompress, and reorder rows of global tile IDs</summary>
        uint[] ParseGIDs(string encoding, string compression, string data, int width, GridLayout.CellLayout layout)
        {
            // Decoding
            byte[] input;
            switch (encoding)
            {
            case "base64": input = Convert.FromBase64String(data); break;

            default: throw new NotImplementedException("Encoding: " + (encoding ?? "xml"));
            }

            // Decompression
            byte[] output;
            switch (compression)
            {
            case null:   output = input;             break;

            case "gzip": output = CompressionHelper.DecompressGZip(input); break;

            case "zlib": output = CompressionHelper.DecompressZlib(input); break;

            default: throw new NotImplementedException("Compression: " + compression);
            }

            // Parse bytes as uint32 gids
            var gids = new uint[output.Length / 4];

            Buffer.BlockCopy(output, 0, gids, 0, output.Length);
            if (layout == GridLayout.CellLayout.Rectangle)
            {
                return(ArrayHelper.Reverse(gids, stride: width));
            }
            else if (layout == GridLayout.CellLayout.Isometric)
            {
                return(ArrayHelper.Swizzle(gids, stride: width).Reverse().ToArray());
            }
            else
            {
                return(gids);
            }
        }