Exemplo n.º 1
0
    /// <summary>
    /// Replace the sprites within the atlas.
    /// </summary>

    static void ReplaceSprites(UIAtlas atlas, List <SpriteEntry> sprites)
    {
        // Get the list of sprites we'll be updating
        List <UISpriteData> spriteList = atlas.spriteList;
        List <UISpriteData> kept       = new List <UISpriteData>();

        // Run through all the textures we added and add them as sprites to the atlas
        for (int i = 0; i < sprites.Count; ++i)
        {
            SpriteEntry  se     = sprites[i];
            UISpriteData sprite = AddSprite(spriteList, se);
            kept.Add(sprite);
        }

        // Remove unused sprites
        for (int i = spriteList.Count; i > 0;)
        {
            UISpriteData sp = spriteList[--i];
            if (!kept.Contains(sp))
            {
                spriteList.RemoveAt(i);
            }
        }

        // Sort the sprites so that they are alphabetical within the atlas
        atlas.SortAlphabetically();
        atlas.MarkAsDirty();
    }
Exemplo n.º 2
0
    /// <summary>
    /// Update the sprite atlas, keeping only the sprites that are on the specified list.
    /// </summary>

    static void UpdateAtlas(UIAtlas atlas, List <SpriteEntry> sprites)
    {
        if (sprites.Count > 0)
        {
            // Combine all sprites into a single texture and save it
            UpdateTexture(atlas, sprites);

            // Replace the sprites within the atlas
            ReplaceSprites(atlas, sprites);

            // Release the temporary textures
            ReleaseSprites(sprites);
        }
        else
        {
            atlas.spriteList.Clear();
            string path = NGUIEditorTools.GetSaveableTexturePath(atlas);
            atlas.spriteMaterial.mainTexture = null;
            if (!string.IsNullOrEmpty(path))
            {
                AssetDatabase.DeleteAsset(path);
            }
        }
        atlas.MarkAsDirty();

        EditorUtility.DisplayDialog("Atlas Changed", "The atlas has been updated. Don't forget to save the scene to write the changes!", "OK");
    }
Exemplo n.º 3
0
    /// <summary>
    /// Replace the sprites within the atlas.
    /// </summary>

    static void ReplaceSprites(UIAtlas atlas, List <SpriteEntry> sprites)
    {
        // Get the list of sprites we'll be updating
        List <UIAtlas.Sprite> spriteList = atlas.spriteList;
        List <UIAtlas.Sprite> kept       = new List <UIAtlas.Sprite>();

        // The atlas must be in pixels
        atlas.coordinates = UIAtlas.Coordinates.Pixels;

        // Run through all the textures we added and add them as sprites to the atlas
        for (int i = 0; i < sprites.Count; ++i)
        {
            SpriteEntry    se     = sprites[i];
            UIAtlas.Sprite sprite = AddSprite(spriteList, se);
            kept.Add(sprite);
        }

        // Remove unused sprites
        for (int i = spriteList.Count; i > 0;)
        {
            UIAtlas.Sprite sp = spriteList[--i];
            if (!kept.Contains(sp))
            {
                spriteList.RemoveAt(i);
            }
        }
        atlas.MarkAsDirty();
    }
Exemplo n.º 4
0
    /// <summary>
    /// Mark all widgets associated with this atlas as having changed.
    /// </summary>

    public void MarkAsDirty()
    {
        if (mReplacement != null)
        {
            mReplacement.MarkAsDirty();
        }
#if UNITY_EDITOR
        UnityEditor.EditorUtility.SetDirty(gameObject);
#endif
        UISprite[] list = NGUITools.FindActive <UISprite>();

        for (int i = 0, imax = list.Length; i < imax; ++i)
        {
            UISprite sp = list[i];

            if (CheckIfRelated(this, sp.atlas))
            {
                UIAtlas atl = sp.atlas;
                sp.atlas = null;
                sp.atlas = atl;
#if UNITY_EDITOR
                UnityEditor.EditorUtility.SetDirty(sp);
#endif
            }
        }

        UIFont[] fonts = Resources.FindObjectsOfTypeAll(typeof(UIFont)) as UIFont[];

        for (int i = 0, imax = fonts.Length; i < imax; ++i)
        {
            UIFont font = fonts[i];

            if (CheckIfRelated(this, font.atlas))
            {
                UIAtlas atl = font.atlas;
                font.atlas = null;
                font.atlas = atl;
#if UNITY_EDITOR
                UnityEditor.EditorUtility.SetDirty(font);
#endif
            }
        }

        UILabel[] labels = NGUITools.FindActive <UILabel>();

        for (int i = 0, imax = labels.Length; i < imax; ++i)
        {
            UILabel lbl = labels[i];

            if (lbl.font != null && CheckIfRelated(this, lbl.font.atlas))
            {
                UIFont font = lbl.font;
                lbl.font = null;
                lbl.font = font;
#if UNITY_EDITOR
                UnityEditor.EditorUtility.SetDirty(lbl);
#endif
            }
        }
    }
