示例#1
0
    private void LoadTilesets()
    {
        TilesetList = UnityInternal.GetAssetsInProjectOfType <Tileset>().ToArray();

        // if no tileset set yet, set
        if (TilesetList.Length > 0 && !_parent.Tileset)
        {
            SetTileSet(TilesetList[0].InstanceID);
        }

        _parent.Repaint();
    }
    public void OnGUI()
    {
        if (_parent == null)
        {
            return;
        }

        var e = Event.current;

        if (e.type == EventType.MouseMove || e.type == EventType.MouseDrag)
        {
            _parent.Repaint();
        }

        GUILayout.FlexibleSpace();

        GUILayout.BeginHorizontal();
        GUILayout.Space(20);

        GUILayout.BeginVertical();

        var selection = _parent.CurrentSelection;
        var tileset   = _parent.Tileset;

        if (e.type != EventType.Ignore)
        {
            if (selection != null && selection.Texture != null)
            {
                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();

                var scaledSize = Mathf.Max(tileset.TileSize, tileset.Collision * 20);
                var size       = Mathf.Min(256, scaledSize);
                //const int size = 256;
                var rect = GUILayoutUtility.GetRect(size, size);
                GUI.DrawTexture(rect, selection.Texture);

                var mouse = Event.current.mousePosition;

                var points  = _parent.Tileset.Collision;
                var cMatrix = new bool[selection.Collision.Length];

                Array.Copy(selection.Collision, cMatrix, selection.Collision.Length);

                int index = -1;

                if (rect.Contains(mouse))
                {
                    // Convert mouse coords to collision point
                    var offset = new Vector2
                    {
                        x = mouse.x - rect.x,
                        y = mouse.y - rect.y
                    };

                    offset  /= rect.width;
                    offset.y = 1 - offset.y;

                    offset *= points;
                    var p = new Point
                    {
                        X = Mathf.Min((int)offset.x, points - 1),
                        Y = Mathf.Min((int)offset.y, points - 1)
                    };

                    index = p.Y * points + p.X;


                    if ((e.type == EventType.MouseDown || e.type == EventType.MouseDrag) && (p != _lastPoint))
                    {
                        if (e.type == EventType.MouseDown)
                        {
                            _setType = !selection.Collision[index];
                        }

                        selection.Collision[index] = _setType;
                        _parent.Repaint();
                        _lastPoint = p;
                    }
                }

                if (e.type == EventType.MouseUp)
                {
                    _lastPoint = new Point(-1, -1);
                }

                for (var y = 0; y < points; y++)
                {
                    for (var x = 0; x < points; x++)
                    {
                        var i           = y * points + x;
                        var pDraw       = new Point(x, y);
                        var overlayRect = PointToRect(rect, pDraw);

                        if (cMatrix[i])
                        {
                            GUI.DrawTexture(overlayRect, i == index ? _highlight1 : _overlay);
                        }
                        else
                        {
                            if (i == index)
                            {
                                GUI.DrawTexture(overlayRect, _highlight2);
                            }
                        }
                    }
                }

                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();

                var id = (int)selection.ID;

                EditorGUILayout.SelectableLabel("ID: " + id);

                /*var texture = (Texture2D)EditorGUILayout.ObjectField("Texture", selection.Texture, typeof(Texture2D), false);
                 *
                 * if (texture != selection.Texture)
                 * {
                 *  var newTexture = new Texture2D(tileset.TileSize, tileset.TileSize);
                 *  var newColor = texture.GetPixels();
                 *  var color = Util.ResizeArray(newColor, tileset.TileSize, texture.width);
                 *
                 *  newTexture.SetPixels(color);
                 *  newTexture.Apply();
                 *  newTexture.name = selection.Texture.name;
                 *
                 *  DestroyImmediate(selection.Texture, true);
                 *  AssetDatabase.AddObjectToAsset(newTexture, tileset);
                 *  selection.Texture = newTexture;
                 *
                 *  AssetDatabase.SaveAssets();
                 *  AssetDatabase.Refresh();
                 * }*/

                GUILayout.BeginHorizontal();
                GUILayout.Label("Connections");

                var c = selection.Connections;
                var d = new bool[4];

                d[0] = GUILayout.Toggle((c & ConnectionMask.Left) == ConnectionMask.Left, "Left");
                d[1] = GUILayout.Toggle((c & ConnectionMask.Top) == ConnectionMask.Top, "Top");
                d[2] = GUILayout.Toggle((c & ConnectionMask.Right) == ConnectionMask.Right, "Right");
                d[3] = GUILayout.Toggle((c & ConnectionMask.Bottom) == ConnectionMask.Bottom, "Bottom");

                var all = d[0] && d[1] && d[2] && d[3];
                if (GUILayout.Toggle(all, "All"))
                {
                    for (var i = 0; i < 4; i++)
                    {
                        d[i] = true;
                    }
                }

                var none = !d[0] && !d[1] && !d[2] && !d[3];
                if (GUILayout.Toggle(none, "None"))
                {
                    for (var i = 0; i < 4; i++)
                    {
                        d[i] = false;
                    }
                }

                ConnectionMask connections = 0;
                for (var i = 0; i < 4; i++)
                {
                    connections |= (ConnectionMask)((d[i] ? 1 : 0) << i);
                }
                selection.Connections = connections;

                GUILayout.EndHorizontal();
            }
        }

        GUILayout.EndVertical();

        GUILayout.Space(20);
        GUILayout.EndHorizontal();
        GUILayout.FlexibleSpace();
    }
