Esempio n. 1
0
        public TileMapColliderGenerator([NotNull] TileMap tileMap, [NotNull] SpriteSheet spriteSheet)
        {
            if (tileMap == null) throw new ArgumentNullException("tileMap");
            if (spriteSheet == null) throw new ArgumentNullException("spriteSheet");

            _tileMap = tileMap;
            _spriteSheet = spriteSheet;
        }
Esempio n. 2
0
        public TileMapMeshGenerator([NotNull] TileMap tileMap, [NotNull] MeshFilter meshFilter,
            [NotNull] SpriteSheet spriteSheet)
        {
            if (tileMap == null) throw new ArgumentNullException("tileMap");
            if (meshFilter == null) throw new ArgumentNullException("meshFilter");
            if (spriteSheet == null) throw new ArgumentNullException("spriteSheet");

            _tileMap = tileMap;
            _meshFilter = meshFilter;
            _spriteSheet = spriteSheet;
        }
Esempio n. 3
0
        public void Refresh()
        {
            var renderer = GetComponent<Renderer>();
            if (renderer == null)
                return;

            var sheet = new SpriteSheet(renderer.sharedMaterial, 32);
            var cell = sheet.GetCellFromIndex(Index);
            var uv = sheet.GetUvCoordsForCell(cell);

            renderer.sharedMaterial.SetTextureScale("_MainTex", new Vector2(sheet.UnitCoordSize, sheet.UnitCoordSize));
            renderer.sharedMaterial.SetTextureScale("_BumpMap", new Vector2(sheet.UnitCoordSize, sheet.UnitCoordSize));
            renderer.sharedMaterial.SetTextureOffset("_MainTex", uv);
            renderer.sharedMaterial.SetTextureOffset("_BumpMap", uv);
        }
Esempio n. 4
0
        private void ApplyAnimationToRenderer()
        {
            if (Renderer == null)
                return;

            var frame = _currentAnimation.Frame;
            if (frame == _oldFrame)
                return;

            _oldFrame = frame;

            // TODO: cache some stuff
            var material = Renderer.material;
            var sheet = new SpriteSheet(material, 32);
            var cell = sheet.GetCellFromIndex(frame);
            var uv = sheet.GetUvCoordsForCell(cell);
            material.SetTextureOffset("_MainTex", uv);
            material.SetTextureOffset("_BumpMap", uv);
        }
Esempio n. 5
0
        void OnSceneGUI()
        {
            if (!_editing)
                return;

            var tilemap = (TileMap)target;

            var sheet = new SpriteSheet(tilemap.GetComponent<Renderer>().sharedMaterial, tilemap.SpriteSheetCellSize);

            var sourceTextureRect = new Rect(0, 0, 256, 256);
            int sourceTexCellSize = (int)(sourceTextureRect.width / sheet.Columns);

            var controlID = GUIUtility.GetControlID(FocusType.Passive);

            var screenPoint = Event.current.mousePosition;
            //var worldPoint = HandleUtility.GUIPointToWorldRay(screenPoint).origin;
            //worldPoint.z = tilemap.transform.position.z;
            var worldPoint = ScreenToWorld(new Vector2(screenPoint.x, screenPoint.y), tilemap.transform.position.z);
            var cellPoint = new Vector3((int)(worldPoint.x / tilemap.UnitSize), (int)(worldPoint.y / tilemap.UnitSize), tilemap.transform.position.z);

            bool isInSourceSelectTexture = sourceTextureRect.Contains(screenPoint);

            if (Event.current.type == EventType.mouseDown)
            {
                if (Event.current.button == 1) // right click
                {
                    if (isInSourceSelectTexture)
                    {
                        var mouseCell = Cell.Convert(screenPoint, sourceTexCellSize);
                        if (mouseCell.Col > -1 && mouseCell.Col < sheet.Columns && mouseCell.Row > -1 &&
                            mouseCell.Row < sheet.Rows)
                        {
                            var newIndex = sheet.GetIndexFromCell(mouseCell);
                            _selectedStampCell = newIndex;
                        }
                        else if (mouseCell.Row == -1 && mouseCell.Col == 0)
                        {
                            _selectedStampCell = -1;
                        }
                    }
                    else
                    {
                        // TODO: dont do this here if we actually are using the toolbox
                        TryCloneCellType(tilemap, cellPoint);
                    }
                }
                if (Event.current.button == 0)
                {
                    if (!isInSourceSelectTexture)
                    {
                        TryInsertCell(tilemap, cellPoint);
                    }
                }
            }
            if (Event.current.type == EventType.MouseDrag)
            {
                if (Event.current.button == 0)
                {
                    if (!isInSourceSelectTexture)
                    {
                        TryInsertCell(tilemap, cellPoint);
                    }
                }
            }

            {
                // TODO: check if cursor is in here, if it is, return and dont draw rest of editor
                Handles.BeginGUI();
                EditorGUI.DrawRect(sourceTextureRect, Color.magenta);
                GUI.DrawTextureWithTexCoords(sourceTextureRect, tilemap.GetComponent<Renderer>().sharedMaterial.mainTexture, new Rect(0,0,1,1), true);

                for (int i = 0; i < sheet.Columns; i++)
                {
                    for (int j = 0; j < sheet.Rows; j++)
                    {
                        int x = (i * sourceTexCellSize);
                        int y = (j * sourceTexCellSize);

                        if (sheet.GetIndexFromCell(new Cell(i, j)) == _selectedStampCell)
                        {
                            EditorGUI.DrawRect(new Rect(sourceTextureRect.x + x, sourceTextureRect.y + y, sourceTexCellSize, sourceTexCellSize), new Color(0f, 1f, 0f, 0.2f));
                        }
                        else
                        {
                            Handles.RectangleCap(controlID, new Vector3(sourceTextureRect.x + x, sourceTextureRect.y + y, 0f), Quaternion.identity, 32);
                        }
                    }
                }
                if (_selectedStampCell == -1)
                {
                    EditorGUI.DrawRect(new Rect(0, sourceTextureRect.y + sourceTextureRect.height, 32, 32), new Color(1f, 0f, 0f, 0.2f));
                }
                Handles.EndGUI();
            }

            var cell = sheet.GetCellFromIndex(_selectedStampCell);
            var uv = sheet.GetUvCoordsForCell(cell);

            Handles.BeginGUI();
            // selected tile
            GUI.DrawTextureWithTexCoords(new Rect(0, sourceTextureRect.y + sourceTextureRect.height, 32, 32), tilemap.GetComponent<Renderer>().sharedMaterial.mainTexture, new Rect(uv.x, uv.y, sheet.UnitCoordSize, sheet.UnitCoordSize));

            Handles.EndGUI();

            // selection
            if (!isInSourceSelectTexture)
            {
                float size = tilemap.UnitSize;
                var quad = new Vector3[]
                {
                    new Vector3(cellPoint.x, cellPoint.y, 0f),
                    new Vector3(cellPoint.x, cellPoint.y + size, 0f),
                    new Vector3(cellPoint.x + size, cellPoint.y + size, 0f),
                    new Vector3(cellPoint.x + size, cellPoint.y, 0f),
                };
                Handles.DrawSolidRectangleWithOutline(quad, new Color(1f, 1f, 1f, 0.2f), Color.red);
            }

            if (Event.current.type == EventType.layout)
            {
                HandleUtility.AddDefaultControl(controlID);
            }

            SceneView.RepaintAll();

            //if (GUI.changed)
            //{
            //    EditorUtility.SetDirty(target);
            //}
        }