Exemplo n.º 5
0
    /// <summary>
    /// Update the sprite atlas, keeping only the sprites that are on the specified list.
    /// </summary>

    static void UpdateAtlas(UIAtlas atlas, List <SpriteEntry> sprites)
    {
        if (sprites.Count > 0)
        {
            // Combine all sprites into a single texture and save it
            if (UpdateTexture(atlas, sprites))
            {
                // Replace the sprites within the atlas
                ReplaceSprites(atlas, sprites);
            }

            // Release the temporary textures
            ReleaseSprites(sprites);
            return;
        }
        else
        {
            atlas.spriteList.Clear();
            string path = NGUIEditorTools.GetSaveableTexturePath(atlas);
            atlas.spriteMaterial.mainTexture = null;
            if (!string.IsNullOrEmpty(path))
            {
                AssetDatabase.DeleteAsset(path);
            }
        }
        atlas.MarkAsDirty();

        Debug.Log("The atlas has been updated. Don't forget to save the scene to write the changes!");
        Selection.activeGameObject = (NGUISettings.atlas != null) ? NGUISettings.atlas.gameObject : null;
    }
Exemplo n.º 6
0
    static RetType handlePrefab(string assetPath)
    {
        UIAtlas atlas = AssetDatabase.LoadAssetAtPath(assetPath, typeof(UIAtlas)) as UIAtlas;

        if (null == atlas)
        {
            return(RetType.Ignore);
        }
        if (null == atlas.spriteMaterial || null == atlas.spriteMaterial.mainTexture)
        {
            return(RetType.Error);
        }
        string path = AssetDatabase.GetAssetPath(atlas.spriteMaterial.mainTexture);
        //缩放关联的texture/
        RetType ret = handleTexture(path);

        if (atlas.scale != 1f)
        {
            return(RetType.Ignore);
        }
        atlas.scale = 0.5f;
        //缩放图集坐标/
        BetterList <string> sl = atlas.GetListOfSprites();

        if (sl == null)
        {
            return(RetType.Error);
        }
        foreach (string sn in sl)
        {
            UISpriteData sd = atlas.GetSprite(sn);
            sd.x      /= 2;
            sd.y      /= 2;
            sd.width  /= 2;
            sd.height /= 2;

            sd.borderBottom /= 2;
            sd.borderLeft   /= 2;
            sd.borderRight  /= 2;
            sd.borderTop    /= 2;

            sd.paddingTop    /= 2;
            sd.paddingBottom /= 2;
            sd.paddingLeft   /= 2;
            sd.paddingRight  /= 2;
        }
        atlas.pixelSize *= 2;
        atlas.MarkAsDirty();
        return(ret);
    }
Exemplo n.º 7
0
    /// <summary>
    /// 压缩UIAtlas
    /// </summary>
    /// <param name="uiatlasobj"></param>
    /// <returns></returns>
    public static ShrinkResult ShrinkAtlas(Object uiatlasobj)
    {
        AtlasProperty originalAtlasProp = null;
        ShrinkResult  ret = prepareShrinkAtlas(uiatlasobj, ref originalAtlasProp);

        if (ret != ShrinkResult.Success)
        {
            return(ret);
        }

        UIAtlas             uiatlas = ((GameObject)uiatlasobj).GetComponent <UIAtlas>();
        BetterList <string> sl      = uiatlas.GetListOfSprites();

        //备份原始尺寸
        originalAtlasProp = GetAtlasProperty(uiatlas, true);

        float times = 2f;

        uiatlas.scale /= times;

        foreach (string sn in sl)
        {
            UISpriteData sd = uiatlas.GetSprite(sn);
            sd.x      /= (int)times;
            sd.y      /= (int)times;
            sd.width  /= (int)times;
            sd.height /= (int)times;

            sd.borderBottom /= (int)times;
            sd.borderLeft   /= (int)times;
            sd.borderRight  /= (int)times;
            sd.borderTop    /= (int)times;

            sd.paddingTop    /= (int)times;
            sd.paddingBottom /= (int)times;
            sd.paddingLeft   /= (int)times;
            sd.paddingRight  /= (int)times;
        }
        uiatlas.pixelSize *= times;
        uiatlas.MarkAsDirty();

        AtlasProperty currentAtlasProp = GetAtlasProperty(uiatlas);

        SaveAtlasData(uiatlas, originalAtlasProp, currentAtlasProp);

        return(ShrinkResult.Success);
    }
