Ejemplo n.º 1
0
    /// <summary>
    /// Draw the specified sprite.
    /// </summary>

    public static void DrawTexture(Texture2D tex, Rect rect, Rect uv, Color color, Material mat)
    {
        int w = Mathf.RoundToInt(tex.width * uv.width);
        int h = Mathf.RoundToInt(tex.height * uv.height);

        // Create the texture rectangle that is centered inside rect.
        Rect outerRect = rect;

        outerRect.width  = w;
        outerRect.height = h;

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

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

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

        // Draw the background
        UGUIEditorTools.DrawTiledTexture(outerRect, UGUIEditorTools.backdropTexture);

        // Draw the sprite
        GUI.color = color;

        if (mat == null)
        {
            GUI.DrawTextureWithTexCoords(outerRect, tex, uv, true);
        }
        else
        {
            // NOTE: There is an issue in Unity that prevents it from clipping the drawn preview
            // using BeginGroup/EndGroup, and there is no way to specify a UV rect... le'suq.
            UnityEditor.EditorGUI.DrawPreviewTexture(outerRect, tex, mat);
        }
        GUI.color = Color.white;

        // Draw the lines around the sprite
        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));

        // Sprite size label
        string text = string.Format("Texture Size: {0}x{1}", w, h);

        EditorGUI.DropShadowLabel(GUILayoutUtility.GetRect(Screen.width, 18f), text);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

    void OnGUI()
    {
        EditorGUIUtility.labelWidth = 80;

        if (UGUISettings.atlas == null)
        {
            GUILayout.Label("No Atlas selected.", "LODLevelNotifyText");
        }
        else
        {
            UGUIAtlas atlas = UGUISettings.atlas;
            bool      close = false;
            GUILayout.Label(atlas.name + " Sprites", "LODLevelNotifyText");
            UGUIEditorTools.DrawSeparator();

            GUILayout.BeginHorizontal();
            GUILayout.Space(84f);

            string before = UGUISettings.partialSprite;
            string after  = EditorGUILayout.TextField("", before, "SearchTextField");
            if (before != after)
            {
                UGUISettings.partialSprite = after;
            }

            if (GUILayout.Button("", "SearchCancelButton", GUILayout.Width(18f)))
            {
                UGUISettings.partialSprite = "";
                GUIUtility.keyboardControl = 0;
            }
            GUILayout.Space(84f);
            GUILayout.EndHorizontal();

//			Texture2D tex = atlas.mainTexture as Texture2D;
//
//			if (tex == null)
//			{
//				GUILayout.Label("The atlas doesn't have a texture to work with");
//				return;
//			}

            BetterList <string> sprites = atlas.GetListOfSprites(UGUISettings.partialSprite);

            float size   = 80f;
            float padded = size + 10f;

            int columns = Mathf.FloorToInt(Screen.width / padded) / 2;
            if (columns < 1)
            {
                columns = 1;
            }
//			Debug.LogWarning (columns);

            int  offset = 0;
            Rect rect   = new Rect(10f, 0, size, size);

            GUILayout.Space(10f);
            mPos = GUILayout.BeginScrollView(mPos);
            int rows = 1;

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

                    for (; offset < sprites.size; ++offset)
                    {
                        Sprite sprite = atlas.GetSprite(sprites[offset]);
                        if (sprite == null)
                        {
                            continue;
                        }

                        // Button comes first
                        if (GUI.Button(rect, ""))
                        {
                            if (Event.current.button == 0)
                            {
                                float delta = Time.realtimeSinceStartup - mClickTime;
                                mClickTime = Time.realtimeSinceStartup;

                                if (UGUISettings.selectedSprite != sprite.name)
                                {
                                    if (mSprite != null)
                                    {
                                        UGUIEditorTools.RegisterUndo("Atlas Selection", mSprite);
                                        mSprite.SetNativeSize();
                                        EditorUtility.SetDirty(mSprite.gameObject);
                                    }

                                    UGUISettings.selectedSprite = sprite.name;
                                    UGUIEditorTools.RepaintSprites();
                                    if (mCallback != null)
                                    {
                                        mCallback(sprite.name);
                                    }
                                }
                                else if (delta < 0.5f)
                                {
                                    close = true;
                                }
                            }
                            else
                            {
//								NGUIContextMenu.AddItem("Edit", false, EditSprite, sprite);
//								NGUIContextMenu.AddItem("Delete", false, DeleteSprite, sprite);
//								NGUIContextMenu.Show();
                            }
                        }

                        if (Event.current.type == EventType.Repaint)
                        {
                            // On top of the button we have a checkboard grid
                            UGUIEditorTools.DrawTiledTexture(rect, UGUIEditorTools.backdropTexture);
//							Rect uv = new Rect(sprite.rect.x, sprite.rect.y, sprite.rect.width, sprite.rect.height);
//							uv = UGUIMath.ConvertToTexCoords(uv, tex.width, tex.height);

                            // Calculate the texture's scale that's needed to display the sprite in the clipped area
//							float scaleX = rect.width / uv.width;
//							float scaleY = rect.height / uv.height;

                            // Stretch the sprite so that it will appear proper
//							float aspect = (scaleY / scaleX) / ((float)tex.height / tex.width);
//							Rect clipRect = rect;
//
//							if (aspect != 1f)
//							{
//								if (aspect < 1f)
//								{
//									// The sprite is taller than it is wider
//									float padding = size * (1f - aspect) * 0.5f;
//									clipRect.xMin += padding;
//									clipRect.xMax -= padding;
//								}
//								else
//								{
//									// The sprite is wider than it is taller
//									float padding = size * (1f - 1f / aspect) * 0.5f;
//									clipRect.yMin += padding;
//									clipRect.yMax -= padding;
//								}
//							}

                            GUI.DrawTexture(rect, sprite.texture);

                            // Draw the selection
                            if (UGUISettings.selectedSprite == sprite.name)
                            {
                                UGUIEditorTools.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();

            if (close)
            {
                Close();
            }
        }
    }