Ejemplo n.º 1
0
	/// <summary>
	/// Replacement atlas selection callback.
	/// </summary>

	void OnSelectAtlas (Object obj)
	{
		if (mReplacement != obj)
		{
			// Undo doesn't work correctly in this case... so I won't bother.
			//NGUIEditorTools.RegisterUndo("Atlas Change");
			//NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);

			mAtlas.replacement = obj as UIAtlas;
			mReplacement = mAtlas.replacement;
			NGUITools.SetDirty(mAtlas);
			if (mReplacement == null) mType = AtlasType.Normal;
		}
	}
Ejemplo n.º 2
0
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        NGUIEditorTools.SetLabelWidth(80f);
        mAtlas = target as UIAtlas;

        UISpriteData sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null;

        GUILayout.Space(6f);

        if (mAtlas.replacement != null)
        {
            mType        = AtlasType.Reference;
            mReplacement = mAtlas.replacement;
        }

        GUILayout.BeginHorizontal();
        AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType);

        NGUIEditorTools.DrawPadding();
        GUILayout.EndHorizontal();

        if (mType != after)
        {
            if (after == AtlasType.Normal)
            {
                mType = AtlasType.Normal;
                OnSelectAtlas(null);
            }
            else
            {
                mType = AtlasType.Reference;
            }
        }

        if (mType == AtlasType.Reference)
        {
            ComponentSelector.Draw <UIAtlas>(mAtlas.replacement, OnSelectAtlas, true);

            GUILayout.Space(6f);
            EditorGUILayout.HelpBox("You can have one atlas simply point to " +
                                    "another one. This is useful if you want to be " +
                                    "able to quickly replace the contents of one " +
                                    "atlas with another one, for example for " +
                                    "swapping an SD atlas with an HD one, or " +
                                    "replacing an English atlas with a Chinese " +
                                    "one. All the sprites referencing this atlas " +
                                    "will update their references to the new one.", MessageType.Info);

            if (mReplacement != mAtlas && mAtlas.replacement != mReplacement)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.replacement = mReplacement;
                NGUITools.SetDirty(mAtlas);
            }
            return;
        }

        //GUILayout.Space(6f);
        Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material;

        if (mAtlas.spriteMaterial != mat)
        {
            NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
            mAtlas.spriteMaterial = mat;

            // Ensure that this atlas has valid import settings
            if (mAtlas.texture != null)
            {
                NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
            }

            mAtlas.MarkAsChanged();
        }

        if (mat != null)
        {
            TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset;

            if (ta != null)
            {
                // Ensure that this atlas has valid import settings
                if (mAtlas.texture != null)
                {
                    NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
                }

                NGUIEditorTools.RegisterUndo("Import Sprites", mAtlas);
                NGUIJson.LoadSpriteData(mAtlas, ta);
                if (sprite != null)
                {
                    sprite = mAtlas.GetSprite(sprite.name);
                }
                mAtlas.MarkAsChanged();
            }

            float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize, GUILayout.Width(120f));

            if (pixelSize != mAtlas.pixelSize)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.pixelSize = pixelSize;
            }
        }

        if (mAtlas.spriteMaterial != null)
        {
            Color blueColor  = new Color(0f, 0.7f, 1f, 1f);
            Color greenColor = new Color(0.4f, 1f, 0f, 1f);

            if (sprite == null && mAtlas.spriteList.Count > 0)
            {
                string spriteName = NGUISettings.selectedSprite;
                if (!string.IsNullOrEmpty(spriteName))
                {
                    sprite = mAtlas.GetSprite(spriteName);
                }
                if (sprite == null)
                {
                    sprite = mAtlas.spriteList[0];
                }
            }

            if (sprite != null)
            {
                if (sprite == null)
                {
                    return;
                }

                Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D;

                if (tex != null)
                {
                    if (!NGUIEditorTools.DrawHeader("Sprite Details"))
                    {
                        return;
                    }

                    NGUIEditorTools.BeginContents();

                    GUILayout.Space(3f);
                    NGUIEditorTools.DrawAdvancedSpriteField(mAtlas, sprite.name, SelectSprite, true);
                    GUILayout.Space(6f);

                    GUI.changed = false;

                    GUI.backgroundColor = greenColor;
                    NGUIEditorTools.IntVector sizeA = NGUIEditorTools.IntPair("Dimensions", "X", "Y", sprite.x, sprite.y);
                    NGUIEditorTools.IntVector sizeB = NGUIEditorTools.IntPair(null, "Width", "Height", sprite.width, sprite.height);

                    EditorGUILayout.Separator();
                    GUI.backgroundColor = blueColor;
                    NGUIEditorTools.IntVector borderA = NGUIEditorTools.IntPair("Border", "Left", "Right", sprite.borderLeft, sprite.borderRight);
                    NGUIEditorTools.IntVector borderB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.borderBottom, sprite.borderTop);

                    EditorGUILayout.Separator();
                    GUI.backgroundColor = Color.white;
                    NGUIEditorTools.IntVector padA = NGUIEditorTools.IntPair("Padding", "Left", "Right", sprite.paddingLeft, sprite.paddingRight);
                    NGUIEditorTools.IntVector padB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.paddingBottom, sprite.paddingTop);

                    if (GUI.changed)
                    {
                        NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);

                        sprite.x      = sizeA.x;
                        sprite.y      = sizeA.y;
                        sprite.width  = sizeB.x;
                        sprite.height = sizeB.y;

                        sprite.paddingLeft   = padA.x;
                        sprite.paddingRight  = padA.y;
                        sprite.paddingBottom = padB.x;
                        sprite.paddingTop    = padB.y;

                        sprite.borderLeft   = borderA.x;
                        sprite.borderRight  = borderA.y;
                        sprite.borderBottom = borderB.x;
                        sprite.borderTop    = borderB.y;

                        MarkSpriteAsDirty();
                    }

                    GUILayout.Space(3f);

                    GUILayout.BeginHorizontal();

                    if (GUILayout.Button("Duplicate"))
                    {
                        UIAtlasMaker.SpriteEntry se = UIAtlasMaker.DuplicateSprite(mAtlas, sprite.name);
                        if (se != null)
                        {
                            NGUISettings.selectedSprite = se.name;
                        }
                    }

                    if (GUILayout.Button("Save As..."))
                    {
#if UNITY_3_5
                        string path = EditorUtility.SaveFilePanel("Save As",
                                                                  NGUISettings.currentPath, sprite.name + ".png", "png");
#else
                        string path = EditorUtility.SaveFilePanelInProject("Save As",
                                                                           sprite.name + ".png", "png",
                                                                           "Extract sprite into which file?", NGUISettings.currentPath);
#endif

                        if (!string.IsNullOrEmpty(path))
                        {
                            NGUISettings.currentPath = System.IO.Path.GetDirectoryName(path);
                            UIAtlasMaker.SpriteEntry se = UIAtlasMaker.ExtractSprite(mAtlas, sprite.name);

                            if (se != null)
                            {
                                byte[] bytes = se.tex.EncodeToPNG();
                                File.WriteAllBytes(path, bytes);
                                AssetDatabase.ImportAsset(path);
                                if (se.temporaryTexture)
                                {
                                    DestroyImmediate(se.tex);
                                }
                            }
                        }
                    }
                    GUILayout.EndHorizontal();
                    NGUIEditorTools.EndContents();
                }

                if (NGUIEditorTools.DrawHeader("Modify"))
                {
                    NGUIEditorTools.BeginContents();

                    EditorGUILayout.BeginHorizontal();
                    GUILayout.Space(20f);
                    EditorGUILayout.BeginVertical();

                    NGUISettings.backgroundColor = EditorGUILayout.ColorField("Background", NGUISettings.backgroundColor);

                    if (GUILayout.Button("Add a Shadow"))
                    {
                        AddShadow(sprite);
                    }
                    if (GUILayout.Button("Add a Soft Outline"))
                    {
                        AddOutline(sprite);
                    }

                    if (GUILayout.Button("Add a Transparent Border"))
                    {
                        AddTransparentBorder(sprite);
                    }
                    if (GUILayout.Button("Add a Clamped Border"))
                    {
                        AddClampedBorder(sprite);
                    }
                    if (GUILayout.Button("Add a Tiled Border"))
                    {
                        AddTiledBorder(sprite);
                    }
                    EditorGUI.BeginDisabledGroup(!sprite.hasBorder);
                    if (GUILayout.Button("Crop Border"))
                    {
                        CropBorder(sprite);
                    }
                    EditorGUI.EndDisabledGroup();

                    EditorGUILayout.EndVertical();
                    GUILayout.Space(20f);
                    EditorGUILayout.EndHorizontal();

                    NGUIEditorTools.EndContents();
                }

                if (NGUIEditorTools.previousSelection != null)
                {
                    GUILayout.Space(3f);
                    GUI.backgroundColor = Color.green;

                    if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name))
                    {
                        NGUIEditorTools.SelectPrevious();
                    }
                    GUI.backgroundColor = Color.white;
                }
            }
        }
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>
    public override void OnInspectorGUI()
    {
        EditorGUIUtility.LookLikeControls(80f);
        mAtlas = target as UIAtlas;

        NGUIEditorTools.DrawSeparator();

        if (mAtlas.replacement != null)
        {
            mType = AtlasType.Reference;
            mReplacement = mAtlas.replacement;
        }

        AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType);

        if (mType != after)
        {
            if (after == AtlasType.Normal)
            {
                OnSelectAtlas(null);
            }
            else
            {
                mType = AtlasType.Reference;
            }
        }

        if (mType == AtlasType.Reference)
        {
            ComponentSelector.Draw<UIAtlas>(mAtlas.replacement, OnSelectAtlas);

            NGUIEditorTools.DrawSeparator();
            GUILayout.Label("You can have one atlas simply point to\n" +
                "another one. This is useful if you want to be\n" +
                "able to quickly replace the contents of one\n" +
                "atlas with another one, for example for\n" +
                "swapping an SD atlas with an HD one, or\n" +
                "replacing an English atlas with a Chinese\n" +
                "one. All the sprites referencing this atlas\n" +
                "will update their references to the new one.");

            if (mReplacement != mAtlas && mAtlas.replacement != mReplacement)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.replacement = mReplacement;
                UnityEditor.EditorUtility.SetDirty(mAtlas);
            }
            return;
        }

        if (!mConfirmDelete)
        {
            NGUIEditorTools.DrawSeparator();
            Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material;

            if (mAtlas.spriteMaterial != mat)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.spriteMaterial = mat;

                // Ensure that this atlas has valid import settings
                if (mAtlas.texture != null) NGUIEditorTools.ImportTexture(mAtlas.texture, false, false);

                mAtlas.MarkAsDirty();
                mConfirmDelete = false;
            }

            if (mat != null)
            {
                TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset;

                if (ta != null)
                {
                    // Ensure that this atlas has valid import settings
                    if (mAtlas.texture != null) NGUIEditorTools.ImportTexture(mAtlas.texture, false, false);

                    NGUIEditorTools.RegisterUndo("Import Sprites", mAtlas);
                    NGUIJson.LoadSpriteData(mAtlas, ta);
                    if (mSprite != null) mSprite = mAtlas.GetSprite(mSprite.name);
                    mAtlas.MarkAsDirty();
                }

                UIAtlas.Coordinates coords = (UIAtlas.Coordinates)EditorGUILayout.EnumPopup("Coordinates", mAtlas.coordinates);

                if (coords != mAtlas.coordinates)
                {
                    NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                    mAtlas.coordinates = coords;
                    mConfirmDelete = false;
                }

                float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize);

                if (pixelSize != mAtlas.pixelSize)
                {
                    NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                    mAtlas.pixelSize = pixelSize;
                    mConfirmDelete = false;
                }
            }
        }

        if (mAtlas.spriteMaterial != null)
        {
            Color blue = new Color(0f, 0.7f, 1f, 1f);
            Color green = new Color(0.4f, 1f, 0f, 1f);

            if (mConfirmDelete)
            {
                if (mSprite != null)
                {
                    // Show the confirmation dialog
                    NGUIEditorTools.DrawSeparator();
                    GUILayout.Label("Are you sure you want to delete '" + mSprite.name + "'?");
                    NGUIEditorTools.DrawSeparator();

                    GUILayout.BeginHorizontal();
                    {
                        GUI.backgroundColor = Color.green;
                        if (GUILayout.Button("Cancel")) mConfirmDelete = false;
                        GUI.backgroundColor = Color.red;

                        if (GUILayout.Button("Delete"))
                        {
                            NGUIEditorTools.RegisterUndo("Delete Sprite", mAtlas);
                            mAtlas.spriteList.Remove(mSprite);
                            mConfirmDelete = false;
                        }
                        GUI.backgroundColor = Color.white;
                    }
                    GUILayout.EndHorizontal();
                }
                else mConfirmDelete = false;
            }
            else
            {
                if (mSprite == null && mAtlas.spriteList.Count > 0)
                {
                    string spriteName = EditorPrefs.GetString("NGUI Selected Sprite");
                    if (!string.IsNullOrEmpty(spriteName)) mSprite = mAtlas.GetSprite(spriteName);
                    if (mSprite == null) mSprite = mAtlas.spriteList[0];
                }

                GUI.backgroundColor = Color.green;

                GUILayout.BeginHorizontal();
                {
                    EditorGUILayout.PrefixLabel("Add/Delete");

                    if (GUILayout.Button("New Sprite"))
                    {
                        NGUIEditorTools.RegisterUndo("Add Sprite", mAtlas);
                        UIAtlas.Sprite newSprite = new UIAtlas.Sprite();

                        if (mSprite != null)
                        {
                            newSprite.name = "Copy of " + mSprite.name;
                            newSprite.outer = mSprite.outer;
                            newSprite.inner = mSprite.inner;
                        }
                        else
                        {
                            newSprite.name = "New Sprite";
                        }

                        mAtlas.spriteList.Add(newSprite);
                        mSprite = newSprite;
                    }

                    // Show the delete button
                    GUI.backgroundColor = Color.red;

                    if (mSprite != null && GUILayout.Button("Delete", GUILayout.Width(55f)))
                    {
                        mConfirmDelete = true;
                    }
                    GUI.backgroundColor = Color.white;
                }
                GUILayout.EndHorizontal();

                if (!mConfirmDelete && mSprite != null)
                {
                    NGUIEditorTools.DrawSeparator();

                    string spriteName = UISpriteInspector.SpriteField(mAtlas, mSprite.name);

                    if (spriteName != mSprite.name)
                    {
                        mSprite = mAtlas.GetSprite(spriteName);
                        EditorPrefs.SetString("NGUI Selected Sprite", spriteName);
                    }

                    if (mSprite == null) return;

                    Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D;

                    if (tex != null)
                    {
                        Rect inner = mSprite.inner;
                        Rect outer = mSprite.outer;

                        string name = EditorGUILayout.TextField("Edit Name", mSprite.name);

                        if (mSprite.name != name && !string.IsNullOrEmpty(name))
                        {
                            bool found = false;

                            foreach (UIAtlas.Sprite sp in mAtlas.spriteList)
                            {
                                if (sp.name == name)
                                {
                                    found = true;
                                    break;
                                }
                            }

                            if (!found)
                            {
                                NGUIEditorTools.RegisterUndo("Edit Sprite Name", mAtlas);
                                mSprite.name = name;
                            }
                        }

                        if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                        {
                            GUI.backgroundColor = green;
                            outer = NGUIEditorTools.IntRect("Dimensions", mSprite.outer);

                            Vector4 border = new Vector4(
                                mSprite.inner.xMin - mSprite.outer.xMin,
                                mSprite.inner.yMin - mSprite.outer.yMin,
                                mSprite.outer.xMax - mSprite.inner.xMax,
                                mSprite.outer.yMax - mSprite.inner.yMax);

                            GUI.backgroundColor = blue;
                            border = NGUIEditorTools.IntPadding("Border", border);
                            GUI.backgroundColor = Color.white;

                            inner.xMin = mSprite.outer.xMin + border.x;
                            inner.yMin = mSprite.outer.yMin + border.y;
                            inner.xMax = mSprite.outer.xMax - border.z;
                            inner.yMax = mSprite.outer.yMax - border.w;
                        }
                        else
                        {
                            // Draw the inner and outer rectangle dimensions
                            GUI.backgroundColor = green;
                            outer = EditorGUILayout.RectField("Outer Rect", mSprite.outer);
                            GUI.backgroundColor = blue;
                            inner = EditorGUILayout.RectField("Inner Rect", mSprite.inner);
                            GUI.backgroundColor = Color.white;
                        }

                        if (outer.xMax < outer.xMin) outer.xMax = outer.xMin;
                        if (outer.yMax < outer.yMin) outer.yMax = outer.yMin;

                        if (outer != mSprite.outer)
                        {
                            float x = outer.xMin - mSprite.outer.xMin;
                            float y = outer.yMin - mSprite.outer.yMin;

                            inner.x += x;
                            inner.y += y;
                        }

                        // Sanity checks to ensure that the inner rect is always inside the outer
                        inner.xMin = Mathf.Clamp(inner.xMin, outer.xMin, outer.xMax);
                        inner.xMax = Mathf.Clamp(inner.xMax, outer.xMin, outer.xMax);
                        inner.yMin = Mathf.Clamp(inner.yMin, outer.yMin, outer.yMax);
                        inner.yMax = Mathf.Clamp(inner.yMax, outer.yMin, outer.yMax);

                        EditorGUILayout.Separator();

                        // Padding is mainly meant to be used by the 'trimmed' feature of TexturePacker
                        if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                        {
                            int left	= Mathf.RoundToInt(mSprite.paddingLeft	 * mSprite.outer.width);
                            int right	= Mathf.RoundToInt(mSprite.paddingRight	 * mSprite.outer.width);
                            int top		= Mathf.RoundToInt(mSprite.paddingTop	 * mSprite.outer.height);
                            int bottom	= Mathf.RoundToInt(mSprite.paddingBottom * mSprite.outer.height);

                            NGUIEditorTools.IntVector a = NGUIEditorTools.IntPair("Padding", "Left", "Top", left, top);
                            NGUIEditorTools.IntVector b = NGUIEditorTools.IntPair(null, "Right", "Bottom", right, bottom);

                            if (a.x != left || a.y != top || b.x != right || b.y != bottom)
                            {
                                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                                mSprite.paddingLeft		= a.x / mSprite.outer.width;
                                mSprite.paddingTop		= a.y / mSprite.outer.width;
                                mSprite.paddingRight	= b.x / mSprite.outer.height;
                                mSprite.paddingBottom	= b.y / mSprite.outer.height;
                                MarkSpriteAsDirty();
                            }
                        }
                        else
                        {
                            // Create a button that can make the coordinates pixel-perfect on click
                            GUILayout.BeginHorizontal();
                            {
                                GUILayout.Label("Correction", GUILayout.Width(75f));

                                Rect corrected0 = outer;
                                Rect corrected1 = inner;

                                if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                                {
                                    corrected0 = NGUIMath.MakePixelPerfect(corrected0);
                                    corrected1 = NGUIMath.MakePixelPerfect(corrected1);
                                }
                                else
                                {
                                    corrected0 = NGUIMath.MakePixelPerfect(corrected0, tex.width, tex.height);
                                    corrected1 = NGUIMath.MakePixelPerfect(corrected1, tex.width, tex.height);
                                }

                                if (corrected0 == mSprite.outer && corrected1 == mSprite.inner)
                                {
                                    GUI.color = Color.grey;
                                    GUILayout.Button("Make Pixel-Perfect");
                                    GUI.color = Color.white;
                                }
                                else if (GUILayout.Button("Make Pixel-Perfect"))
                                {
                                    outer = corrected0;
                                    inner = corrected1;
                                    GUI.changed = true;
                                }
                            }
                            GUILayout.EndHorizontal();
                        }

                        GUILayout.BeginHorizontal();
                        {
                            mView = (View)EditorGUILayout.EnumPopup("Show", mView);
                            GUILayout.Label("Shader", GUILayout.Width(45f));

                            if (mUseShader != EditorGUILayout.Toggle(mUseShader, GUILayout.Width(20f)))
                            {
                                mUseShader = !mUseShader;

                                if (mUseShader && mView == View.Sprite)
                                {
                                    // TODO: Remove this when Unity fixes the bug with DrawPreviewTexture not being affected by BeginGroup
                                    Debug.LogWarning("There is a bug in Unity that prevents the texture from getting clipped properly.\n" +
                                        "Until it's fixed by Unity, your texture may spill onto the rest of the Unity's GUI while using this mode.");
                                }
                            }
                        }
                        GUILayout.EndHorizontal();

                        if (mSprite.outer != outer || mSprite.inner != inner)
                        {
                            NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                            mSprite.outer = outer;
                            mSprite.inner = inner;
                            mConfirmDelete = false;
                            MarkSpriteAsDirty();
                        }

                        Rect uv0 = outer;
                        Rect uv1 = inner;

                        if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                        {
                            uv0 = NGUIMath.ConvertToTexCoords(uv0, tex.width, tex.height);
                            uv1 = NGUIMath.ConvertToTexCoords(uv1, tex.width, tex.height);
                        }

                        // Draw the atlas
                        EditorGUILayout.Separator();
                        Material m = mUseShader ? mAtlas.spriteMaterial : null;
                        Rect rect = (mView == View.Atlas) ? NGUIEditorTools.DrawAtlas(tex, m) : NGUIEditorTools.DrawSprite(tex, uv0, m);

                        // Draw the sprite outline
                        NGUIEditorTools.DrawOutline(rect, uv0, uv1);
                        EditorGUILayout.Separator();
                    }
                }
            }
        }
    }