Exemplo n.º 8
0
    public void MarkAsDirty()
    {
        if (mReplacement != null)
        {
            mReplacement.MarkAsDirty();
        }
        UISprite[] array = NGUITools.FindActive <UISprite>();
        int        i     = 0;

        for (int num = array.Length; i < num; i++)
        {
            UISprite uISprite = array[i];
            if (CheckIfRelated(this, uISprite.atlas))
            {
                UIAtlas atlas = uISprite.atlas;
                uISprite.atlas = null;
                uISprite.atlas = atlas;
            }
        }
        UIFont[] array2 = Resources.FindObjectsOfTypeAll(typeof(UIFont)) as UIFont[];
        int      j      = 0;

        for (int num2 = array2.Length; j < num2; j++)
        {
            UIFont uIFont = array2[j];
            if (CheckIfRelated(this, uIFont.atlas))
            {
                UIAtlas atlas2 = uIFont.atlas;
                uIFont.atlas = null;
                uIFont.atlas = atlas2;
            }
        }
        UILabel[] array3 = NGUITools.FindActive <UILabel>();
        int       k      = 0;

        for (int num3 = array3.Length; k < num3; k++)
        {
            UILabel uILabel = array3[k];
            if (uILabel.font != null && CheckIfRelated(this, uILabel.font.atlas))
            {
                UIFont font = uILabel.font;
                uILabel.font = null;
                uILabel.font = font;
            }
        }
    }
Exemplo n.º 9
0
    /// <summary>
    /// 回复UIAtlas
    /// </summary>
    /// <param name="uiatlas"></param>
    public static void RecoverUIAtlasFromMeta(Object uiatlasobj)
    {
        UIAtlas uiatlas   = ((GameObject)uiatlasobj).GetComponent <UIAtlas>();
        string  assetpath = AssetDatabase.GetAssetPath(uiatlas.gameObject);

        if (null == uiatlas.spriteMaterial || null == uiatlas.spriteList)
        {
            return;
        }
        //先把纹理都恢复了
        Object[]         dependObjs  = new Object[] { uiatlas.spriteMaterial.mainTexture };//获取纹理
        List <Texture2D> textureList = new List <Texture2D>();

        for (int i = 0; i < dependObjs.Length; i++)
        {
            if (dependObjs[i] is Texture2D)
            {
                textureList.Add(dependObjs[i] as Texture2D);
            }
        }

        for (int i = textureList.Count - 1; i >= 0; --i)
        {
            try
            {
                RecoverTextureFromMeta(textureList[i]);
            }
            catch (System.Exception e)
            {
                Debug.Log(e.ToString() + " " + textureList[i].name);
            }
        }

        if (!AtlasAlreadyShrinked(assetpath))
        {//并未缩减过,则不对它进行操作
            return;
        }
        AtlasProperty afterAtlasPro = db.GetAtlasProperty(AssetDatabase.GetAssetPath(uiatlas.gameObject), true);

        uiatlas = ((GameObject)uiatlasobj).GetComponent <UIAtlas>();
        BetterList <string> sl = uiatlas.GetListOfSprites();

        if (sl == null)
        {
            return;
        }
        uiatlas.scale     = afterAtlasPro.scale;
        uiatlas.pixelSize = afterAtlasPro.pixelSize;
        for (int i = 0; i < afterAtlasPro.SpriteDetails.Count; i++)
        {
            UISpriteData sd  = uiatlas.GetSprite(sl[i]);
            SpriteDetail spd = afterAtlasPro.SpriteDetails[i];

            sd.x      = (int)spd.Demension.x;
            sd.y      = (int)spd.Demension.y;
            sd.width  = (int)spd.Demension.z;
            sd.height = (int)spd.Demension.w;

            sd.borderLeft   = (int)spd.Border.x;
            sd.borderRight  = (int)spd.Border.y;
            sd.borderBottom = (int)spd.Border.z;
            sd.borderTop    = (int)spd.Border.w;

            sd.paddingLeft   = (int)spd.Padding.x;
            sd.paddingRight  = (int)spd.Padding.y;
            sd.paddingBottom = (int)spd.Padding.z;
            sd.paddingTop    = (int)spd.Padding.w;
        }
        uiatlas.MarkAsDirty();
        AtlasProperty currentAtlas = GetAtlasProperty(uiatlas, true);

        SaveAtlasData(uiatlas, afterAtlasPro, currentAtlas);
    }
