Пример #1
0
        public Rect GetSpriteUV(string name, bool textureCoords = false)
        {
            var texture = Texture as Texture2D;
            var data    = GetSpriteData(name);

            var rect = new Rect(new Vector2(data.x, data.y), new Vector2(data.width, data.height));

            if (!textureCoords)
            {
                rect = TextureUtility.ConvertToTexCoords(rect, texture.width, texture.height);
            }

            return(rect);
        }
Пример #2
0
        void OnGUI()
        {
            EditorLayoutTools.SetLabelWidth(80f);

            if (EditorAtlasPrefs.atlas == null)
            {
                EditorGUILayout.HelpBox("No Atlas selected.", MessageType.Info);
            }
            else
            {
                var atlas = EditorAtlasPrefs.atlas;

                GUILayout.Space(15f);

                GUILayout.BeginHorizontal();
                {
                    GUILayout.Space(10f);

                    var txtStyle = new GUIStyle();

                    txtStyle.fontSize         = 12;
                    txtStyle.normal.textColor = Color.yellow;

                    GUILayout.Label("Atlas : " + atlas.name, txtStyle);

                    GUILayout.FlexibleSpace();

                    GUILayout.BeginHorizontal();
                    {
                        string before = EditorAtlasPrefs.spriteSearchText;
                        string after  = EditorGUILayout.TextField(string.Empty, before, "SearchTextField", GUILayout.Width(200f));
                        if (before != after)
                        {
                            EditorAtlasPrefs.spriteSearchText = after;
                        }

                        if (GUILayout.Button(string.Empty, "SearchCancelButton", GUILayout.Width(18f)))
                        {
                            EditorAtlasPrefs.spriteSearchText = string.Empty;
                            GUIUtility.keyboardControl        = 0;
                        }
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndHorizontal();

                EditorGUILayout.Separator();

                Texture2D tex = atlas.Texture as Texture2D;

                if (tex == null)
                {
                    EditorGUILayout.HelpBox("The atlas doesn't have a texture to work with.", MessageType.Info);
                    return;
                }

                var sprites = atlas.GetListOfSprites(EditorAtlasPrefs.spriteSearchText);

                var size    = 80f;
                var padded  = size + 10f;
                var columns = Mathf.FloorToInt(Screen.width / padded);
                var offset  = 0;
                var rect    = new Rect(10f, 0, size, size);

                if (columns < 1)
                {
                    columns = 1;
                }

                GUILayout.Space(10f);

                pos = GUILayout.BeginScrollView(pos);

                int rows = 1;

                while (offset < sprites.Length)
                {
                    GUILayout.BeginHorizontal();
                    {
                        int col = 0;
                        rect.x = 10f;

                        for (; offset < sprites.Length; ++offset)
                        {
                            var sprite = atlas.GetSpriteData(sprites[offset]);

                            if (sprite == null)
                            {
                                continue;
                            }

                            if (GUI.Button(rect, ""))
                            {
                                if (Event.current.button == 0)
                                {
                                    if (EditorAtlasPrefs.spriteSearchText != sprite.name)
                                    {
                                        EditorAtlasPrefs.selectedSprite = sprite.name;
                                        AtlasTextureInspector.RepaintSprites();

                                        if (callback != null)
                                        {
                                            callback(sprite.name);
                                            Close();
                                        }
                                    }
                                }
                            }

                            if (Event.current.type == EventType.Repaint)
                            {
                                EditorLayoutTools.DrawTiledTexture(rect, EditorLayoutTools.backdropTexture);

                                var uv = new Rect(sprite.x, sprite.y, sprite.width, sprite.height);
                                uv = TextureUtility.ConvertToTexCoords(uv, tex.width, tex.height);

                                var scaleX = rect.width / uv.width;
                                var scaleY = rect.height / uv.height;

                                var aspect   = (scaleY / scaleX) / ((float)tex.height / tex.width);
                                var clipRect = rect;

                                if (aspect != 1f)
                                {
                                    if (aspect < 1f)
                                    {
                                        var padding = size * (1f - aspect) * 0.5f;
                                        clipRect.xMin += padding;
                                        clipRect.xMax -= padding;
                                    }
                                    else
                                    {
                                        var padding = size * (1f - 1f / aspect) * 0.5f;
                                        clipRect.yMin += padding;
                                        clipRect.yMax -= padding;
                                    }
                                }

                                GUI.DrawTextureWithTexCoords(clipRect, tex, uv);

                                if (EditorAtlasPrefs.selectedSprite == sprite.name)
                                {
                                    EditorLayoutTools.DrawOutline(rect, new Color(0.4f, 1f, 0f, 1f));
                                }
                            }

                            GUI.backgroundColor = new Color(1f, 1f, 1f, 0.5f);
                            GUI.contentColor    = new Color(1f, 1f, 1f, 0.7f);
                            GUI.Label(new Rect(rect.x, rect.y + rect.height, rect.width, 32f), sprite.name, "ProgressBarBack");
                            GUI.contentColor    = Color.white;
                            GUI.backgroundColor = Color.white;

                            if (++col >= columns)
                            {
                                ++offset;
                                break;
                            }
                            rect.x += padded;
                        }
                    }
                    GUILayout.EndHorizontal();
                    GUILayout.Space(padded);
                    rect.y += padded + 26;
                    ++rows;
                }

                GUILayout.Space(rows * 26);

                GUILayout.EndScrollView();
            }
        }
Пример #3
0
        public static void DrawSprite(Texture2D tex, Rect drawRect, Color color, Material mat,
                                      float x, float y, float width, float height,
                                      float borderLeft, float borderBottom, float borderRight, float borderTop,
                                      bool hasSizeLabel)
        {
            if (!tex)
            {
                return;
            }

            var outerRect = drawRect;

            outerRect.width  = width;
            outerRect.height = height;

            //----- Label Area -----

            if (hasSizeLabel)
            {
                var text = string.Format("Size: {0}x{1}", Mathf.RoundToInt(width), Mathf.RoundToInt(height));

                var labelSize = GUI.skin.label.CalcSize(new GUIContent(text));

                drawRect.height -= labelSize.y;

                var labelRect = new Rect(drawRect.x, drawRect.y + drawRect.height, drawRect.width, labelSize.y);

                EditorGUI.DropShadowLabel(labelRect, text);
            }

            //----- Texture Area -----

            if (width > 0)
            {
                float f = drawRect.width / outerRect.width;
                outerRect.width  *= f;
                outerRect.height *= f;
            }

            if (drawRect.height > outerRect.height)
            {
                outerRect.y += (drawRect.height - outerRect.height) * 0.5f;
            }
            else if (outerRect.height > drawRect.height)
            {
                float f = drawRect.height / outerRect.height;
                outerRect.width  *= f;
                outerRect.height *= f;
            }

            if (drawRect.width > outerRect.width)
            {
                outerRect.x += (drawRect.width - outerRect.width) * 0.5f;
            }

            DrawTiledTexture(outerRect, backdropTexture);

            GUI.color = color;

            if (mat == null)
            {
                var uv = new Rect(x, y, width, height);
                uv = TextureUtility.ConvertToTexCoords(uv, tex.width, tex.height);
                GUI.DrawTextureWithTexCoords(outerRect, tex, uv, true);
            }
            else
            {
                EditorGUI.DrawPreviewTexture(outerRect, tex, mat);
            }

            GUI.BeginGroup(outerRect);
            {
                tex = contrastTexture;

                GUI.color = Color.white;

                if (borderLeft > 0)
                {
                    float x0 = (float)borderLeft / width * outerRect.width - 1;
                    DrawTiledTexture(new Rect(x0, 0f, 1f, outerRect.height), tex);
                }

                if (borderRight > 0)
                {
                    float x1 = (float)(width - borderRight) / width * outerRect.width - 1;
                    DrawTiledTexture(new Rect(x1, 0f, 1f, outerRect.height), tex);
                }

                if (borderBottom > 0)
                {
                    float y0 = (float)(height - borderBottom) / height * outerRect.height - 1;
                    DrawTiledTexture(new Rect(0f, y0, outerRect.width, 1f), tex);
                }

                if (borderTop > 0)
                {
                    float y1 = (float)borderTop / height * outerRect.height - 1;
                    DrawTiledTexture(new Rect(0f, y1, outerRect.width, 1f), tex);
                }
            }
            GUI.EndGroup();

            Handles.color = Color.black;
            Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMin), new Vector3(outerRect.xMin, outerRect.yMax));
            Handles.DrawLine(new Vector3(outerRect.xMax, outerRect.yMin), new Vector3(outerRect.xMax, outerRect.yMax));
            Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMin), new Vector3(outerRect.xMax, outerRect.yMin));
            Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMax), new Vector3(outerRect.xMax, outerRect.yMax));
        }