Ejemplo n.º 4
0
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        NGUIEditorTools.SetLabelWidth(80f);
        mAtlas = target as UIAtlas;

        UISpriteData sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null;

        GUILayout.Space(6f);

        if (mAtlas.replacement != null)
        {
            mType        = AtlasType.Reference;
            mReplacement = mAtlas.replacement;
        }

        GUILayout.BeginHorizontal();
        AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType);

        GUILayout.Space(18f);
        GUILayout.EndHorizontal();

        if (mType != after)
        {
            if (after == AtlasType.Normal)
            {
                mType = AtlasType.Normal;
                OnSelectAtlas(null);
            }
            else
            {
                mType = AtlasType.Reference;
            }
        }

        if (mType == AtlasType.Reference)
        {
            ComponentSelector.Draw <UIAtlas>(mAtlas.replacement, OnSelectAtlas, true);

            GUILayout.Space(6f);
            EditorGUILayout.HelpBox("You can have one atlas simply point to " +
                                    "another one. This is useful if you want to be " +
                                    "able to quickly replace the contents of one " +
                                    "atlas with another one, for example for " +
                                    "swapping an SD atlas with an HD one, or " +
                                    "replacing an English atlas with a Chinese " +
                                    "one. All the sprites referencing this atlas " +
                                    "will update their references to the new one.", MessageType.Info);

            if (mReplacement != mAtlas && mAtlas.replacement != mReplacement)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.replacement = mReplacement;
                UnityEditor.EditorUtility.SetDirty(mAtlas);
            }
            return;
        }

        //GUILayout.Space(6f);
        Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material;

        if (mAtlas.spriteMaterial != mat)
        {
            NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
            mAtlas.spriteMaterial = mat;

            // Ensure that this atlas has valid import settings
            if (mAtlas.texture != null)
            {
                NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
            }

            mAtlas.MarkAsDirty();
        }

        if (mat != null)
        {
            TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset;

            if (ta != null)
            {
                // Ensure that this atlas has valid import settings
                if (mAtlas.texture != null)
                {
                    NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
                }

                NGUIEditorTools.RegisterUndo("Import Sprites", mAtlas);
                NGUIJson.LoadSpriteData(mAtlas, ta);
                if (sprite != null)
                {
                    sprite = mAtlas.GetSprite(sprite.name);
                }
                mAtlas.MarkAsDirty();
            }

            float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize, GUILayout.Width(120f));

            if (pixelSize != mAtlas.pixelSize)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.pixelSize = pixelSize;
            }
        }

        if (mAtlas.spriteMaterial != null)
        {
            Color blue  = new Color(0f, 0.7f, 1f, 1f);
            Color green = new Color(0.4f, 1f, 0f, 1f);

            if (sprite == null && mAtlas.spriteList.Count > 0)
            {
                string spriteName = NGUISettings.selectedSprite;
                if (!string.IsNullOrEmpty(spriteName))
                {
                    sprite = mAtlas.GetSprite(spriteName);
                }
                if (sprite == null)
                {
                    sprite = mAtlas.spriteList[0];
                }
            }

            if (sprite != null)
            {
                if (sprite == null)
                {
                    return;
                }

                Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D;

                if (tex != null)
                {
                    if (!NGUIEditorTools.DrawHeader("Sprite Details"))
                    {
                        return;
                    }

                    NGUIEditorTools.BeginContents();

                    GUILayout.Space(3f);
                    NGUIEditorTools.DrawAdvancedSpriteField(mAtlas, sprite.name, SelectSprite, true);
                    GUILayout.Space(6f);

                    GUI.changed = false;

                    GUI.backgroundColor = green;
                    NGUIEditorTools.IntVector sizeA = NGUIEditorTools.IntPair("Dimensions", "X", "Y", sprite.x, sprite.y);
                    NGUIEditorTools.IntVector sizeB = NGUIEditorTools.IntPair(null, "Width", "Height", sprite.width, sprite.height);

                    EditorGUILayout.Separator();
                    GUI.backgroundColor = blue;
                    NGUIEditorTools.IntVector borderA = NGUIEditorTools.IntPair("Border", "Left", "Right", sprite.borderLeft, sprite.borderRight);
                    NGUIEditorTools.IntVector borderB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.borderBottom, sprite.borderTop);

                    EditorGUILayout.Separator();
                    GUI.backgroundColor = Color.white;
                    NGUIEditorTools.IntVector padA = NGUIEditorTools.IntPair("Padding", "Left", "Right", sprite.paddingLeft, sprite.paddingRight);
                    NGUIEditorTools.IntVector padB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.paddingBottom, sprite.paddingTop);

                    if (GUI.changed)
                    {
                        NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);

                        sprite.x      = sizeA.x;
                        sprite.y      = sizeA.y;
                        sprite.width  = sizeB.x;
                        sprite.height = sizeB.y;

                        sprite.paddingLeft   = padA.x;
                        sprite.paddingRight  = padA.y;
                        sprite.paddingBottom = padB.x;
                        sprite.paddingTop    = padB.y;

                        sprite.borderLeft   = borderA.x;
                        sprite.borderRight  = borderA.y;
                        sprite.borderBottom = borderB.x;
                        sprite.borderTop    = borderB.y;

                        MarkSpriteAsDirty();
                    }
                    NGUIEditorTools.EndContents();
                }

                if (NGUIEditorTools.previousSelection != null)
                {
                    GUI.backgroundColor = Color.green;

                    if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name))
                    {
                        NGUIEditorTools.SelectPrevious();
                    }
                    GUI.backgroundColor = Color.white;
                }
            }
        }
    }