Exemplo n.º 10
0
	/// <summary>
	/// Update the sprite atlas, keeping only the sprites that are on the specified list.
	/// </summary>

	static void UpdateAtlas (UIAtlas atlas, List<SpriteEntry> sprites)
	{
		if (sprites.Count > 0)
		{
			// Combine all sprites into a single texture and save it
			UpdateTexture(atlas, sprites);

			// Replace the sprites within the atlas
			ReplaceSprites(atlas, sprites);

			// Release the temporary textures
			ReleaseSprites(sprites);
		}
		else
		{
			atlas.spriteList.Clear();
			string path = NGUIEditorTools.GetSaveableTexturePath(atlas);
			atlas.spriteMaterial.mainTexture = null;
			if (!string.IsNullOrEmpty(path)) AssetDatabase.DeleteAsset(path);
		}
		atlas.MarkAsDirty();

		EditorUtility.DisplayDialog("Atlas Changed", "The atlas has been updated. Don't forget to save the scene to write the changes!", "OK");
	}
Exemplo n.º 11
0
	/// <summary>
	/// Replace the sprites within the atlas.
	/// </summary>

	static void ReplaceSprites (UIAtlas atlas, List<SpriteEntry> sprites)
	{
		// Get the list of sprites we'll be updating
		List<UIAtlas.Sprite> spriteList = atlas.spriteList;
		List<UIAtlas.Sprite> kept = new List<UIAtlas.Sprite>();

		// The atlas must be in pixels
		atlas.coordinates = UIAtlas.Coordinates.Pixels;

		// Run through all the textures we added and add them as sprites to the atlas
		for (int i = 0; i < sprites.Count; ++i)
		{
			SpriteEntry se = sprites[i];
			UIAtlas.Sprite sprite = AddSprite(spriteList, se);
			kept.Add(sprite);
		}

		// Remove unused sprites
		for (int i = spriteList.Count; i > 0; )
		{
			UIAtlas.Sprite sp = spriteList[--i];
			if (!kept.Contains(sp)) spriteList.RemoveAt(i);
		}
		atlas.MarkAsDirty();
	}
Exemplo n.º 12
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 as UIAtlas;
        }

        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 as UIAtlas, 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();
                    }
                }
            }
        }
    }
Exemplo n.º 13
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;
					}
				}
			}
		}
	}
Exemplo n.º 14
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;
                    }
                }
            }
        }
    }
    /// <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);

            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.AdvancedSpriteField(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;
                }
            }
        }
    }
Exemplo n.º 16
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();
                    }
                }
            }
        }
    }
