コード例 #1
0
ファイル: TileMapRenderer.cs プロジェクト: bmotamer/tilemap
        public void Start()
        {
            _ScreenTiles = new Tile[AVERAGE_NUMBER_OF_TILES_ON_SCREEN];

            _Editor = GetComponent<TileMap>();
        }
コード例 #2
0
ファイル: TileMapCollision.cs プロジェクト: bmotamer/tilemap
        public void Build(Transform transform)
        {
            if (_TileMap == null)
                _TileMap = GetComponent<TileMap>();

            _TileMap.Awake();

            Map<Sprite> tiles = _TileMap.Data;

            List<Rect> boxes = new List<Rect>();

            for (int bottom = 0; bottom < tiles.Height; bottom++)
                for (int left = 0; left < tiles.Width; left++)
                {
                    Sprite tile = tiles[left, bottom];

                    if (tile != null)
                    {
                        bool contains;

                        int right = tiles.Width - 1, top = tiles.Height - 1;

                        for (int dx = left; dx < tiles.Width; dx++)
                        {
                            contains = false;

                            foreach (Rect box in boxes)
                                if (box.Contains(new Vector2(dx, bottom)))
                                {
                                    contains = true;
                                    break;
                                }

                            if (contains || (Array.IndexOf(CollidableTiles, tiles[dx, bottom]) == -1))
                            {
                                right = dx - 1;
                                break;
                            }

                            for (int dy = bottom + 1; dy < tiles.Height; dy++)
                            {
                                contains = false;

                                foreach (Rect box in boxes)
                                    if (box.Contains(new Vector2(dx, dy)))
                                    {
                                        contains = true;
                                        break;
                                    }

                                if (contains || ((top >= dy) && (Array.IndexOf(CollidableTiles, tiles[dx, dy]) == -1)))
                                {
                                    top = dy - 1;
                                    break;
                                }
                            }
                        }

                        int width  = right - left + 1;
                        int height = top - bottom + 1;

                        if ((width > 0) && (height > 0))
                        {
                            GameObject gameObject = new GameObject("BoxCollider2D");
                            //gameObject.hideFlags = HideFlags.HideInHierarchy;

                            BoxCollider2D boxCollider2D = gameObject.AddComponent<BoxCollider2D>();

                            boxCollider2D.size = new Vector2(width * _TileMap.UnitWidth, height * _TileMap.UnitHeight);
                            boxCollider2D.offset = 0.5f * boxCollider2D.size;

                            gameObject.transform.position = _TileMap.TileToWorld(left + _TileMap.Left, bottom + _TileMap.Bottom);
                            gameObject.transform.parent = transform;

                            gameObject.isStatic = true;

                            boxes.Add(new Rect(left, bottom, width, height));

                            //Debug.Log(new Rect(left, bottom, right - left + 1, top - bottom + 1));

                            left = right;
                        }
                    }

                }
        }
コード例 #3
0
ファイル: TileMapEditor.cs プロジェクト: bmotamer/tilemap
        private void EraseAt(TileMap tilemap, int x, int y)
        {
            int dx = x + tilemap.BrushWidth;
            int dy = y + tilemap.BrushHeight;

            for (int j = y; j < dy; j++)
                for (int i = x; i < dx; i++)
                    tilemap.EditorEraseTileAt(i, j);
        }
コード例 #4
0
ファイル: TileMapEditor.cs プロジェクト: bmotamer/tilemap
        private void SetTileDefault(TileMap tilemap, int x, int y)
        {
            if (_SelectedTile != null)
            {
                Sprite info = _SelectedTile;

                int dx = x + tilemap.BrushWidth;
                int dy = y + tilemap.BrushHeight;

                for (int j = y; j < dy; j++)
                    for (int i = x; i < dx; i++)
                        tilemap.EditorSetTileAt(i, j, info);
            }
        }
コード例 #5
0
ファイル: TileMapEditor.cs プロジェクト: bmotamer/tilemap
        private void DrawGrid(TileMap tilemap, Vector3 mousePos)
        {
            Camera camera = Camera.current;
            Rect cameraViewport = camera.rect;

            Vector2 cameraMin = camera.ViewportToWorldPoint(cameraViewport.min);
            Vector2 cameraMax = camera.ViewportToWorldPoint(cameraViewport.max);

            Vector3 tileSize = new Vector3(
                tilemap.TileWidth / tilemap.PixelsToUnits,
                tilemap.TileHeight / tilemap.PixelsToUnits
            );

            Vector3 tilemapPosition = tilemap.transform.position;

            int horz = Mathf.CeilToInt((cameraMax.x - cameraMin.x) / tileSize.x);
            int vert = Mathf.CeilToInt((cameraMax.y - cameraMin.y) / tileSize.y);

            Vector3 from = new Vector3(
                cameraMin.x - (cameraMin.x - tilemapPosition.x) % tileSize.x - tileSize.x,
                cameraMax.y,
                tilemapPosition.z
            );

            Vector3 to = from;
            to.y = cameraMin.y;

            Handles.color = new Color(1, 1, 1, 0.125f);

            for (int i = 0; i < horz; i++)
            {
                from.x += tileSize.x;
                to.x = from.x;

                Handles.DrawLine(from, to);
            }

            from = new Vector3(
                cameraMin.x,
                cameraMax.y - (cameraMax.y - tilemapPosition.y) % tileSize.y + tileSize.y,
                tilemapPosition.z
            );

            to = from;
            to.x = cameraMax.x;

            for (int i = 0; i < vert; i++)
            {
                from.y -= tileSize.y;
                to.y = from.y;

                Handles.DrawLine(from, to);
            }

            Handles.color = new Color(1, 1, 1, 0.25f);

            Vector3 min = tilemap.TileToWorld(tilemap.Left, tilemap.Bottom);
            Vector3 max = min;
            max.x += tilemap.Width * tilemap.UnitWidth;
            max.y += tilemap.Height * tilemap.UnitHeight;

            from = min;
            to   = min;
            to.x = max.x;

            Handles.DrawLine(from, to);
            Handles.DrawLine(to, max);

            from.y = max.y;

            Handles.DrawLine(from, max);
            Handles.DrawLine(from, min);

            // mouse

            Handles.color = Color.white;

            min = mousePos;
            max = mousePos;

            max.x += tilemap.BrushWidth * tilemap.UnitWidth;
            max.y += tilemap.BrushHeight * tilemap.UnitHeight;

            from = min;
            to = min;
            to.x = max.x;

            Handles.DrawLine(from, to);
            Handles.DrawLine(to, max);

            from.y = max.y;

            Handles.DrawLine(from, max);
            Handles.DrawLine(from, min);

            //Debug.Log(world);
        }