Ejemplo n.º 5
0
 /// <summary>
 /// アトラスセット
 /// </summary>
 public static void SetAtlas(AtlasType atlasType, AtlasSpriteCache atlas)
 {
     atlasList[(int)atlasType] = atlas;
 }
Ejemplo n.º 6
0
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        NGUIEditorTools.SetLabelWidth(80f);
        mAtlas = target as UIAtlas;

        UISpriteData sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null;

        GUILayout.Space(6f);

        if (mAtlas.replacement != null)
        {
            mType        = AtlasType.Reference;
            mReplacement = mAtlas.replacement;
        }

        GUILayout.BeginHorizontal();
        AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType);

        NGUIEditorTools.DrawPadding();
        GUILayout.EndHorizontal();

        if (mType != after)
        {
            if (after == AtlasType.Normal)
            {
                mType = AtlasType.Normal;
                OnSelectAtlas(null);
            }
            else
            {
                mType = AtlasType.Reference;
            }
        }

        if (mType == AtlasType.Reference)
        {
            ComponentSelector.Draw <UIAtlas>(mAtlas.replacement, OnSelectAtlas, true);

            GUILayout.Space(6f);
            EditorGUILayout.HelpBox("You can have one atlas simply point to " +
                                    "another one. This is useful if you want to be " +
                                    "able to quickly replace the contents of one " +
                                    "atlas with another one, for example for " +
                                    "swapping an SD atlas with an HD one, or " +
                                    "replacing an English atlas with a Chinese " +
                                    "one. All the sprites referencing this atlas " +
                                    "will update their references to the new one.", MessageType.Info);

            if (mReplacement != mAtlas && mAtlas.replacement != mReplacement)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.replacement = mReplacement;
                NGUITools.SetDirty(mAtlas);
            }
            return;
        }

        //GUILayout.Space(6f);
        Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material;

        if (mAtlas.spriteMaterial != mat)
        {
            NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
            mAtlas.spriteMaterial = mat;

            // Ensure that this atlas has valid import settings
            if (mAtlas.texture != null)
            {
                NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
            }

            mAtlas.MarkAsChanged();
        }

        #region         //add by chenbin
        if (mAtlas.isBorrowSpriteMode)
        {
            GUI.color = Color.green;
        }
        else
        {
            GUI.color = Color.white;
        }
        GUILayout.Label("\n===	=========================================");
        if (GUILayout.Button("说明"))
        {
            isShowBorrowModeDesc = !isShowBorrowModeDesc;
        }
        if (isShowBorrowModeDesc)
        {
            GUILayout.Label("勾选isBorrowSpriteMode时,表明该atlas是空atlas,\n" +
                            "当有sprit对象来borrow时,atlas会根据名字从\n" +
                            "设置的路径中去取得图片资源。\n" +
                            //"其中资源必须在Resources/下,\n" +
                            "另外atlas必须要引用一个material,\n" +
                            "就算引用的是个空material也行。\n");
        }

        if (mAtlas != null && runTimes == 0)
        {
            runTimes++;
            isBorrowMode = mAtlas.isBorrowSpriteMode;
            //			isUserUnity3DType = mAtlas.useUnity3DType;
        }
        mAtlas.isBorrowSpriteMode = EditorGUILayout.Toggle("isBorrowSpriteMode", mAtlas.isBorrowSpriteMode);
        if (isBorrowMode != mAtlas.isBorrowSpriteMode)
        {
            isBorrowMode = mAtlas.isBorrowSpriteMode;
            UnityEditor.EditorUtility.SetDirty(mAtlas.gameObject);
        }
        //		if (mAtlas.isBorrowSpriteMode) {
        //			mAtlas.useUnity3DType = EditorGUILayout.Toggle ("useUnity3DType", mAtlas.useUnity3DType);
        //			if (isUserUnity3DType != mAtlas.useUnity3DType) {
        //				isUserUnity3DType = mAtlas.useUnity3DType;
        //				UnityEditor.EditorUtility.SetDirty (NGUISettings.atlas.gameObject);
        //			}
        //		}
        GUILayout.Label("============================================\n");
        GUI.color = Color.white;
        if (GUILayout.Button("Clean items if no png"))
        {
            if (EditorUtility.DisplayDialog("Alert", "Really ?", "Do it now!", "NO"))
            {
                UISpriteData        item = null;
                List <UISpriteData> list = new List <UISpriteData> ();
                for (int i = 0; i < mAtlas.spriteList.Count; i++)
                {
                    item = mAtlas.spriteList [i];
                    string path = Application.dataPath + "/" + item.path;
                    path = path.Replace("/upgradeRes/", "/upgradeRes4Dev/");
                    path = path.Replace("/upgradeRes4Publish/", "/upgradeRes4Dev/");
                    if (!File.Exists(path))
                    {
                        Debug.Log(path);
                        list.Add(item);
                    }
                }
                deleteSprites(list);
            }
        }
        #endregion                                    //end ======add by chenbin

        if (mat != null || mAtlas.isBorrowSpriteMode) // modify by chenbin
        {
            TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset;

            if (ta != null)
            {
                // Ensure that this atlas has valid import settings
                if (mAtlas.texture != null)
                {
                    NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
                }

                NGUIEditorTools.RegisterUndo("Import Sprites", mAtlas);
                NGUIJson.LoadSpriteData(mAtlas, ta);
                if (sprite != null)
                {
                    sprite = mAtlas.GetSprite(sprite.name);
                }
                mAtlas.MarkAsChanged();
            }

            float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize, GUILayout.Width(120f));

            if (pixelSize != mAtlas.pixelSize)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.pixelSize = pixelSize;
            }
        }

        if (mAtlas.spriteMaterial != null || mAtlas.isBorrowSpriteMode)                 //modify by chenbin
        {
            Color blueColor  = new Color(0f, 0.7f, 1f, 1f);
            Color greenColor = new Color(0.4f, 1f, 0f, 1f);

            if (sprite == null && mAtlas.spriteList.Count > 0)
            {
                string spriteName = NGUISettings.selectedSprite;
                if (!string.IsNullOrEmpty(spriteName))
                {
                    sprite = mAtlas.GetSprite(spriteName);
                }
//				if (sprite == null) sprite = mAtlas.spriteList[0];  // delete by chenbin
                #region add by chenbin
                if (sprite == null)
                {
                    if (mAtlas.isBorrowSpriteMode)
                    {
                        sprite     = mAtlas.spriteList [0];
                        spriteName = sprite.name;
                        sprite     = mAtlas.borrowSpriteByname(spriteName, null);
                    }
                    else
                    {
                        sprite = mAtlas.spriteList [0];
                    }
                }
                #endregion
            }

            if (sprite != null)
            {
                if (sprite == null)
                {
                    return;
                }

//				Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D;	//delete by chenbin
                #region add by chenbin
                Texture2D tex = null;
                if (mAtlas != null)
                {
                    if (mAtlas.isBorrowSpriteMode)
                    {
                        if (sprite != null && sprite.material != null)
                        {
                            tex = sprite.material.mainTexture as Texture2D;
                        }
                    }
                    else
                    {
                        tex = mAtlas.spriteMaterial.mainTexture as Texture2D;
                    }
                }
                #endregion


//				if (tex != null)		//modify by chenbin
                if (tex != null || (mAtlas != null && mAtlas.isBorrowSpriteMode && sprite != null))                  //modify by chenbin
                {
                    if (!NGUIEditorTools.DrawHeader("Sprite Details"))
                    {
                        return;
                    }

                    NGUIEditorTools.BeginContents();

                    GUILayout.Space(3f);
                    NGUIEditorTools.DrawAdvancedSpriteField(mAtlas, sprite.name, SelectSprite, true);
                    GUILayout.Space(6f);

                    GUI.changed = false;

                    GUI.backgroundColor = greenColor;
                    NGUIEditorTools.IntVector sizeA = NGUIEditorTools.IntPair("Dimensions", "X", "Y", sprite.x, sprite.y);
                    NGUIEditorTools.IntVector sizeB = NGUIEditorTools.IntPair(null, "Width", "Height", sprite.width, sprite.height);

                    EditorGUILayout.Separator();
                    GUI.backgroundColor = blueColor;
                    NGUIEditorTools.IntVector borderA = NGUIEditorTools.IntPair("Border", "Left", "Right", sprite.borderLeft, sprite.borderRight);
                    NGUIEditorTools.IntVector borderB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.borderBottom, sprite.borderTop);

                    EditorGUILayout.Separator();
                    GUI.backgroundColor = Color.white;
                    NGUIEditorTools.IntVector padA = NGUIEditorTools.IntPair("Padding", "Left", "Right", sprite.paddingLeft, sprite.paddingRight);
                    NGUIEditorTools.IntVector padB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.paddingBottom, sprite.paddingTop);

                    if (GUI.changed)
                    {
                        NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);

                        sprite.x      = sizeA.x;
                        sprite.y      = sizeA.y;
                        sprite.width  = sizeB.x;
                        sprite.height = sizeB.y;

                        sprite.paddingLeft   = padA.x;
                        sprite.paddingRight  = padA.y;
                        sprite.paddingBottom = padB.x;
                        sprite.paddingTop    = padB.y;

                        sprite.borderLeft   = borderA.x;
                        sprite.borderRight  = borderA.y;
                        sprite.borderBottom = borderB.x;
                        sprite.borderTop    = borderB.y;

                        MarkSpriteAsDirty();
                    }

                    GUILayout.Space(3f);

                    GUILayout.BeginHorizontal();

                    if (GUILayout.Button("Duplicate"))
                    {
                        string newName = UIAtlasMaker.DuplicateSprite(mAtlas, sprite.name);                         // modify by chenbin
                        if (newName != null)
                        {
                            NGUISettings.selectedSprite = newName;                                              // modify by chenbin
                        }
                    }

                    if (GUILayout.Button("Save As..."))
                    {
#if UNITY_3_5
                        string path = EditorUtility.SaveFilePanel("Save As",
                                                                  NGUISettings.currentPath, sprite.name + ".png", "png");
#else
                        string path = EditorUtility.SaveFilePanelInProject("Save As",
                                                                           sprite.name + ".png", "png",
                                                                           "Extract sprite into which file?", NGUISettings.currentPath);
#endif

                        if (!string.IsNullOrEmpty(path))
                        {
                            NGUISettings.currentPath = System.IO.Path.GetDirectoryName(path);
                            UIAtlasMaker.SpriteEntry se = UIAtlasMaker.ExtractSprite(mAtlas, sprite.name);

                            if (se != null)
                            {
                                byte[] bytes = se.tex.EncodeToPNG();
                                File.WriteAllBytes(path, bytes);
                                AssetDatabase.ImportAsset(path);
                                if (se.temporaryTexture)
                                {
                                    DestroyImmediate(se.tex);
                                }
                            }
                        }
                    }
                    GUILayout.EndHorizontal();
                    NGUIEditorTools.EndContents();
                }

                if (NGUIEditorTools.DrawHeader("Modify"))
                {
                    NGUIEditorTools.BeginContents();

                    EditorGUILayout.BeginHorizontal();
                    GUILayout.Space(20f);
                    EditorGUILayout.BeginVertical();

                    NGUISettings.backgroundColor = EditorGUILayout.ColorField("Background", NGUISettings.backgroundColor);

                    if (GUILayout.Button("Add a Shadow"))
                    {
                        AddShadow(sprite);
                    }
                    if (GUILayout.Button("Add a Soft Outline"))
                    {
                        AddOutline(sprite);
                    }

                    if (GUILayout.Button("Add a Transparent Border"))
                    {
                        AddTransparentBorder(sprite);
                    }
                    if (GUILayout.Button("Add a Clamped Border"))
                    {
                        AddClampedBorder(sprite);
                    }
                    if (GUILayout.Button("Add a Tiled Border"))
                    {
                        AddTiledBorder(sprite);
                    }
                    EditorGUI.BeginDisabledGroup(!sprite.hasBorder);
                    if (GUILayout.Button("Crop Border"))
                    {
                        CropBorder(sprite);
                    }
                    EditorGUI.EndDisabledGroup();

                    EditorGUILayout.EndVertical();
                    GUILayout.Space(20f);
                    EditorGUILayout.EndHorizontal();

                    NGUIEditorTools.EndContents();
                }

                if (NGUIEditorTools.previousSelection != null)
                {
                    GUILayout.Space(3f);
                    GUI.backgroundColor = Color.green;

                    if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name))
                    {
                        NGUIEditorTools.SelectPrevious();
                    }
                    GUI.backgroundColor = Color.white;
                }
            }
        }
    }