示例#3
0
    public void OnGUI()
    {
        if (_parent == null)
        {
            return;
        }

        GUILayout.FlexibleSpace();

        GUILayout.BeginHorizontal();
        GUILayout.Space(20);

        GUILayout.BeginVertical();

        var tileset = _parent.Tileset;

        if (tileset != null)
        {
            EditorGUILayout.BeginHorizontal(); // H2
            GUILayout.FlexibleSpace();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Texture:");
            _newTileTexture = (Texture2D)EditorGUILayout.ObjectField(_newTileTexture, typeof(Texture2D), false, GUILayout.Width(64),
                                                                     GUILayout.Height(64));
            EditorGUILayout.EndHorizontal();

            GUILayout.Space(20);

            EditorGUILayout.BeginVertical();

            GUILayout.BeginHorizontal();
            _tilesX = EditorGUILayout.IntField("Tiles X", _tilesX, GUILayout.Width(200));
            _tilesX = Mathf.Max(_tilesX, 0);
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            _tilesY = EditorGUILayout.IntField("Tiles Y", _tilesY, GUILayout.Width(200));
            _tilesY = Mathf.Max(_tilesY, 0);
            GUILayout.EndHorizontal();

            GUILayout.Space(20);

            if (_newTileTexture == null)
            {
                GUI.enabled = false;
            }

            if (GUILayout.Button("Add", GUILayout.Width(100)))
            {
                EditorUtil.SetTextureReadable(_newTileTexture);

                System.Diagnostics.Debug.Assert(_newTileTexture != null, "_newTileTexture.Texture != null");

                var pixelWidth = _newTileTexture.width;
                var pixelsX    = (float)_newTileTexture.width / _tilesX;
                var pixelsY    = (float)_newTileTexture.height / _tilesY;

                var pixels = _newTileTexture.GetPixels();

                // Loop through each tile
                for (var j = 0; j < _tilesY; j++)
                {
                    var ty0 = (int)(j * pixelsY);
                    var ty1 = (int)((j + 1) * pixelsY);

                    for (var i = 0; i < _tilesX; i++)
                    {
                        var tx0    = (int)(i * pixelsX);
                        var tx1    = (int)((i + 1) * pixelsX);
                        var width  = tx1 - tx0;
                        var height = ty1 - ty0;

                        var texture = new Texture2D(tileset.TileSize, tileset.TileSize, TextureFormat.ARGB32, false);
                        var color   = new Color[width * height];

                        for (var y = ty0; y < ty1; y++)
                        {
                            var yo = y - ty0;

                            for (var x = tx0; x < tx1; x++)
                            {
                                var xo = x - tx0;

                                color[yo * width + xo] = pixels[y * pixelWidth + x];
                            }
                        }

                        var c = Util.ResizeArray(color, tileset.TileSize, width);
                        texture.SetPixels(c);
                        texture.Apply();

                        AddAsset(texture);
                    }
                }

                _newTileTexture = null;

                _parent.Repaint();
            }



            GUI.enabled = true;
            EditorGUILayout.EndVertical();
            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal(); // H2
        }

        GUILayout.EndVertical();

        GUILayout.Space(20);
        GUILayout.EndHorizontal();
        GUILayout.FlexibleSpace();
    }