Exemplo n.º 17
0
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>
    public override void OnInspectorGUI()
    {
        mRegisteredUndo = false;
        EditorGUIUtility.LookLikeControls(80f);
        mAtlas = target as UIAtlas;

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

            if (mAtlas.material != mat)
            {
                RegisterUndo();
                mAtlas.material = 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);

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

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

                if (coords != mAtlas.coordinates)
                {
                    RegisterUndo();
                    mAtlas.coordinates = coords;
                    mConfirmDelete = false;
                }
            }
        }

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

            if (mSprite == null && mAtlas.sprites.Count > 0)
            {
                mSprite = mAtlas.sprites[0];
            }

            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"))
                        {
                            RegisterUndo();
                            mAtlas.sprites.Remove(mSprite);
                            mConfirmDelete = false;
                        }
                        GUI.backgroundColor = Color.white;
                    }
                    GUILayout.EndHorizontal();
                }
                else mConfirmDelete = false;
            }
            else
            {
                GUI.backgroundColor = Color.green;

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

                    if (GUILayout.Button("New Sprite"))
                    {
                        RegisterUndo();
                        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.sprites.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);
                    }

                    if (mSprite == null) return;

                    // Grab the sprite's inner and outer dimensions
                    Rect inner = mSprite.inner;
                    Rect outer = mSprite.outer;

                    Texture2D tex = mAtlas.material.mainTexture as Texture2D;

                    if (tex != null)
                    {
                        string name = EditorGUILayout.TextField("Edit Name", mSprite.name);

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

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

                            if (!found)
                            {
                                RegisterUndo();
                                mSprite.name = name;
                            }
                        }

                        // 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 l0 = Mathf.RoundToInt(mSprite.paddingLeft	* mSprite.outer.width);
                            int r0 = Mathf.RoundToInt(mSprite.paddingRight	* mSprite.outer.width);
                            int t0 = Mathf.RoundToInt(mSprite.paddingTop	* mSprite.outer.height);
                            int b0 = Mathf.RoundToInt(mSprite.paddingBottom * mSprite.outer.height);

                            int l1 = l0;
                            int r1 = r0;
                            int t1 = t0;
                            int b1 = b0;

                            GUILayout.BeginHorizontal();
                            {
                                GUILayout.Label("Padding");
                                GUILayout.Space(7f);
                                EditorGUIUtility.LookLikeControls(40f);
                                l1 = EditorGUILayout.IntField("Left", l0, GUILayout.MinWidth(40f));
                                r1 = EditorGUILayout.IntField("Right", r0, GUILayout.MinWidth(40f));
                            }
                            GUILayout.EndHorizontal();

                            GUILayout.BeginHorizontal();
                            {
                                GUILayout.Space(60f);
                                EditorGUIUtility.LookLikeControls(40f);
                                t1 = EditorGUILayout.IntField("Top", t0, GUILayout.MinWidth(40f));
                                b1 = EditorGUILayout.IntField("Btm.", b0, GUILayout.MinWidth(40f));
                            }
                            GUILayout.EndHorizontal();

                            if (l0 != l1 || r0 != r1 || t0 != t1 || b0 != b1)
                            {
                                RegisterUndo();
                                mSprite.paddingLeft		= l1 / mSprite.outer.width;
                                mSprite.paddingRight	= r1 / mSprite.outer.width;
                                mSprite.paddingTop		= t1 / mSprite.outer.height;
                                mSprite.paddingBottom	= b1 / mSprite.outer.height;
                                MarkSpriteAsDirty();
                            }
                            EditorGUIUtility.LookLikeControls(80f);
                        }

                        // 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();

                        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.material : 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();
                    }

                    if (GUI.changed)
                    {
                        RegisterUndo();
                        mSprite.outer = outer;
                        mSprite.inner = inner;
                        mConfirmDelete = false;
                    }
                }
            }
        }

        // If something changed, mark the atlas as dirty
        if (mRegisteredUndo) EditorUtility.SetDirty(mAtlas);
    }
Exemplo n.º 18
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;
                }
            }
        }
    }
Exemplo n.º 19
0
    /// <summary>
    /// Update the sprite atlas, keeping only the sprites that are on the specified list.
    /// </summary>
    static void UpdateAtlas(UIAtlas atlas, List<SpriteEntry> sprites)
    {
        if (sprites.Count > 0)
        {
            // Combine all sprites into a single texture and save it
            if (UpdateTexture(atlas, sprites))
            {
                // Replace the sprites within the atlas
                ReplaceSprites(atlas, sprites);
            }

            // Release the temporary textures
            ReleaseSprites(sprites);
            return;
        }
        else
        {
            atlas.spriteList.Clear();
            string path = NGUIEditorTools.GetSaveableTexturePath(atlas);
            atlas.spriteMaterial.mainTexture = null;
            if (!string.IsNullOrEmpty(path)) AssetDatabase.DeleteAsset(path);
        }

        atlas.MarkAsDirty();
        Selection.activeGameObject = (NGUISettings.atlas != null) ? NGUISettings.atlas.gameObject : null;
    }
Exemplo n.º 20
0
    /// <summary>
    /// Replace the sprites within the atlas.
    /// </summary>
    static void ReplaceSprites(UIAtlas atlas, List<SpriteEntry> sprites)
    {
        // Get the list of sprites we'll be updating
        List<UISpriteData> spriteList = atlas.spriteList;
        List<UISpriteData> kept = new List<UISpriteData>();

        // Run through all the textures we added and add them as sprites to the atlas
        for (int i = 0; i < sprites.Count; ++i)
        {
            SpriteEntry se = sprites[i];
            UISpriteData sprite = AddSprite(spriteList, se);
            kept.Add(sprite);
        }

        // Remove unused sprites
        for (int i = spriteList.Count; i > 0; )
        {
            UISpriteData sp = spriteList[--i];
            if (!kept.Contains(sp)) spriteList.RemoveAt(i);
        }

        // Sort the sprites so that they are alphabetical within the atlas
        atlas.SortAlphabetically();
        atlas.MarkAsDirty();
    }