Ejemplo n.º 7
0
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        NGUIEditorTools.SetLabelWidth(80f);
        mAtlas = target as UIAtlas;

        UISpriteData sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null;

        GUILayout.Space(6f);

        if (mAtlas.replacement != null)
        {
            mType        = AtlasType.Reference;
            mReplacement = mAtlas.replacement;
        }

        GUILayout.BeginHorizontal();
        AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType);

        GUILayout.Space(18f);
        GUILayout.EndHorizontal();

        if (mType != after)
        {
            if (after == AtlasType.Normal)
            {
                mType = AtlasType.Normal;
                OnSelectAtlas(null);
            }
            else
            {
                mType = AtlasType.Reference;
            }
        }

        if (mType == AtlasType.Reference)
        {
            ComponentSelector.Draw <UIAtlas>(mAtlas.replacement, OnSelectAtlas, true);

            GUILayout.Space(6f);
            EditorGUILayout.HelpBox("You can have one atlas simply point to " +
                                    "another one. This is useful if you want to be " +
                                    "able to quickly replace the contents of one " +
                                    "atlas with another one, for example for " +
                                    "swapping an SD atlas with an HD one, or " +
                                    "replacing an English atlas with a Chinese " +
                                    "one. All the sprites referencing this atlas " +
                                    "will update their references to the new one.", MessageType.Info);

            if (mReplacement != mAtlas && mAtlas.replacement != mReplacement)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.replacement = mReplacement;
                NGUITools.SetDirty(mAtlas);
            }
            return;
        }

        //GUILayout.Space(6f);
        Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material;

        if (mAtlas.spriteMaterial != mat)
        {
            NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
            mAtlas.spriteMaterial = mat;

            // Ensure that this atlas has valid import settings
            if (mAtlas.texture != null)
            {
                NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
            }

            mAtlas.MarkAsChanged();
        }

        if (mat != null)
        {
            TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset;

            if (ta != null)
            {
                // Ensure that this atlas has valid import settings
                if (mAtlas.texture != null)
                {
                    NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
                }

                NGUIEditorTools.RegisterUndo("Import Sprites", mAtlas);
                NGUIJson.LoadSpriteData(mAtlas, ta);
                if (sprite != null)
                {
                    sprite = mAtlas.GetSprite(sprite.name);
                }
                mAtlas.MarkAsChanged();
            }

            float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize, GUILayout.Width(120f));

            if (pixelSize != mAtlas.pixelSize)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.pixelSize = pixelSize;
            }

            if (NGUIEditorTools.DrawHeader("Scale Factor"))
            {
                NGUIEditorTools.BeginContents();
                {
                    EditorGUILayout.HelpBox(
                        "If the image size(pixel size in photoshop) is not equal to texture size(pixel size in unity), you should set the scale factor to help NGUI to adjust uv.",
                        MessageType.Info, true
                        );

                    EditorGUILayout.HelpBox(
                        "You should click the the button below to recalculate scale factor for atlas when the atlas' texture is changed. For example, when you add sprite, remove sprite, set compressed format or change maxsize etc.",
                        MessageType.Warning, true
                        );

                    if (GUILayout.Button("Calculate Scale Factor Automatically"))
                    {
                        if (mAtlas.texture != null)
                        {
                            string path     = AssetDatabase.GetAssetPath(mAtlas.texture);
                            string fullPath = Application.dataPath + path.Substring("Assets".Length);
                            if (File.Exists(fullPath))
                            {
                                MemoryStream memoryStream = null;
                                BinaryReader binaryReader = null;
                                try
                                {
                                    byte[] bytes = File.ReadAllBytes(fullPath);
                                    memoryStream = new MemoryStream(bytes);
                                    binaryReader = new BinaryReader(memoryStream);
                                    // Is PNG
                                    if (binaryReader.ReadByte() == 0x89 && binaryReader.ReadByte() == 0x50 && binaryReader.ReadByte() == 0x4E &&
                                        binaryReader.ReadByte() == 0x47 && binaryReader.ReadByte() == 0x0D && binaryReader.ReadByte() == 0x0A &&
                                        binaryReader.ReadByte() == 0x1A && binaryReader.ReadByte() == 0x0A)
                                    {
                                        binaryReader.ReadUInt32();
                                        binaryReader.ReadUInt32();
                                        uint            width           = LittleEndianToBigEndian(binaryReader.ReadUInt32());
                                        uint            height          = LittleEndianToBigEndian(binaryReader.ReadUInt32());
                                        TextureImporter textureImporter = (TextureImporter)TextureImporter.GetAtPath(AssetDatabase.GetAssetPath(mAtlas.texture));

                                        System.Action <string, UIAtlas.SCALE_PLATFORM, BuildTarget> processScale = delegate(string platformName, UIAtlas.SCALE_PLATFORM platformAtlasEnum, BuildTarget platformEnum)
                                        {
                                            if (mAtlas.scales == null || mAtlas.scales.Length != (int)UIAtlas.SCALE_PLATFORM.SIZE * 2)
                                            {
                                                mAtlas.scales = new float[(int)UIAtlas.SCALE_PLATFORM.SIZE * 2] {
                                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1
                                                };
                                            }

                                            //      TextureImportInstructions info = new TextureImportInstructions();
                                            //      textureImporter.ReadTextureImportInstructions(info, platformEnum);
                                            int maxSize;
                                            TextureImporterFormat format;
                                            if (textureImporter.GetPlatformTextureSettings(platformName, out maxSize, out format))
                                            {
                                                float len = width > height ? width : height;
                                                mAtlas.scales[(int)platformAtlasEnum * 2]     = len / (float)width;
                                                mAtlas.scales[(int)platformAtlasEnum * 2 + 1] = len / (float)height;
                                            }
                                            else
                                            {
                                                mAtlas.scales[(int)platformAtlasEnum * 2]     = 1;
                                                mAtlas.scales[(int)platformAtlasEnum * 2 + 1] = 1;
                                            }
                                        };

                                        processScale("Web", UIAtlas.SCALE_PLATFORM.WEB, BuildTarget.WebPlayer);
                                        processScale("Standalone", UIAtlas.SCALE_PLATFORM.STANDALONE, BuildTarget.StandaloneWindows);
                                        processScale("iPhone", UIAtlas.SCALE_PLATFORM.IPHONE, BuildTarget.iOS);
                                        processScale("Android", UIAtlas.SCALE_PLATFORM.ANDROID, BuildTarget.Android);

                                        NGUITools.SetDirty(mAtlas);
                                    }
                                }
                                catch (System.Exception exception)
                                {
                                    Debug.LogError(exception.StackTrace);
                                    UnityEditor.EditorUtility.DisplayDialog("Error", "Error:Set the scale factor mamually.", "ok");
                                }
                                finally
                                {
                                    if (memoryStream != null)
                                    {
                                        memoryStream.Close();
                                        memoryStream.Dispose();
                                        memoryStream = null;
                                    }
                                    if (binaryReader != null)
                                    {
                                        binaryReader.Close();
                                        binaryReader = null;
                                    }
                                }
                            }
                            else
                            {
                                UnityEditor.EditorUtility.DisplayDialog("Alert", "File is not exists\n" + fullPath, "ok");
                            }
                        }
                        else
                        {
                            UnityEditor.EditorUtility.DisplayDialog("Alert", "Null Texture", "ok");
                        }
                    }
                }
                NGUIEditorTools.EndContents();
            }
        }

        if (mAtlas.spriteMaterial != null)
        {
            Color blueColor  = new Color(0f, 0.7f, 1f, 1f);
            Color greenColor = new Color(0.4f, 1f, 0f, 1f);

            if (sprite == null && mAtlas.spriteList.Count > 0)
            {
                string spriteName = NGUISettings.selectedSprite;
                if (!string.IsNullOrEmpty(spriteName))
                {
                    sprite = mAtlas.GetSprite(spriteName);
                }
                if (sprite == null)
                {
                    sprite = mAtlas.spriteList[0];
                }
            }

            if (sprite != null)
            {
                if (sprite == null)
                {
                    return;
                }

                Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D;

                if (tex != null)
                {
                    if (!NGUIEditorTools.DrawHeader("Sprite Details"))
                    {
                        return;
                    }

                    NGUIEditorTools.BeginContents();

                    GUILayout.Space(3f);
                    NGUIEditorTools.DrawAdvancedSpriteField(mAtlas, sprite.name, SelectSprite, true);
                    GUILayout.Space(6f);

                    GUI.changed = false;

                    GUI.backgroundColor = greenColor;
                    NGUIEditorTools.IntVector sizeA = NGUIEditorTools.IntPair("Dimensions", "X", "Y", sprite.x, sprite.y);
                    NGUIEditorTools.IntVector sizeB = NGUIEditorTools.IntPair(null, "Width", "Height", sprite.width, sprite.height);

                    EditorGUILayout.Separator();
                    GUI.backgroundColor = blueColor;
                    NGUIEditorTools.IntVector borderA = NGUIEditorTools.IntPair("Border", "Left", "Right", sprite.borderLeft, sprite.borderRight);
                    NGUIEditorTools.IntVector borderB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.borderBottom, sprite.borderTop);

                    EditorGUILayout.Separator();
                    GUI.backgroundColor = Color.white;
                    NGUIEditorTools.IntVector padA = NGUIEditorTools.IntPair("Padding", "Left", "Right", sprite.paddingLeft, sprite.paddingRight);
                    NGUIEditorTools.IntVector padB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.paddingBottom, sprite.paddingTop);

                    if (GUI.changed)
                    {
                        NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);

                        sprite.x      = sizeA.x;
                        sprite.y      = sizeA.y;
                        sprite.width  = sizeB.x;
                        sprite.height = sizeB.y;

                        sprite.paddingLeft   = padA.x;
                        sprite.paddingRight  = padA.y;
                        sprite.paddingBottom = padB.x;
                        sprite.paddingTop    = padB.y;

                        sprite.borderLeft   = borderA.x;
                        sprite.borderRight  = borderA.y;
                        sprite.borderBottom = borderB.x;
                        sprite.borderTop    = borderB.y;

                        MarkSpriteAsDirty();
                    }

                    GUILayout.Space(3f);

                    GUILayout.BeginHorizontal();

                    if (GUILayout.Button("Duplicate"))
                    {
                        UIAtlasMaker.SpriteEntry se = UIAtlasMaker.DuplicateSprite(mAtlas, sprite.name);
                        if (se != null)
                        {
                            NGUISettings.selectedSprite = se.name;
                        }
                    }

                    if (GUILayout.Button("Save As..."))
                    {
                        string path = EditorUtility.SaveFilePanelInProject("Save As", sprite.name + ".png", "png", "Extract sprite into which file?");

                        if (!string.IsNullOrEmpty(path))
                        {
                            UIAtlasMaker.SpriteEntry se = UIAtlasMaker.ExtractSprite(mAtlas, sprite.name);

                            if (se != null)
                            {
                                byte[] bytes = se.tex.EncodeToPNG();
                                File.WriteAllBytes(path, bytes);
                                AssetDatabase.ImportAsset(path);
                                if (se.temporaryTexture)
                                {
                                    DestroyImmediate(se.tex);
                                }
                            }
                        }
                    }
                    GUILayout.EndHorizontal();
                    NGUIEditorTools.EndContents();
                }

                if (NGUIEditorTools.DrawHeader("Modify"))
                {
                    NGUIEditorTools.BeginContents();

                    EditorGUILayout.BeginHorizontal();
                    GUILayout.Space(20f);
                    EditorGUILayout.BeginVertical();

                    NGUISettings.backgroundColor = EditorGUILayout.ColorField("Background", NGUISettings.backgroundColor);

                    if (GUILayout.Button("Add a Shadow"))
                    {
                        AddShadow(sprite);
                    }
                    if (GUILayout.Button("Add a Soft Outline"))
                    {
                        AddOutline(sprite);
                    }

                    if (GUILayout.Button("Add a Transparent Border"))
                    {
                        AddTransparentBorder(sprite);
                    }
                    if (GUILayout.Button("Add a Clamped Border"))
                    {
                        AddClampedBorder(sprite);
                    }
                    if (GUILayout.Button("Add a Tiled Border"))
                    {
                        AddTiledBorder(sprite);
                    }
                    EditorGUI.BeginDisabledGroup(!sprite.hasBorder);
                    if (GUILayout.Button("Crop Border"))
                    {
                        CropBorder(sprite);
                    }
                    EditorGUI.EndDisabledGroup();

                    EditorGUILayout.EndVertical();
                    GUILayout.Space(20f);
                    EditorGUILayout.EndHorizontal();

                    NGUIEditorTools.EndContents();
                }

                if (NGUIEditorTools.previousSelection != null)
                {
                    GUILayout.Space(3f);
                    GUI.backgroundColor = Color.green;

                    if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name))
                    {
                        NGUIEditorTools.SelectPrevious();
                    }
                    GUI.backgroundColor = Color.white;
                }
            }
        }
    }
Ejemplo n.º 8
0
    private static UIAtlas CreateAtlasInternal(string atlasName, Device device, AtlasType atlasType, out List <UISpriteData> oldSpriteData)
    {
        oldSpriteData = new List <UISpriteData>();

#if RSATLASHELPER_DEBUG
        Debug.Log(string.Format("Create atlas; atlasName = {0}, device = {1}, atlasType = {2}", atlasName, device, atlasType));
#endif
        string resourceDir = GetAtlasResourceFolder(device, atlasType);

        string newAtlasName     = GetAtlasName(atlasName, device, atlasType);
        string newAtlas         = string.Format("Assets{0}{1}.prefab", resourceDir, newAtlasName);
        string newAtlasMaterial = string.Format("Assets{0}{1}.mat", resourceDir, newAtlasName);

        DirectoryInfo dirInfo = new DirectoryInfo("Assets" + resourceDir);
        if (!dirInfo.Exists)
        {
#if RSATLASHELPER_DEBUG
            Debug.Log("create directory: " + resourceDir);
#endif
            dirInfo.Create();
        }


#if RSATLASHELPER_DEBUG
        Debug.Log("creating atlas assets: " + newAtlas);
#endif
        Material mat = null;
        if (atlasType != AtlasType.Ref)
        {
            mat = AssetDatabase.LoadAssetAtPath(newAtlasMaterial, typeof(Material)) as Material;
            if (mat == null)
            {
                Shader shader = Shader.Find("Unlit/Transparent Colored");
                if (shader != null)
                {
                    mat = new Material(shader);

                    AssetDatabase.CreateAsset(mat, newAtlasMaterial);
                    AssetDatabase.SaveAssets();

                    mat = AssetDatabase.LoadAssetAtPath(newAtlasMaterial, typeof(Material)) as Material;
                }
            }

            if (mat == null)
            {
                Debug.LogWarning("Error; Could not create material for atlas = " + atlasName);
                return(null);
            }
        }

        bool   saveSpriteData = true;
        Object atlasPrefab    = AssetDatabase.LoadAssetAtPath(newAtlas, typeof(GameObject));
        if (atlasPrefab == null)
        {
            atlasPrefab    = PrefabUtility.CreateEmptyPrefab(newAtlas);
            saveSpriteData = false;
        }

        if (atlasPrefab == null)
        {
            Debug.LogWarning("Error; Could not create atlas prefab for atlas = " + atlasName);
            return(null);
        }

        GameObject go = new GameObject(atlasName);

        UIAtlas atlas = go.AddComponent <UIAtlas>();
        if (atlasType != AtlasType.Ref)
        {
            atlas.spriteMaterial = mat;
        }

        if (saveSpriteData)
        {
            GameObject goOldAtlas = (GameObject)Instantiate(atlasPrefab);
            UIAtlas    oldAtlas   = goOldAtlas.GetComponent <UIAtlas>();
            GetAtlasSpriteData(ref oldAtlas, ref oldSpriteData);
            oldAtlas = null;
            DestroyImmediate(goOldAtlas);
        }

        PrefabUtility.ReplacePrefab(go, atlasPrefab, ReplacePrefabOptions.ReplaceNameBased);

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();

        GameObject go2 = AssetDatabase.LoadAssetAtPath(newAtlas, typeof(GameObject)) as GameObject;

        DestroyImmediate(go);
        EditorUtility.UnloadUnusedAssetsImmediate();

        UIAtlas ret = go2.GetComponent <UIAtlas>();

#if RSATLASHELPER_DEBUG
        Debug.Log("Finished creating atlas = " + ret);
#endif
        return(ret);
    }
Ejemplo n.º 9
0
    public static UIAtlas CreateAtlas(string atlasName, Device device, AtlasType atlasType)
    {
        List <UISpriteData> oldSpriteData;

#if RSATLASHELPER_DEBUG
        Debug.Log(string.Format("AddNewAtlas; atlasName = {0}, device = {1}, atlasType = {2}", atlasName, device, atlasType));
#endif

        string texturesDir = GetAtlasTexturesDirectory(atlasName, device, atlasType);

        DirectoryInfo dirInfo = new DirectoryInfo("Assets/" + texturesDir);
        if (dirInfo == null || !dirInfo.Exists)
        {
            Debug.LogWarning("Directory does not exist; " + atlasName + ", for device: " + device + " " + texturesDir);
            return(null);
        }
        List <FileInfo> fis;
        GetTextureAssets(dirInfo, out fis);

        if (fis == null && fis.Count == 0)
        {
            Debug.LogWarning("Directory empty; " + atlasName + ", for device: " + device);
            return(null);
        }

        UIAtlas newAtlas = CreateAtlasInternal(atlasName, device, atlasType, out oldSpriteData);
        if (newAtlas == null)
        {
            Debug.LogWarning("Could not create atlas for " + atlasName + ", device = " + device);
            return(null);
        }

        NGUISettings.atlas            = newAtlas;
        NGUISettings.atlasPadding     = 2;
        NGUISettings.atlasTrimming    = false;
        NGUISettings.forceSquareAtlas = true;
        NGUISettings.allow4096        = (atlasType == AtlasType.HD);
        NGUISettings.fontTexture      = null;
        NGUISettings.unityPacking     = false;

        if (device == Device.Tablet || device == Device.Standalone)
        {
            newAtlas.pixelSize = (atlasType == AtlasType.HD) ? 1f : 2f;
        }
        else
        {
            newAtlas.pixelSize = 1f;
        }

        EditorUtility.SetDirty(newAtlas.gameObject);

        List <Texture> textures = new List <Texture>();

        foreach (FileInfo fi in fis)
        {
            string textureName = "Assets" + texturesDir + fi.Name;

            Texture2D tex = AssetDatabase.LoadAssetAtPath(textureName, typeof(Texture2D)) as Texture2D;

            TextureImporter texImporter = AssetImporter.GetAtPath(textureName) as TextureImporter;
            texImporter.textureType         = TextureImporterType.Default;
            texImporter.npotScale           = TextureImporterNPOTScale.None;
            texImporter.generateCubemap     = TextureImporterGenerateCubemap.None;
            texImporter.normalmap           = false;
            texImporter.linearTexture       = true;
            texImporter.alphaIsTransparency = true;
            texImporter.convertToNormalmap  = false;
            texImporter.grayscaleToAlpha    = false;
            texImporter.lightmap            = false;
            texImporter.npotScale           = TextureImporterNPOTScale.None;
            texImporter.filterMode          = FilterMode.Point;
            texImporter.wrapMode            = TextureWrapMode.Clamp;
            texImporter.maxTextureSize      = 4096;
            texImporter.mipmapEnabled       = false;
            texImporter.textureFormat       = TextureImporterFormat.AutomaticTruecolor;
            AssetDatabase.ImportAsset(textureName, ImportAssetOptions.ForceUpdate);

            tex.filterMode = FilterMode.Bilinear;
            tex.wrapMode   = TextureWrapMode.Clamp;

            textures.Add(tex);

#if RSATLASHELPER_DEBUG
            Debug.Log("- added tex: " + textureName);
#endif
        }

        List <UIAtlasMaker.SpriteEntry> sprites = UIAtlasMaker.CreateSprites(textures);
        UIAtlasMaker.ExtractSprites(newAtlas, sprites);
        UIAtlasMaker.UpdateAtlas(newAtlas, sprites);
        AssetDatabase.SaveAssets();

        {
            string resourceDir = GetAtlasResourceFolder(device, atlasType);
            string atlasName2  = GetAtlasName(atlasName, device, atlasType);
            string newTex      = "Assets" + resourceDir + atlasName2 + ".png";

            TextureImporter texImporter    = AssetImporter.GetAtPath(newTex) as TextureImporter;
            int             maxTextureSize = atlasType == AtlasType.HD ? 4096 : 2048;
            texImporter.maxTextureSize = maxTextureSize;
            texImporter.textureFormat  = TextureImporterFormat.AutomaticCompressed;
            if (device == Device.Phone)
            {
                texImporter.mipmapEnabled = true;
                texImporter.borderMipmap  = true;
                texImporter.mipmapFilter  = TextureImporterMipFilter.BoxFilter;
            }
            else
            {
                texImporter.mipmapEnabled = false;
            }
            texImporter.alphaIsTransparency = true;
            texImporter.filterMode          = FilterMode.Bilinear;
            texImporter.wrapMode            = TextureWrapMode.Clamp;
            texImporter.anisoLevel          = 1;
            texImporter.SetPlatformTextureSettings("iPhone", maxTextureSize, TextureImporterFormat.PVRTC_RGBA4);
            texImporter.SetPlatformTextureSettings("Android", maxTextureSize, TextureImporterFormat.PVRTC_RGBA4);
            AssetDatabase.ImportAsset(newTex, ImportAssetOptions.ForceUpdate);
        }

        UpdateAtlasSpriteData(ref newAtlas, ref oldSpriteData);

#if RSATLASHELPER_DEBUG
        DebugAtlasSpriteData(ref newAtlas);
#endif

        newAtlas.MarkAsChanged();
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();

        return(newAtlas);
    }
Ejemplo n.º 10
0
 private string GetPath(AtlasType type)
 {
     return(UIAtlasUtility.ConstAtlasPath[(int)type]);
 }
Ejemplo n.º 11
0
    /// <summary>
    /// Replacement atlas selection callback.
    /// </summary>
    void OnSelectAtlas(MonoBehaviour obj)
    {
        if (mReplacement != obj)
        {
            mReplacement = obj as UIAtlas;

            // Undo doesn't work correctly in this case... so I won't bother.
            //NGUIEditorTools.RegisterUndo("Atlas Change");
            //NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
            mAtlas.replacement = mReplacement;
            UnityEditor.EditorUtility.SetDirty(mAtlas);
            if (mReplacement == null) mType = AtlasType.Normal;
        }
    }
Ejemplo n.º 12
0
        public void ReleaseAtlas(AtlasType type)
        {
            string path = GetPath(type);

            ReleaseAtlas(path);
        }
Ejemplo n.º 13
0
        public UIAtlas LoadAtlas(AtlasType type)
        {
            string path = GetPath(type);

            return(LoadAtlas(path));
        }
Ejemplo n.º 14
0
 public void PreLoadAtlas(AtlasType type)
 {
     LoadAtlas(type);
 }
Ejemplo n.º 15
0
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        EditorGUIUtility.LookLikeControls(80f);
        mAtlas = target as UIAtlas;

        UIAtlas.Sprite sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null;

        NGUIEditorTools.DrawSeparator();

        if (mAtlas.replacement != null)
        {
            mType        = AtlasType.Reference;
            mReplacement = mAtlas.replacement;
        }

        AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType);

        if (mType != after)
        {
            if (after == AtlasType.Normal)
            {
                OnSelectAtlas(null);
            }
            else
            {
                mType = AtlasType.Reference;
            }
        }

        if (mType == AtlasType.Reference)
        {
            ComponentSelector.Draw <UIAtlas>(mAtlas.replacement, OnSelectAtlas);

            NGUIEditorTools.DrawSeparator();
            EditorGUILayout.HelpBox("You can have one atlas simply point to " +
                                    "another one. This is useful if you want to be " +
                                    "able to quickly replace the contents of one " +
                                    "atlas with another one, for example for " +
                                    "swapping an SD atlas with an HD one, or " +
                                    "replacing an English atlas with a Chinese " +
                                    "one. All the sprites referencing this atlas " +
                                    "will update their references to the new one.", MessageType.Info);

            if (mReplacement != mAtlas && mAtlas.replacement != mReplacement)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.replacement = mReplacement;
                UnityEditor.EditorUtility.SetDirty(mAtlas);
            }
            return;
        }

        if (!mConfirmDelete)
        {
            NGUIEditorTools.DrawSeparator();
            Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material;

            if (mAtlas.spriteMaterial != mat)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.spriteMaterial = mat;

                // Ensure that this atlas has valid import settings
                if (mAtlas.texture != null)
                {
                    NGUIEditorTools.ImportTexture(mAtlas.texture, false, false);
                }

                mAtlas.MarkAsDirty();
                mConfirmDelete = false;
            }

            if (mat != null)
            {
                TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset;

                if (ta != null)
                {
                    // Ensure that this atlas has valid import settings
                    if (mAtlas.texture != null)
                    {
                        NGUIEditorTools.ImportTexture(mAtlas.texture, false, false);
                    }

                    NGUIEditorTools.RegisterUndo("Import Sprites", mAtlas);
                    NGUIJson.LoadSpriteData(mAtlas, ta);
                    if (sprite != null)
                    {
                        sprite = mAtlas.GetSprite(sprite.name);
                    }
                    mAtlas.MarkAsDirty();
                }

                UIAtlas.Coordinates coords = (UIAtlas.Coordinates)EditorGUILayout.EnumPopup("Coordinates", mAtlas.coordinates);

                if (coords != mAtlas.coordinates)
                {
                    NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                    mAtlas.coordinates = coords;
                    mConfirmDelete     = false;
                }

                float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize, GUILayout.Width(120f));

                if (pixelSize != mAtlas.pixelSize)
                {
                    NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                    mAtlas.pixelSize = pixelSize;
                    mConfirmDelete   = false;
                }
            }
        }

        if (mAtlas.spriteMaterial != null)
        {
            Color blue  = new Color(0f, 0.7f, 1f, 1f);
            Color green = new Color(0.4f, 1f, 0f, 1f);

            if (mConfirmDelete)
            {
                if (sprite != null)
                {
                    // Show the confirmation dialog
                    NGUIEditorTools.DrawSeparator();
                    GUILayout.Label("Are you sure you want to delete '" + sprite.name + "'?");
                    NGUIEditorTools.DrawSeparator();

                    GUILayout.BeginHorizontal();
                    {
                        GUI.backgroundColor = Color.green;
                        if (GUILayout.Button("Cancel"))
                        {
                            mConfirmDelete = false;
                        }
                        GUI.backgroundColor = Color.red;

                        if (GUILayout.Button("Delete"))
                        {
                            NGUIEditorTools.RegisterUndo("Delete Sprite", mAtlas);
                            mAtlas.spriteList.Remove(sprite);
                            mConfirmDelete = false;
                        }
                        GUI.backgroundColor = Color.white;
                    }
                    GUILayout.EndHorizontal();
                }
                else
                {
                    mConfirmDelete = false;
                }
            }
            else
            {
                if (sprite == null && mAtlas.spriteList.Count > 0)
                {
                    string spriteName = NGUISettings.selectedSprite;
                    if (!string.IsNullOrEmpty(spriteName))
                    {
                        sprite = mAtlas.GetSprite(spriteName);
                    }
                    if (sprite == null)
                    {
                        sprite = mAtlas.spriteList[0];
                    }
                }

                if (!mConfirmDelete && sprite != null)
                {
                    NGUIEditorTools.DrawSeparator();
                    NGUIEditorTools.AdvancedSpriteField(mAtlas, sprite.name, SelectSprite, true);

                    if (sprite == null)
                    {
                        return;
                    }

                    Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D;

                    if (tex != null)
                    {
                        Rect inner = sprite.inner;
                        Rect outer = sprite.outer;

                        if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                        {
                            GUI.backgroundColor = green;
                            outer = NGUIEditorTools.IntRect("Dimensions", sprite.outer);

                            Vector4 border = new Vector4(
                                sprite.inner.xMin - sprite.outer.xMin,
                                sprite.inner.yMin - sprite.outer.yMin,
                                sprite.outer.xMax - sprite.inner.xMax,
                                sprite.outer.yMax - sprite.inner.yMax);

                            GUI.backgroundColor = blue;
                            border = NGUIEditorTools.IntPadding("Border", border);
                            GUI.backgroundColor = Color.white;

                            inner.xMin = sprite.outer.xMin + border.x;
                            inner.yMin = sprite.outer.yMin + border.y;
                            inner.xMax = sprite.outer.xMax - border.z;
                            inner.yMax = sprite.outer.yMax - border.w;
                        }
                        else
                        {
                            // Draw the inner and outer rectangle dimensions
                            GUI.backgroundColor = green;
                            outer = EditorGUILayout.RectField("Outer Rect", sprite.outer);
                            GUI.backgroundColor = blue;
                            inner = EditorGUILayout.RectField("Inner Rect", sprite.inner);
                            GUI.backgroundColor = Color.white;
                        }

                        if (outer.xMax < outer.xMin)
                        {
                            outer.xMax = outer.xMin;
                        }
                        if (outer.yMax < outer.yMin)
                        {
                            outer.yMax = outer.yMin;
                        }

                        if (outer != sprite.outer)
                        {
                            float x = outer.xMin - sprite.outer.xMin;
                            float y = outer.yMin - sprite.outer.yMin;

                            inner.x += x;
                            inner.y += y;
                        }

                        // Sanity checks to ensure that the inner rect is always inside the outer
                        inner.xMin = Mathf.Clamp(inner.xMin, outer.xMin, outer.xMax);
                        inner.xMax = Mathf.Clamp(inner.xMax, outer.xMin, outer.xMax);
                        inner.yMin = Mathf.Clamp(inner.yMin, outer.yMin, outer.yMax);
                        inner.yMax = Mathf.Clamp(inner.yMax, outer.yMin, outer.yMax);

                        bool changed = false;

                        if (sprite.inner != inner || sprite.outer != outer)
                        {
                            NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                            sprite.inner = inner;
                            sprite.outer = outer;
                            MarkSpriteAsDirty();
                            changed = true;
                        }

                        EditorGUILayout.Separator();

                        if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                        {
                            int left   = Mathf.RoundToInt(sprite.paddingLeft * sprite.outer.width);
                            int right  = Mathf.RoundToInt(sprite.paddingRight * sprite.outer.width);
                            int top    = Mathf.RoundToInt(sprite.paddingTop * sprite.outer.height);
                            int bottom = Mathf.RoundToInt(sprite.paddingBottom * sprite.outer.height);

                            NGUIEditorTools.IntVector a = NGUIEditorTools.IntPair("Padding", "Left", "Top", left, top);
                            NGUIEditorTools.IntVector b = NGUIEditorTools.IntPair(null, "Right", "Bottom", right, bottom);

                            if (changed || a.x != left || a.y != top || b.x != right || b.y != bottom)
                            {
                                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                                sprite.paddingLeft   = a.x / sprite.outer.width;
                                sprite.paddingTop    = a.y / sprite.outer.height;
                                sprite.paddingRight  = b.x / sprite.outer.width;
                                sprite.paddingBottom = b.y / sprite.outer.height;
                                MarkSpriteAsDirty();
                            }
                        }
                        else
                        {
                            // Create a button that can make the coordinates pixel-perfect on click
                            GUILayout.BeginHorizontal();
                            {
                                GUILayout.Label("Correction", GUILayout.Width(75f));

                                Rect corrected0 = outer;
                                Rect corrected1 = inner;

                                if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                                {
                                    corrected0 = NGUIMath.MakePixelPerfect(corrected0);
                                    corrected1 = NGUIMath.MakePixelPerfect(corrected1);
                                }
                                else
                                {
                                    corrected0 = NGUIMath.MakePixelPerfect(corrected0, tex.width, tex.height);
                                    corrected1 = NGUIMath.MakePixelPerfect(corrected1, tex.width, tex.height);
                                }

                                if (corrected0 == sprite.outer && corrected1 == sprite.inner)
                                {
                                    GUI.color = Color.grey;
                                    GUILayout.Button("Make Pixel-Perfect");
                                    GUI.color = Color.white;
                                }
                                else if (GUILayout.Button("Make Pixel-Perfect"))
                                {
                                    outer       = corrected0;
                                    inner       = corrected1;
                                    GUI.changed = true;
                                }
                            }
                            GUILayout.EndHorizontal();
                        }
                    }

                    // This functionality is no longer used. It became obsolete when the Atlas Maker was added.

                    /*NGUIEditorTools.DrawSeparator();
                     *
                     * GUILayout.BeginHorizontal();
                     * {
                     *      EditorGUILayout.PrefixLabel("Add/Delete");
                     *
                     *      if (GUILayout.Button("Clone Sprite"))
                     *      {
                     *              NGUIEditorTools.RegisterUndo("Add Sprite", mAtlas);
                     *              UIAtlas.Sprite newSprite = new UIAtlas.Sprite();
                     *
                     *              if (sprite != null)
                     *              {
                     *                      newSprite.name = "Copy of " + sprite.name;
                     *                      newSprite.outer = sprite.outer;
                     *                      newSprite.inner = sprite.inner;
                     *              }
                     *              else
                     *              {
                     *                      newSprite.name = "New Sprite";
                     *              }
                     *
                     *              mAtlas.spriteList.Add(newSprite);
                     *              sprite = newSprite;
                     *      }
                     *
                     *      // Show the delete button
                     *      GUI.backgroundColor = Color.red;
                     *
                     *      if (sprite != null && GUILayout.Button("Delete", GUILayout.Width(55f)))
                     *      {
                     *              mConfirmDelete = true;
                     *      }
                     *      GUI.backgroundColor = Color.white;
                     * }
                     * GUILayout.EndHorizontal();*/

                    if (NGUIEditorTools.previousSelection != null)
                    {
                        NGUIEditorTools.DrawSeparator();

                        GUI.backgroundColor = Color.green;

                        if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name))
                        {
                            NGUIEditorTools.SelectPrevious();
                        }
                        GUI.backgroundColor = Color.white;
                    }
                }
            }
        }
    }
Ejemplo n.º 16
0
 public static void RefreshAtlasesReferences(List <string> atlasNames, Device device, AtlasType refTargetType)
 {
     foreach (string atlasName in atlasNames)
     {
         RefreshAtlasReference(atlasName, device, refTargetType);
     }
 }
Ejemplo n.º 17
0
	/// <summary>
	/// Draw the inspector widget.
	/// </summary>

	public override void OnInspectorGUI ()
	{
		EditorGUIUtility.LookLikeControls(80f);
		mAtlas = target as UIAtlas;

		UIAtlas.Sprite sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null;

		NGUIEditorTools.DrawSeparator();

		if (mAtlas.replacement != null)
		{
			mType = AtlasType.Reference;
			mReplacement = mAtlas.replacement;
		}

		AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType);

		if (mType != after)
		{
			if (after == AtlasType.Normal)
			{
				OnSelectAtlas(null);
			}
			else
			{
				mType = AtlasType.Reference;
			}
		}

		if (mType == AtlasType.Reference)
		{
			ComponentSelector.Draw<UIAtlas>(mAtlas.replacement, OnSelectAtlas);

			NGUIEditorTools.DrawSeparator();
			EditorGUILayout.HelpBox("You can have one atlas simply point to " +
				"another one. This is useful if you want to be " +
				"able to quickly replace the contents of one " +
				"atlas with another one, for example for " +
				"swapping an SD atlas with an HD one, or " +
				"replacing an English atlas with a Chinese " +
				"one. All the sprites referencing this atlas " +
				"will update their references to the new one.", MessageType.Info);

			if (mReplacement != mAtlas && mAtlas.replacement != mReplacement)
			{
				NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
				mAtlas.replacement = mReplacement;
				UnityEditor.EditorUtility.SetDirty(mAtlas);
			}
			return;
		}

		if (!mConfirmDelete)
		{
			NGUIEditorTools.DrawSeparator();
			Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material;

			if (mAtlas.spriteMaterial != mat)
			{
				NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
				mAtlas.spriteMaterial = mat;

				// Ensure that this atlas has valid import settings
				if (mAtlas.texture != null) NGUIEditorTools.ImportTexture(mAtlas.texture, false, false);

				mAtlas.MarkAsDirty();
				mConfirmDelete = false;
			}

			if (mat != null)
			{
				TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset;

				if (ta != null)
				{
					// Ensure that this atlas has valid import settings
					if (mAtlas.texture != null) NGUIEditorTools.ImportTexture(mAtlas.texture, false, false);

					NGUIEditorTools.RegisterUndo("Import Sprites", mAtlas);
					NGUIJson.LoadSpriteData(mAtlas, ta);
					if (sprite != null) sprite = mAtlas.GetSprite(sprite.name);
					mAtlas.MarkAsDirty();
				}
				
				UIAtlas.Coordinates coords = (UIAtlas.Coordinates)EditorGUILayout.EnumPopup("Coordinates", mAtlas.coordinates);

				if (coords != mAtlas.coordinates)
				{
					NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
					mAtlas.coordinates = coords;
					mConfirmDelete = false;
				}

				float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize, GUILayout.Width(120f));

				if (pixelSize != mAtlas.pixelSize)
				{
					NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
					mAtlas.pixelSize = pixelSize;
					mConfirmDelete = false;
				}
			}
		}

		if (mAtlas.spriteMaterial != null)
		{
			Color blue = new Color(0f, 0.7f, 1f, 1f);
			Color green = new Color(0.4f, 1f, 0f, 1f);

			if (mConfirmDelete)
			{
				if (sprite != null)
				{
					// Show the confirmation dialog
					NGUIEditorTools.DrawSeparator();
					GUILayout.Label("Are you sure you want to delete '" + sprite.name + "'?");
					NGUIEditorTools.DrawSeparator();

					GUILayout.BeginHorizontal();
					{
						GUI.backgroundColor = Color.green;
						if (GUILayout.Button("Cancel")) mConfirmDelete = false;
						GUI.backgroundColor = Color.red;

						if (GUILayout.Button("Delete"))
						{
							NGUIEditorTools.RegisterUndo("Delete Sprite", mAtlas);
							mAtlas.spriteList.Remove(sprite);
							mConfirmDelete = false;
						}
						GUI.backgroundColor = Color.white;
					}
					GUILayout.EndHorizontal();
				}
				else mConfirmDelete = false;
			}
			else
			{
				if (sprite == null && mAtlas.spriteList.Count > 0)
				{
					string spriteName = NGUISettings.selectedSprite;
					if (!string.IsNullOrEmpty(spriteName)) sprite = mAtlas.GetSprite(spriteName);
					if (sprite == null) sprite = mAtlas.spriteList[0];
				}

				if (!mConfirmDelete && sprite != null)
				{
					NGUIEditorTools.DrawSeparator();
					NGUIEditorTools.AdvancedSpriteField(mAtlas, sprite.name, SelectSprite, true);

					if (sprite == null) return;

					Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D;

					if (tex != null)
					{
						Rect inner = sprite.inner;
						Rect outer = sprite.outer;

						if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
						{
							GUI.backgroundColor = green;
							outer = NGUIEditorTools.IntRect("Dimensions", sprite.outer);

							Vector4 border = new Vector4(
								sprite.inner.xMin - sprite.outer.xMin,
								sprite.inner.yMin - sprite.outer.yMin,
								sprite.outer.xMax - sprite.inner.xMax,
								sprite.outer.yMax - sprite.inner.yMax);

							GUI.backgroundColor = blue;
							border = NGUIEditorTools.IntPadding("Border", border);
							GUI.backgroundColor = Color.white;

							inner.xMin = sprite.outer.xMin + border.x;
							inner.yMin = sprite.outer.yMin + border.y;
							inner.xMax = sprite.outer.xMax - border.z;
							inner.yMax = sprite.outer.yMax - border.w;
						}
						else
						{
							// Draw the inner and outer rectangle dimensions
							GUI.backgroundColor = green;
							outer = EditorGUILayout.RectField("Outer Rect", sprite.outer);
							GUI.backgroundColor = blue;
							inner = EditorGUILayout.RectField("Inner Rect", sprite.inner);
							GUI.backgroundColor = Color.white;
						}

						if (outer.xMax < outer.xMin) outer.xMax = outer.xMin;
						if (outer.yMax < outer.yMin) outer.yMax = outer.yMin;

						if (outer != sprite.outer)
						{
							float x = outer.xMin - sprite.outer.xMin;
							float y = outer.yMin - sprite.outer.yMin;

							inner.x += x;
							inner.y += y;
						}

						// Sanity checks to ensure that the inner rect is always inside the outer
						inner.xMin = Mathf.Clamp(inner.xMin, outer.xMin, outer.xMax);
						inner.xMax = Mathf.Clamp(inner.xMax, outer.xMin, outer.xMax);
						inner.yMin = Mathf.Clamp(inner.yMin, outer.yMin, outer.yMax);
						inner.yMax = Mathf.Clamp(inner.yMax, outer.yMin, outer.yMax);
						
						bool changed = false;
						
						if (sprite.inner != inner || sprite.outer != outer)
						{
							NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
							sprite.inner = inner;
							sprite.outer = outer;
							MarkSpriteAsDirty();
							changed = true;
						}

						EditorGUILayout.Separator();

						if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
						{
							int left	= Mathf.RoundToInt(sprite.paddingLeft	* sprite.outer.width);
							int right	= Mathf.RoundToInt(sprite.paddingRight	* sprite.outer.width);
							int top		= Mathf.RoundToInt(sprite.paddingTop	* sprite.outer.height);
							int bottom	= Mathf.RoundToInt(sprite.paddingBottom	* sprite.outer.height);

							NGUIEditorTools.IntVector a = NGUIEditorTools.IntPair("Padding", "Left", "Top", left, top);
							NGUIEditorTools.IntVector b = NGUIEditorTools.IntPair(null, "Right", "Bottom", right, bottom);

							if (changed || a.x != left || a.y != top || b.x != right || b.y != bottom)
							{
								NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
								sprite.paddingLeft		= a.x / sprite.outer.width;
								sprite.paddingTop		= a.y / sprite.outer.height;
								sprite.paddingRight		= b.x / sprite.outer.width;
								sprite.paddingBottom	= b.y / sprite.outer.height;
								MarkSpriteAsDirty();
							}
						}
						else
						{
							// Create a button that can make the coordinates pixel-perfect on click
							GUILayout.BeginHorizontal();
							{
								GUILayout.Label("Correction", GUILayout.Width(75f));

								Rect corrected0 = outer;
								Rect corrected1 = inner;

								if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
								{
									corrected0 = NGUIMath.MakePixelPerfect(corrected0);
									corrected1 = NGUIMath.MakePixelPerfect(corrected1);
								}
								else
								{
									corrected0 = NGUIMath.MakePixelPerfect(corrected0, tex.width, tex.height);
									corrected1 = NGUIMath.MakePixelPerfect(corrected1, tex.width, tex.height);
								}

								if (corrected0 == sprite.outer && corrected1 == sprite.inner)
								{
									GUI.color = Color.grey;
									GUILayout.Button("Make Pixel-Perfect");
									GUI.color = Color.white;
								}
								else if (GUILayout.Button("Make Pixel-Perfect"))
								{
									outer = corrected0;
									inner = corrected1;
									GUI.changed = true;
								}
							}
							GUILayout.EndHorizontal();
						}
					}

					// This functionality is no longer used. It became obsolete when the Atlas Maker was added.
					/*NGUIEditorTools.DrawSeparator();

					GUILayout.BeginHorizontal();
					{
						EditorGUILayout.PrefixLabel("Add/Delete");

						if (GUILayout.Button("Clone Sprite"))
						{
							NGUIEditorTools.RegisterUndo("Add Sprite", mAtlas);
							UIAtlas.Sprite newSprite = new UIAtlas.Sprite();

							if (sprite != null)
							{
								newSprite.name = "Copy of " + sprite.name;
								newSprite.outer = sprite.outer;
								newSprite.inner = sprite.inner;
							}
							else
							{
								newSprite.name = "New Sprite";
							}

							mAtlas.spriteList.Add(newSprite);
							sprite = newSprite;
						}

						// Show the delete button
						GUI.backgroundColor = Color.red;

						if (sprite != null && GUILayout.Button("Delete", GUILayout.Width(55f)))
						{
							mConfirmDelete = true;
						}
						GUI.backgroundColor = Color.white;
					}
					GUILayout.EndHorizontal();*/

					if (NGUIEditorTools.previousSelection != null)
					{
						NGUIEditorTools.DrawSeparator();

						GUI.backgroundColor = Color.green;

						if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name))
						{
							NGUIEditorTools.SelectPrevious();
						}
						GUI.backgroundColor = Color.white;
					}
				}
			}
		}
	}
Ejemplo n.º 18
0
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        EditorGUIUtility.LookLikeControls(80f);
        mAtlas = target as UIAtlas;

        NGUIEditorTools.DrawSeparator();

        if (mAtlas.replacement != null)
        {
            mType        = AtlasType.Reference;
            mReplacement = mAtlas.replacement;
        }

        AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType);

        if (mType != after)
        {
            if (after == AtlasType.Normal)
            {
                OnSelectAtlas(null);
            }
            else
            {
                mType = AtlasType.Reference;
            }
        }

        if (mType == AtlasType.Reference)
        {
            ComponentSelector.Draw <UIAtlas>(mAtlas.replacement, OnSelectAtlas);

            NGUIEditorTools.DrawSeparator();
            GUILayout.Label("You can have one atlas simply point to\n" +
                            "another one. This is useful if you want to be\n" +
                            "able to quickly replace the contents of one\n" +
                            "atlas with another one, for example for\n" +
                            "swapping an SD atlas with an HD one, or\n" +
                            "replacing an English atlas with a Chinese\n" +
                            "one. All the sprites referencing this atlas\n" +
                            "will update their references to the new one.");

            if (mReplacement != mAtlas && mAtlas.replacement != mReplacement)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.replacement = mReplacement;
                UnityEditor.EditorUtility.SetDirty(mAtlas);
            }
            return;
        }

        if (!mConfirmDelete)
        {
            NGUIEditorTools.DrawSeparator();
            Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material;

            if (mAtlas.spriteMaterial != mat)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.spriteMaterial = mat;

                // Ensure that this atlas has valid import settings
                if (mAtlas.texture != null)
                {
                    NGUIEditorTools.ImportTexture(mAtlas.texture, false, false);
                }

                mAtlas.MarkAsDirty();
                mConfirmDelete = false;
            }

            if (mat != null)
            {
                TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset;

                if (ta != null)
                {
                    // Ensure that this atlas has valid import settings
                    if (mAtlas.texture != null)
                    {
                        NGUIEditorTools.ImportTexture(mAtlas.texture, false, false);
                    }

                    NGUIEditorTools.RegisterUndo("Import Sprites", mAtlas);
                    NGUIJson.LoadSpriteData(mAtlas, ta);
                    if (mSprite != null)
                    {
                        mSprite = mAtlas.GetSprite(mSprite.name);
                    }
                    mAtlas.MarkAsDirty();
                }

                UIAtlas.Coordinates coords = (UIAtlas.Coordinates)EditorGUILayout.EnumPopup("Coordinates", mAtlas.coordinates);

                if (coords != mAtlas.coordinates)
                {
                    NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                    mAtlas.coordinates = coords;
                    mConfirmDelete     = false;
                }

                float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize);

                if (pixelSize != mAtlas.pixelSize)
                {
                    NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                    mAtlas.pixelSize = pixelSize;
                    mConfirmDelete   = false;
                }
            }
        }

        if (mAtlas.spriteMaterial != null)
        {
            Color blue  = new Color(0f, 0.7f, 1f, 1f);
            Color green = new Color(0.4f, 1f, 0f, 1f);

            if (mConfirmDelete)
            {
                if (mSprite != null)
                {
                    // Show the confirmation dialog
                    NGUIEditorTools.DrawSeparator();
                    GUILayout.Label("Are you sure you want to delete '" + mSprite.name + "'?");
                    NGUIEditorTools.DrawSeparator();

                    GUILayout.BeginHorizontal();
                    {
                        GUI.backgroundColor = Color.green;
                        if (GUILayout.Button("Cancel"))
                        {
                            mConfirmDelete = false;
                        }
                        GUI.backgroundColor = Color.red;

                        if (GUILayout.Button("Delete"))
                        {
                            NGUIEditorTools.RegisterUndo("Delete Sprite", mAtlas);
                            mAtlas.spriteList.Remove(mSprite);
                            mConfirmDelete = false;
                        }
                        GUI.backgroundColor = Color.white;
                    }
                    GUILayout.EndHorizontal();
                }
                else
                {
                    mConfirmDelete = false;
                }
            }
            else
            {
                if (mSprite == null && mAtlas.spriteList.Count > 0)
                {
                    string spriteName = EditorPrefs.GetString("NGUI Selected Sprite");
                    if (!string.IsNullOrEmpty(spriteName))
                    {
                        mSprite = mAtlas.GetSprite(spriteName);
                    }
                    if (mSprite == null)
                    {
                        mSprite = mAtlas.spriteList[0];
                    }
                }

                GUI.backgroundColor = Color.green;

                GUILayout.BeginHorizontal();
                {
                    EditorGUILayout.PrefixLabel("Add/Delete");

                    if (GUILayout.Button("New Sprite"))
                    {
                        NGUIEditorTools.RegisterUndo("Add Sprite", mAtlas);
                        UIAtlas.Sprite newSprite = new UIAtlas.Sprite();

                        if (mSprite != null)
                        {
                            newSprite.name  = "Copy of " + mSprite.name;
                            newSprite.outer = mSprite.outer;
                            newSprite.inner = mSprite.inner;
                        }
                        else
                        {
                            newSprite.name = "New Sprite";
                        }

                        mAtlas.spriteList.Add(newSprite);
                        mSprite = newSprite;
                    }

                    // Show the delete button
                    GUI.backgroundColor = Color.red;

                    if (mSprite != null && GUILayout.Button("Delete", GUILayout.Width(55f)))
                    {
                        mConfirmDelete = true;
                    }
                    GUI.backgroundColor = Color.white;
                }
                GUILayout.EndHorizontal();

                if (!mConfirmDelete && mSprite != null)
                {
                    NGUIEditorTools.DrawSeparator();

                    string spriteName = UISpriteInspector.SpriteField(mAtlas, mSprite.name);

                    if (spriteName != mSprite.name)
                    {
                        mSprite = mAtlas.GetSprite(spriteName);
                        EditorPrefs.SetString("NGUI Selected Sprite", spriteName);
                    }

                    if (mSprite == null)
                    {
                        return;
                    }

                    Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D;

                    if (tex != null)
                    {
                        Rect inner = mSprite.inner;
                        Rect outer = mSprite.outer;

                        string name = EditorGUILayout.TextField("Edit Name", mSprite.name);

                        if (mSprite.name != name && !string.IsNullOrEmpty(name))
                        {
                            bool found = false;

                            foreach (UIAtlas.Sprite sp in mAtlas.spriteList)
                            {
                                if (sp.name == name)
                                {
                                    found = true;
                                    break;
                                }
                            }

                            if (!found)
                            {
                                NGUIEditorTools.RegisterUndo("Edit Sprite Name", mAtlas);
                                mSprite.name = name;
                            }
                        }

                        if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                        {
                            GUI.backgroundColor = green;
                            outer = NGUIEditorTools.IntRect("Dimensions", mSprite.outer);

                            Vector4 border = new Vector4(
                                mSprite.inner.xMin - mSprite.outer.xMin,
                                mSprite.inner.yMin - mSprite.outer.yMin,
                                mSprite.outer.xMax - mSprite.inner.xMax,
                                mSprite.outer.yMax - mSprite.inner.yMax);

                            GUI.backgroundColor = blue;
                            border = NGUIEditorTools.IntPadding("Border", border);
                            GUI.backgroundColor = Color.white;

                            inner.xMin = mSprite.outer.xMin + border.x;
                            inner.yMin = mSprite.outer.yMin + border.y;
                            inner.xMax = mSprite.outer.xMax - border.z;
                            inner.yMax = mSprite.outer.yMax - border.w;
                        }
                        else
                        {
                            // Draw the inner and outer rectangle dimensions
                            GUI.backgroundColor = green;
                            outer = EditorGUILayout.RectField("Outer Rect", mSprite.outer);
                            GUI.backgroundColor = blue;
                            inner = EditorGUILayout.RectField("Inner Rect", mSprite.inner);
                            GUI.backgroundColor = Color.white;
                        }

                        if (outer.xMax < outer.xMin)
                        {
                            outer.xMax = outer.xMin;
                        }
                        if (outer.yMax < outer.yMin)
                        {
                            outer.yMax = outer.yMin;
                        }

                        if (outer != mSprite.outer)
                        {
                            float x = outer.xMin - mSprite.outer.xMin;
                            float y = outer.yMin - mSprite.outer.yMin;

                            inner.x += x;
                            inner.y += y;
                        }

                        // Sanity checks to ensure that the inner rect is always inside the outer
                        inner.xMin = Mathf.Clamp(inner.xMin, outer.xMin, outer.xMax);
                        inner.xMax = Mathf.Clamp(inner.xMax, outer.xMin, outer.xMax);
                        inner.yMin = Mathf.Clamp(inner.yMin, outer.yMin, outer.yMax);
                        inner.yMax = Mathf.Clamp(inner.yMax, outer.yMin, outer.yMax);

                        EditorGUILayout.Separator();

                        // Padding is mainly meant to be used by the 'trimmed' feature of TexturePacker
                        if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                        {
                            int left   = Mathf.RoundToInt(mSprite.paddingLeft * mSprite.outer.width);
                            int right  = Mathf.RoundToInt(mSprite.paddingRight * mSprite.outer.width);
                            int top    = Mathf.RoundToInt(mSprite.paddingTop * mSprite.outer.height);
                            int bottom = Mathf.RoundToInt(mSprite.paddingBottom * mSprite.outer.height);

                            NGUIEditorTools.IntVector a = NGUIEditorTools.IntPair("Padding", "Left", "Top", left, top);
                            NGUIEditorTools.IntVector b = NGUIEditorTools.IntPair(null, "Right", "Bottom", right, bottom);

                            if (a.x != left || a.y != top || b.x != right || b.y != bottom)
                            {
                                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                                mSprite.paddingLeft   = a.x / mSprite.outer.width;
                                mSprite.paddingTop    = a.y / mSprite.outer.width;
                                mSprite.paddingRight  = b.x / mSprite.outer.height;
                                mSprite.paddingBottom = b.y / mSprite.outer.height;
                                MarkSpriteAsDirty();
                            }
                        }
                        else
                        {
                            // Create a button that can make the coordinates pixel-perfect on click
                            GUILayout.BeginHorizontal();
                            {
                                GUILayout.Label("Correction", GUILayout.Width(75f));

                                Rect corrected0 = outer;
                                Rect corrected1 = inner;

                                if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                                {
                                    corrected0 = NGUIMath.MakePixelPerfect(corrected0);
                                    corrected1 = NGUIMath.MakePixelPerfect(corrected1);
                                }
                                else
                                {
                                    corrected0 = NGUIMath.MakePixelPerfect(corrected0, tex.width, tex.height);
                                    corrected1 = NGUIMath.MakePixelPerfect(corrected1, tex.width, tex.height);
                                }

                                if (corrected0 == mSprite.outer && corrected1 == mSprite.inner)
                                {
                                    GUI.color = Color.grey;
                                    GUILayout.Button("Make Pixel-Perfect");
                                    GUI.color = Color.white;
                                }
                                else if (GUILayout.Button("Make Pixel-Perfect"))
                                {
                                    outer       = corrected0;
                                    inner       = corrected1;
                                    GUI.changed = true;
                                }
                            }
                            GUILayout.EndHorizontal();
                        }

                        GUILayout.BeginHorizontal();
                        {
                            mView = (View)EditorGUILayout.EnumPopup("Show", mView);
                            GUILayout.Label("Shader", GUILayout.Width(45f));

                            if (mUseShader != EditorGUILayout.Toggle(mUseShader, GUILayout.Width(20f)))
                            {
                                mUseShader = !mUseShader;

                                if (mUseShader && mView == View.Sprite)
                                {
                                    // TODO: Remove this when Unity fixes the bug with DrawPreviewTexture not being affected by BeginGroup
                                    Debug.LogWarning("There is a bug in Unity that prevents the texture from getting clipped properly.\n" +
                                                     "Until it's fixed by Unity, your texture may spill onto the rest of the Unity's GUI while using this mode.");
                                }
                            }
                        }
                        GUILayout.EndHorizontal();

                        if (mSprite.outer != outer || mSprite.inner != inner)
                        {
                            NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                            mSprite.outer  = outer;
                            mSprite.inner  = inner;
                            mConfirmDelete = false;
                            MarkSpriteAsDirty();
                        }

                        Rect uv0 = outer;
                        Rect uv1 = inner;

                        if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                        {
                            uv0 = NGUIMath.ConvertToTexCoords(uv0, tex.width, tex.height);
                            uv1 = NGUIMath.ConvertToTexCoords(uv1, tex.width, tex.height);
                        }

                        // Draw the atlas
                        EditorGUILayout.Separator();
                        Material m    = mUseShader ? mAtlas.spriteMaterial : null;
                        Rect     rect = (mView == View.Atlas) ? NGUIEditorTools.DrawAtlas(tex, m) : NGUIEditorTools.DrawSprite(tex, uv0, m);

                        // Draw the sprite outline
                        NGUIEditorTools.DrawOutline(rect, uv0, uv1);
                        EditorGUILayout.Separator();
                    }
                }
            }
        }
    }
Ejemplo n.º 19
0
	/// <summary>
	/// Draw the inspector widget.
	/// </summary>

	public override void OnInspectorGUI ()
	{
		NGUIEditorTools.SetLabelWidth(80f);
		mAtlas = target as UIAtlas;

		UISpriteData sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null;

		GUILayout.Space(6f);

		if (mAtlas.replacement != null)
		{
			mType = AtlasType.Reference;
			mReplacement = mAtlas.replacement;
		}

		GUILayout.BeginHorizontal();
		AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType);
		GUILayout.Space(18f);
		GUILayout.EndHorizontal();

		if (mType != after)
		{
			if (after == AtlasType.Normal)
			{
				mType = AtlasType.Normal;
				OnSelectAtlas(null);
			}
			else
			{
				mType = AtlasType.Reference;
			}
		}

		if (mType == AtlasType.Reference)
		{
			ComponentSelector.Draw<UIAtlas>(mAtlas.replacement, OnSelectAtlas, true);

			GUILayout.Space(6f);
			EditorGUILayout.HelpBox("You can have one atlas simply point to " +
				"another one. This is useful if you want to be " +
				"able to quickly replace the contents of one " +
				"atlas with another one, for example for " +
				"swapping an SD atlas with an HD one, or " +
				"replacing an English atlas with a Chinese " +
				"one. All the sprites referencing this atlas " +
				"will update their references to the new one.", MessageType.Info);

			if (mReplacement != mAtlas && mAtlas.replacement != mReplacement)
			{
				NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
				mAtlas.replacement = mReplacement;
				NGUITools.SetDirty(mAtlas);
			}
			return;
		}

		//GUILayout.Space(6f);
		Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material;

		if (mAtlas.spriteMaterial != mat)
		{
			NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
			mAtlas.spriteMaterial = mat;

			// Ensure that this atlas has valid import settings
			if (mAtlas.texture != null) NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);

			mAtlas.MarkAsChanged();
		}

		if (mat != null)
		{
			TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset;

			if (ta != null)
			{
				// Ensure that this atlas has valid import settings
				if (mAtlas.texture != null) NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);

				NGUIEditorTools.RegisterUndo("Import Sprites", mAtlas);
				NGUIJson.LoadSpriteData(mAtlas, ta);
				if (sprite != null) sprite = mAtlas.GetSprite(sprite.name);
				mAtlas.MarkAsChanged();
			}

			float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize, GUILayout.Width(120f));

			if (pixelSize != mAtlas.pixelSize)
			{
				NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
				mAtlas.pixelSize = pixelSize;
			}
		}

		if (mAtlas.spriteMaterial != null)
		{
			Color blueColor = new Color(0f, 0.7f, 1f, 1f);
			Color greenColor = new Color(0.4f, 1f, 0f, 1f);

			if (sprite == null && mAtlas.spriteList.Count > 0)
			{
				string spriteName = NGUISettings.selectedSprite;
				if (!string.IsNullOrEmpty(spriteName)) sprite = mAtlas.GetSprite(spriteName);
				if (sprite == null) sprite = mAtlas.spriteList[0];
			}

			if (sprite != null)
			{
				if (sprite == null) return;
					
				Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D;

				if (tex != null)
				{
					if (!NGUIEditorTools.DrawHeader("Sprite Details")) return;

					NGUIEditorTools.BeginContents();

					GUILayout.Space(3f);
					NGUIEditorTools.DrawAdvancedSpriteField(mAtlas, sprite.name, SelectSprite, true);
					GUILayout.Space(6f);

					GUI.changed = false;

					GUI.backgroundColor = greenColor;
					NGUIEditorTools.IntVector sizeA = NGUIEditorTools.IntPair("Dimensions", "X", "Y", sprite.x, sprite.y);
					NGUIEditorTools.IntVector sizeB = NGUIEditorTools.IntPair(null, "Width", "Height", sprite.width, sprite.height);

					EditorGUILayout.Separator();
					GUI.backgroundColor = blueColor;
					NGUIEditorTools.IntVector borderA = NGUIEditorTools.IntPair("Border", "Left", "Right", sprite.borderLeft, sprite.borderRight);
					NGUIEditorTools.IntVector borderB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.borderBottom, sprite.borderTop);

					EditorGUILayout.Separator();
					GUI.backgroundColor = Color.white;
					NGUIEditorTools.IntVector padA = NGUIEditorTools.IntPair("Padding", "Left", "Right", sprite.paddingLeft, sprite.paddingRight);
					NGUIEditorTools.IntVector padB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.paddingBottom, sprite.paddingTop);

					if (GUI.changed)
					{
						NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
						
						sprite.x = sizeA.x;
						sprite.y = sizeA.y;
						sprite.width = sizeB.x;
						sprite.height = sizeB.y;

						sprite.paddingLeft = padA.x;
						sprite.paddingRight = padA.y;
						sprite.paddingBottom = padB.x;
						sprite.paddingTop = padB.y;

						sprite.borderLeft = borderA.x;
						sprite.borderRight = borderA.y;
						sprite.borderBottom = borderB.x;
						sprite.borderTop = borderB.y;

						MarkSpriteAsDirty();
					}

					GUILayout.Space(3f);

					GUILayout.BeginHorizontal();

					if (GUILayout.Button("Duplicate"))
					{
						UIAtlasMaker.SpriteEntry se = UIAtlasMaker.DuplicateSprite(mAtlas, sprite.name);
						if (se != null) NGUISettings.selectedSprite = se.name;
					}

					if (GUILayout.Button("Save As..."))
					{
						string path = EditorUtility.SaveFilePanelInProject("Save As", sprite.name + ".png", "png", "Extract sprite into which file?");

						if (!string.IsNullOrEmpty(path))
						{
							UIAtlasMaker.SpriteEntry se = UIAtlasMaker.ExtractSprite(mAtlas, sprite.name);

							if (se != null)
							{
								byte[] bytes = se.tex.EncodeToPNG();
								File.WriteAllBytes(path, bytes);
								AssetDatabase.ImportAsset(path);
								if (se.temporaryTexture) DestroyImmediate(se.tex);
							}
						}
					}
					GUILayout.EndHorizontal();
					NGUIEditorTools.EndContents();
				}

				if (NGUIEditorTools.DrawHeader("Modify"))
				{
					NGUIEditorTools.BeginContents();

					EditorGUILayout.BeginHorizontal();
					GUILayout.Space(20f);
					EditorGUILayout.BeginVertical();

					NGUISettings.backgroundColor = EditorGUILayout.ColorField("Background", NGUISettings.backgroundColor);

					if (GUILayout.Button("Add a Shadow")) AddShadow(sprite);
					if (GUILayout.Button("Add a Soft Outline")) AddOutline(sprite);

					if (GUILayout.Button("Add a Transparent Border")) AddTransparentBorder(sprite);
					if (GUILayout.Button("Add a Clamped Border")) AddClampedBorder(sprite);
					if (GUILayout.Button("Add a Tiled Border")) AddTiledBorder(sprite);
					EditorGUI.BeginDisabledGroup(!sprite.hasBorder);
					if (GUILayout.Button("Crop Border")) CropBorder(sprite);
					EditorGUI.EndDisabledGroup();

					EditorGUILayout.EndVertical();
					GUILayout.Space(20f);
					EditorGUILayout.EndHorizontal();

					NGUIEditorTools.EndContents();
				}

				if (NGUIEditorTools.previousSelection != null)
				{
					GUILayout.Space(3f);
					GUI.backgroundColor = Color.green;

					if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name))
					{
						NGUIEditorTools.SelectPrevious();
					}
					GUI.backgroundColor = Color.white;
				}
			}
		}
	}
Ejemplo n.º 20
0
 // 팝업 단위로 로드할때 쓰인다
 public void DynamicLoadAtlasTexture(string textureName, AtlasType atlasType = AtlasType.None)
 {
     // 지정하지 않았을때
     LoadAtlasTexture(atlasType, textureName);
 }