GetSprite() public method

Convenience function that retrieves a sprite by name.
public GetSprite ( string name ) : UISpriteData,
name string
return UISpriteData,
 public bool Validate(UIAtlas atlas)
 {
     if (atlas == null)
     {
         return false;
     }
     if (!this.mIsValid)
     {
         if (string.IsNullOrEmpty(this.spriteName))
         {
             return false;
         }
         this.mSprite = (atlas == null) ? null : atlas.GetSprite(this.spriteName);
         if (this.mSprite != null)
         {
             Texture texture = atlas.texture;
             if (texture == null)
             {
                 this.mSprite = null;
             }
             else
             {
                 this.mUV = new Rect((float) this.mSprite.x, (float) this.mSprite.y, (float) this.mSprite.width, (float) this.mSprite.height);
                 this.mUV = NGUIMath.ConvertToTexCoords(this.mUV, texture.width, texture.height);
                 this.mOffsetX = this.mSprite.paddingLeft;
                 this.mOffsetY = this.mSprite.paddingTop;
                 this.mWidth = this.mSprite.width;
                 this.mHeight = this.mSprite.height;
                 this.mAdvance = this.mSprite.width + (this.mSprite.paddingLeft + this.mSprite.paddingRight);
                 this.mIsValid = true;
             }
         }
     }
     return (this.mSprite != null);
 }
Example #2
0
    public static float[] CalculateSprite(UIAtlas atlas, string name)
    {
        float[] output = new float[6];
        float left;
        float bottom;
        float width;
        float height;
        float UVHeight = 1f;
        float UVWidth = 1f;

        UIAtlas.Sprite sprite = atlas.GetSprite(name);
        if (sprite == null) {
            Debug.LogError("No sprite with that name: " + name);
            return null;
        }
        left = (int)sprite.inner.xMin;
        bottom = (int)sprite.inner.yMax;
        width = (int)sprite.inner.width;
        height = (int)sprite.inner.height;

        float widthHeightRatio = sprite.inner.width / sprite.inner.height;
        if (widthHeightRatio > 1)
            UVHeight = 1f / widthHeightRatio;       // It's a "wide" sprite
        else if (widthHeightRatio < 1)
            UVWidth = 1f * widthHeightRatio;        // It's a "tall" sprite

        output[0] = UVWidth;
        output[1] = UVHeight;
        output[2] = left;
        output[3] = bottom;
        output[4] = width;
        output[5] = height;

        return output;
    }
Example #3
0
 /// <summary>
 /// Add a sprite appropriate for the specified atlas sprite.
 /// It will be a UISlicedSprite if the sprite has an inner rect, and a regular sprite otherwise.
 /// </summary>
 public static UISprite Add(GameObject go, UIAtlas atlas, string spriteName)
 {
     UIAtlas.Sprite sp = (atlas != null) ? atlas.GetSprite(spriteName) : null;
     UISprite sprite = (sp == null || sp.inner == sp.outer) ? NGUITools.AddWidget<UISprite>(go) : (UISprite)NGUITools.AddWidget<UISlicedSprite>(go);
     sprite.atlas = atlas;
     sprite.spriteName = spriteName;
     return sprite;
 }
Example #4
0
	/// <summary>
	/// Validate this symbol, given the specified atlas.
	/// </summary>

	public bool Validate (UIAtlas atlas)
	{
		if (atlas == null) return false;

#if UNITY_EDITOR
		if (!Application.isPlaying || !mIsValid)
#else
		if (!mIsValid)
#endif
		{
			if (string.IsNullOrEmpty(spriteName)) return false;

			mSprite = (atlas != null) ? atlas.GetSprite(spriteName) : null;

			if (mSprite != null)
			{
				Texture tex = atlas.texture;

				if (tex == null)
				{
					mSprite = null;
				}
				else
				{
					Rect outer = mSprite.outer;
					mUV = outer;

					if (atlas.coordinates == UIAtlas.Coordinates.Pixels)
					{
						mUV = NGUIMath.ConvertToTexCoords(mUV, tex.width, tex.height);
					}
					else
					{
						outer = NGUIMath.ConvertToPixels(outer, tex.width, tex.height, true);
					}

					mOffsetX = Mathf.RoundToInt(mSprite.paddingLeft * outer.width);
					mOffsetY = Mathf.RoundToInt(mSprite.paddingTop * outer.width);
					mWidth = Mathf.RoundToInt(outer.width);
					mHeight = Mathf.RoundToInt(outer.height);
					mAdvance = Mathf.RoundToInt(outer.width + (mSprite.paddingRight + mSprite.paddingLeft) * outer.width);
					mIsValid = true;
				}
			}
		}
		return (mSprite != null);
	}
Example #5
0
    void CalculateSprite(UIAtlas atlas, string name)
    {
        UIAtlas.Sprite sprite = atlas.GetSprite(name);
        if (sprite == null) {
            Debug.LogError("No sprite with that name: " + name);
            return;
        }
        left = (int)sprite.inner.xMin;
        bottom = (int)sprite.inner.yMax;
        width = (int)sprite.inner.width;
        height = (int)sprite.inner.height;

        float widthHeightRatio = sprite.inner.width / sprite.inner.height;
        if (widthHeightRatio > 1)
            UVHeight = 1f / widthHeightRatio;       // It's a "wide" sprite
        else if (widthHeightRatio < 1)
            UVWidth = 1f * widthHeightRatio;        // It's a "tall" sprite
    }
Example #6
0
    /// <summary>
    /// Draw a sprite selection field.
    /// </summary>

    static public void SpriteField(string fieldName, string caption, UIAtlas atlas, string spriteName, SpriteSelector.Callback callback)
    {
        GUILayout.BeginHorizontal();
        GUILayout.Label(fieldName, GUILayout.Width(76f));

        if (atlas.GetSprite(spriteName) == null)
        {
            spriteName = "";
        }

        if (GUILayout.Button(spriteName, "MiniPullDown", GUILayout.Width(120f)))
        {
            SpriteSelector.Show(atlas, spriteName, callback);
        }

        if (!string.IsNullOrEmpty(caption))
        {
            GUILayout.Space(20f);
            GUILayout.Label(caption);
        }
        GUILayout.EndHorizontal();
    }
Example #7
0
    public void run(int id)
    {
        names.Clear();

        int index = 1;

        do
        {
            string       name = "fc" + (id * 100 + index);
            UISpriteData sp   = atlas.GetSprite(name);

            if (sp == null)
            {
                break;
            }

            Debug.Log("add " + name);
            names.Add(name);
            index++;
        } while (true);

        int cnt = names.Count;

        if (cnt == 0)
        {
            Debug.Log("names null");
            reset();
            return;
        }

        float now = Time.time;

        current           = 0;
        sprite.spriteName = names[0];
        rate     = 1.0f / cnt;
        nextFire = now + rate;
        endtime  = now + 3.0f;
        gameObject.SetActive(true);
    }
Example #8
0
    /// <summary>
    /// Retrieve the atlas sprite referenced by the spriteName field.
    /// </summary>

    public UISpriteData GetAtlasSprite()
    {
        if (!mSpriteSet)
        {
            mSprite = null;
        }

        if (mSprite == null && mAtlas != null)
        {
            if (!string.IsNullOrEmpty(mSpriteName))
            {
                UISpriteData sp = mAtlas.GetSprite(mSpriteName);
                if (sp == null)
                {
                    return(null);
                }
                SetAtlasSprite(sp);
            }

            if (mSprite == null && mAtlas.spriteList.Count > 0)
            {
                UISpriteData sp = mAtlas.spriteList[0];
                if (sp == null)
                {
                    return(null);
                }
                SetAtlasSprite(sp);

                if (mSprite == null)
                {
                    Debug.LogError(mAtlas.name + " seems to have a null sprite!");
                    return(null);
                }
                mSpriteName = mSprite.name;
            }
        }
        return(mSprite);
    }
Example #9
0
        internal static string getSprite(UIAtlas atlas, string uispriteName, ExportPreset preset)
        {
            Material  atlasMaterial = atlas.spriteMaterial;
            Texture2D texture2D     = (Texture2D)atlasMaterial.GetTexture("_MainTex");
            string    res           = null;

            if (texture2D != null)
            {
                string picturePath = AssetDatabase.GetAssetPath(texture2D.GetInstanceID());
                //Texture2D copyTexture = DuplicateTexture(texture2D);

                //picturePath = path.Split('.')[0] + ".png";
                string texturePath = new WXTexture(texture2D).Export(preset);

                JSONObject metadata = TextureUtil.getMeta(texture2D);

                BetterList <string> allSpriteList = atlas.GetListOfSprites();
                string[]            list          = allSpriteList.ToArray();
                foreach (string spriteName in list)
                {
                    UISpriteData  currentData     = atlas.GetSprite(spriteName);
                    WXSpriteFrame spriteConverter = new WXSpriteFrame(currentData, texturePath, picturePath);
                    string        uuid            = spriteConverter.Export(preset);
                    // 这里不需要了,外部去给hierarchyContext addResource就好
                    if (spriteName == uispriteName)
                    {
                        res = uuid;
                        return(res);
                        //    WXBeefBallExportContext.addDep(uuid);
                    }
                    //else
                    //{
                    //    WXBeefBallExportContext.addNoDependence(uuid);
                    //}
                }
            }
            return(res);
        }
Example #10
0
    void SpriteAnim(int index)
    {
        string spriteName = spritesList[index];

        sprite.spriteName = spriteName;

        if (needSnap)
        {
            var spriteData = atlas.GetSprite(spriteName);
            sprite.width  = spriteData.width;
            sprite.height = spriteData.height;
        }

        if (needRotate)
        {
            sprite.transform.rotation = Quaternion.Euler(spriteRotation[index]);
        }

        if (needTranslate)
        {
            sprite.transform.localPosition = spriteDicection[index];
        }
    }
Example #11
0
    public ReplaceUISpriteTreeElement(GameObject go, string path, string name, int depth, int id) : base(name, depth, id)
    {
        if (go != null)
        {
            this.Go        = go;
            GameObjectName = go.name;
            UISprite sprite = go.GetComponent <UISprite>();
            if (sprite != null)
            {
                AtlasStr   = sprite.atlas.name;
                Atlas      = sprite.atlas;
                SpriteName = sprite.spriteName;
                SpriteData = (Atlas != null) ? Atlas.GetSprite(NGUISettings.selectedSprite) : null;
                if (SpriteData != null)
                {
                    SpriteTexture = Atlas.texture as Texture2D;
                }
            }
        }

        Path    = path;
        Replace = true;
    }
Example #12
0
    /// <summary>
    /// Convenience function that retrieves a sprite by name.
    /// </summary>

    public Sprite GetSprite(string name)
    {
        if (mReplacement != null)
        {
            return(mReplacement.GetSprite(name));
        }
        else if (!string.IsNullOrEmpty(name))
        {
            foreach (Sprite s in sprites)
            {
                // string.Equals doesn't seem to work with Flash export
                if (!string.IsNullOrEmpty(s.name) && name == s.name)
                {
                    return(s);
                }
            }
        }
        else
        {
            Debug.LogWarning("Expected a valid name, found nothing");
        }
        return(null);
    }
Example #13
0
    /// <summary>
    /// Retrieve the atlas sprite referenced by the spriteName field.
    /// </summary>

    public UISpriteData GetAtlasSprite()
    {
        if (!mSpriteSet)
        {
            mSprite = null;
        }

        if (mSprite == null && mAtlas != null)
        {
            if (!string.IsNullOrEmpty(mSpriteName))
            {
                UISpriteData sp = mAtlas.GetSprite(mSpriteName);
                if (sp == null)
                {
                    return(null);
                }
                SetAtlasSprite(sp);
            }

            // jwu - sprite refrences get removed when dynamically loading atlases

            /*if (mSprite == null && mAtlas.spriteList.Count > 0)
             * {
             *      UISpriteData sp = mAtlas.spriteList[0];
             *      if (sp == null) return null;
             *      SetAtlasSprite(sp);
             *
             *      if (mSprite == null)
             *      {
             *              Debug.LogError(mAtlas.name + " seems to have a null sprite!");
             *              return null;
             *      }
             *      mSpriteName = mSprite.name;
             * }*/
        }
        return(mSprite);
    }
Example #14
0
	/// <summary>
	/// Validate this symbol, given the specified atlas.
	/// </summary>

	public bool Validate (UIAtlas atlas)
	{
		if (atlas == null) return false;

#if UNITY_EDITOR
		if (!Application.isPlaying || !mIsValid)
#else
		if (!mIsValid)
#endif
		{
			if (string.IsNullOrEmpty(spriteName)) return false;

			mSprite = (atlas != null) ? atlas.GetSprite(spriteName) : null;

			if (mSprite != null)
			{
				Texture tex = atlas.texture;

				if (tex == null)
				{
					mSprite = null;
				}
				else
				{
					mUV = new Rect(mSprite.x, mSprite.y, mSprite.width, mSprite.height);
					mUV = NGUIMath.ConvertToTexCoords(mUV, tex.width, tex.height);
					mOffsetX = mSprite.paddingLeft;
					mOffsetY = mSprite.paddingTop;
					mWidth = mSprite.width;
					mHeight = mSprite.height;
					mAdvance = mSprite.width + (mSprite.paddingLeft + mSprite.paddingRight);
					mIsValid = true;
				}
			}
		}
		return (mSprite != null);
	}
Example #15
0
    Dictionary <string, int> dictStr2Idx = new Dictionary <string, int>();  // add by thomasmeng
    public Sprite GetSprite(string name)
    {
        if (mReplacement != null)
        {
            return(mReplacement.GetSprite(name));
        }
        else if (!string.IsNullOrEmpty(name))
        {
            //add by thomas start
            int idx = 0;
            if (dictStr2Idx.TryGetValue(name, out idx))
            {
                if (idx < sprites.Count && !string.IsNullOrEmpty(sprites[idx].name) && name == sprites[idx].name)
                {
                    return(sprites[idx]);
                }
            }
            //add by thomas end

            for (int i = 0, imax = sprites.Count; i < imax; ++i)
            {
                Sprite s = sprites[i];

                // string.Equals doesn't seem to work with Flash export
                if (!string.IsNullOrEmpty(s.name) && name == s.name)
                {
                    if (dictStr2Idx.ContainsKey(name) == false)
                    {
                        dictStr2Idx.Add(name, i);
                    }
                    return(s);
                }
            }
        }
        return(null);
    }
Example #16
0
 public UISpriteData GetSprite(string name)
 {
     if (mReplacement != null)
     {
         return(mReplacement.GetSprite(name));
     }
     if (!string.IsNullOrEmpty(name))
     {
         if (mSprites.Count == 0)
         {
             Upgrade();
         }
         int i = 0;
         for (int count = mSprites.Count; i < count; i++)
         {
             UISpriteData uISpriteData = mSprites[i];
             if (!string.IsNullOrEmpty(uISpriteData.name) && name == uISpriteData.name)
             {
                 return(uISpriteData);
             }
         }
     }
     return(null);
 }
Example #17
0
    /// <summary>
    /// Draw a sprite selection field.
    /// </summary>

    static public void DrawSpriteField(string label, string caption, UIAtlas atlas, string spriteName, SpriteSelector.Callback callback, params GUILayoutOption[] options)
    {
        GUILayout.BeginHorizontal();
        GUILayout.Label(label, GUILayout.Width(76f));

        if (atlas.GetSprite(spriteName) == null)
        {
            spriteName = "";
        }

        if (GUILayout.Button(spriteName, "MiniPullDown", options))
        {
            NGUISettings.atlas          = atlas;
            NGUISettings.selectedSprite = spriteName;
            SpriteSelector.Show(callback);
        }

        if (!string.IsNullOrEmpty(caption))
        {
            GUILayout.Space(20f);
            GUILayout.Label(caption);
        }
        GUILayout.EndHorizontal();
    }
Example #18
0
 public UIAtlas AddPreviewSprite(UISprite uiSprite, string spriteName, bool isDrag)
 {
     if (null == uiSprite)
     {
         Debug.LogError("uiSprite is null");
         return(null);
     }
     GetPreviewAtlas();
     if ((mPreviewAtlas != null) && (mPreviewAtlas.GetSprite(spriteName) == null))
     {
         UISpriteData item = new UISpriteData
         {
             name          = "preview",
             paddingLeft   = 0,
             paddingRight  = 0,
             paddingBottom = 0,
             paddingTop    = 0
         };
         item.SetRect(0, 0, mTargetSprite.width, mTargetSprite.height);
         mPreviewAtlas.spriteList.Clear();
         mPreviewAtlas.spriteList.Add(item);
     }
     return(mPreviewAtlas);
 }
Example #19
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;
					}
				}
			}
		}
	}
Example #20
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;
                    }
                }
            }
        }
    }
Example #21
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);
    }
Example #22
0
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

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

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

        GUILayout.Space(6f);

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

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

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

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

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

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

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

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

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

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

            mAtlas.MarkAsChanged();
        }

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

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

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

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

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

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

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

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

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

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

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


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

                    NGUIEditorTools.BeginContents();

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

                    GUI.changed = false;

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

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

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

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

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

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

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

                        MarkSpriteAsDirty();
                    }

                    GUILayout.Space(3f);

                    GUILayout.BeginHorizontal();

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

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

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

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

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

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

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

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

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

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

                    NGUIEditorTools.EndContents();
                }

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

                    if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name))
                    {
                        NGUIEditorTools.SelectPrevious();
                    }
                    GUI.backgroundColor = Color.white;
                }
            }
        }
    }
    public void createTile(TileWindow tileWindow, MapTileAdvance tile)
    {
        // 只有真正要使用该地砖时才解析
        if (!tile.mParsed)
        {
            tile.parseTile();
        }
        int    x          = tileIndexToTileX(tile.mIndex);
        int    y          = tileIndexToTileY(tile.mIndex);
        string tileSuffix = x + "_" + y;

        // 偶数行偶数列才渲染大地砖
        if (tileWindow.mBackTile == null && x % 2 == 0 && y % 2 == 0)
        {
            // 大地砖图集
            UIAtlas bngAtlas = getBackTileAtlas(tile);
            // 大地砖图片窗口
            string bngSpriteName = "" + (tile.mBngImgIdx - 1);
            if (bngAtlas != null && bngAtlas.GetSprite(bngSpriteName) != null)
            {
                if (!mBackAtlasList.ContainsKey(tile.mBngAtlasIndex))
                {
                    mBackAtlasList.Add(tile.mBngAtlasIndex, new List <int>());
                }
                // 一个panel中只允许有一个Drawcall
                // 一个panel满时,就应该放入新的panel中
                // 查找该图集存放在哪些panel中
                int        panelIndex     = -1;
                List <int> panelIndexList = mBackAtlasList[tile.mBngAtlasIndex];
                int        panelCount     = panelIndexList.Count;
                for (int j = 0; j < panelCount; ++j)
                {
                    // 找到一个包含该图集,且数量未达上限的panel
                    if (mBackPanelList[panelIndexList[j]].mObjectCount < MAX_WINDOW_PER_PANEL)
                    {
                        panelIndex = panelIndexList[j];
                        break;
                    }
                }
                // 没有找到合适的panel,则创建新的panel
                if (panelIndex == -1)
                {
                    panelIndex = GameUtility.makeID();
                    PanelInfo panelInfo = new PanelInfo();
                    panelInfo.mPanel       = createObject <txNGUIPanel>(mBackRoot, "BackPanel" + panelIndex);
                    panelInfo.mObjectCount = 0;
                    panelInfo.mAtlasIndex  = tile.mBngAtlasIndex;
                    mBackPanelList.Add(panelIndex, panelInfo);
                    mBackAtlasList[tile.mBngAtlasIndex].Add(panelIndex);
                }
                // panel中的窗口计数+1
                mBackPanelList[panelIndex].mObjectCount += 1;
                tileWindow.mBackTile = mWindowPool.createWindow <txNGUISprite>("back_" + tileSuffix, mBackPanelList[panelIndex].mPanel);
                tileWindow.mBackTile.setAtlas(bngAtlas);
                tileWindow.mBackTile.setSpriteName(bngSpriteName, true);
                Vector2 posOffset = tileWindow.mBackTile.getSpriteSize() / 2.0f;
                posOffset += new Vector2(48 * x - mHalfMap.x, mHalfMap.y - 32 * y - 32);
                tileWindow.mBackTile.setLocalPosition(posOffset);
                tileWindow.mBackTile.setDepth(1);
                if (tile.mHasBng)
                {
                    tileWindow.mBackTile.getSprite().color = new Color(0.5f, 0.0f, 0.0f);
                }
                else
                {
                    tileWindow.mBackTile.getSprite().color = Color.white;
                }
            }
        }
        if (tileWindow.mMidTile == null)
        {
            // 小地砖图片窗口
            string midSpriteName = "" + (tile.mMidImgIdx - 1);
            if (midAtlas.GetSprite(midSpriteName) != null)
            {
                tileWindow.mMidTile = mWindowPool.createWindow <txNGUISprite>("mid_" + tileSuffix, mMiddleRoot);
                tileWindow.mMidTile.setAtlas(midAtlas);
                tileWindow.mMidTile.setSpriteName(midSpriteName, true);
                Vector2 posOffset = tileWindow.mMidTile.getSpriteSize() / 2.0f;
                posOffset += new Vector2(48 * x - mHalfMap.x, mHalfMap.y - 32 * y);
                tileWindow.mMidTile.setLocalPosition(posOffset);
            }
        }
        if (tileWindow.mObjectTile == null)
        {
            if (!tile.mHasAni)
            {
                // 加载对象图集
                UIAtlas atlas = getObjTileAtlas(tile);
                // 创建对象图片窗口
                string spriteName = "" + (tile.mObjImgIdx - 1);
                if (atlas != null && atlas.GetSprite(spriteName) != null)
                {
                    tileWindow.mObjectTile = mWindowPool.createWindow <txNGUISprite>("obj_" + tileSuffix, mObjRoot);
                    txNGUISprite objTile = tileWindow.mObjectTile;
                    objTile.setAtlas(atlas);
                    objTile.setSpriteName(spriteName, true);
                    Vector2 posOffset = objTile.getSpriteSize() / 2.0f;
                    posOffset += new Vector2(48 * x - mHalfMap.x, mHalfMap.y - 32 * y);
                    objTile.setLocalPosition(posOffset);
                    objTile.setDepth(y);
                    if (!tile.mCanWalk)
                    {
                        BoxCollider collider = objTile.getBoxCollider(true);
                        collider.size = objTile.getSpriteSize();
                    }
                    objTile.setHandleInput(!tile.mCanWalk);
                    if (tile.mHasObj)
                    {
                        objTile.getSprite().color = new Color(0.5f, 0.0f, 0.0f);
                    }
                    else
                    {
                        objTile.getSprite().color = Color.white;
                    }
                }
            }
        }
        if (tileWindow.mEffect == null)
        {
            if (tile.mHasAni)
            {
                // 加载对象图集
                UIAtlas atlas = getEffectAtlas(tile);
                // 创建对象图片窗口
                if (atlas != null)
                {
                    tileWindow.mEffect = mWindowPool.createWindow <txNGUISpriteAnim>("effect_" + tileSuffix, mObjRoot);
                    tileWindow.mEffect.setAtlas(atlas);
                    tileWindow.mEffect.setTextureSet(intToString(tile.mObjImgIdx - 1));
                    tileWindow.mEffect.play();
                    tileWindow.mEffect.setLoop(LOOP_MODE.LM_LOOP);
                    tileWindow.mEffect.setSpeed(0.5f);
                    Vector2 posOffset = new Vector2(0.0f, tileWindow.mEffect.getSpriteSize().y * 1.5f);
                    posOffset += new Vector2(48 * x - mHalfMap.x, mHalfMap.y - 32 * y);
                    tileWindow.mEffect.setLocalPosition(posOffset);
                    tileWindow.mEffect.setDepth(y);
                }
            }
        }
    }
    /// <summary>
    /// Convenience function that displays a list of sprites and returns the selected value.
    /// </summary>

    static public void AdvancedSpriteField(UIAtlas atlas, string spriteName, SpriteSelector.Callback callback, bool editable,
                                           params GUILayoutOption[] options)
    {
        // Give the user a warning if there are no sprites in the atlas
        if (atlas.spriteList.Count == 0)
        {
            EditorGUILayout.HelpBox("No sprites found", MessageType.Warning);
            return;
        }

        // Sprite selection drop-down list
        GUILayout.BeginHorizontal();
        {
            if (GUILayout.Button("Sprite", "DropDownButton", GUILayout.Width(76f)))
            {
                SpriteSelector.Show(atlas, spriteName, callback);
            }

            if (editable)
            {
                if (!string.Equals(spriteName, mLastSprite))
                {
                    mLastSprite = spriteName;
                    mEditedName = null;
                }

                string newName = GUILayout.TextField(string.IsNullOrEmpty(mEditedName) ? spriteName : mEditedName);

                if (newName != spriteName)
                {
                    mEditedName = newName;

                    if (GUILayout.Button("Rename", GUILayout.Width(60f)))
                    {
                        UIAtlas.Sprite sprite = atlas.GetSprite(spriteName);

                        if (sprite != null)
                        {
                            NGUIEditorTools.RegisterUndo("Edit Sprite Name", atlas);
                            sprite.name = newName;

                            List <UISprite> sprites = FindAll <UISprite>();

                            for (int i = 0; i < sprites.Count; ++i)
                            {
                                UISprite sp = sprites[i];

                                if (sp.atlas == atlas && sp.spriteName == spriteName)
                                {
                                    NGUIEditorTools.RegisterUndo("Edit Sprite Name", sp);
                                    sp.spriteName = newName;
                                }
                            }

                            mLastSprite = newName;
                            spriteName  = newName;
                            mEditedName = null;

                            NGUISettings.selectedSprite = spriteName;
                        }
                    }
                }
            }
            else
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label(spriteName, "HelpBox", GUILayout.Height(18f));
                GUILayout.Space(18f);
                GUILayout.EndHorizontal();

                if (GUILayout.Button("Edit", GUILayout.Width(40f)))
                {
                    NGUISettings.selectedSprite = spriteName;
                    Select(atlas.gameObject);
                }
            }
        }
        GUILayout.EndHorizontal();
    }
Example #25
0
    /// <summary>
    /// Convenience function that retrieves a sprite by name.
    /// </summary>

    public UISpriteData GetSprite(string name)
    {
        if (mReplacement != null)
        {
            return(mReplacement.GetSprite(name));
        }
        else if (!string.IsNullOrEmpty(name))
        {
            if (mSprites.Count == 0)
            {
                Upgrade();
            }
            if (mSprites.Count == 0)
            {
                return(null);
            }

            // O(1) lookup via a dictionary
#if UNITY_EDITOR
            if (Application.isPlaying)
#endif
            {
                // The number of indices differs from the sprite list? Rebuild the indices.
                if (mSpriteIndices.Count != mSprites.Count)
                {
                    MarkSpriteListAsChanged();
                }

                int index;
                if (mSpriteIndices.TryGetValue(name, out index))
                {
                    // If the sprite is present, return it as-is
                    if (index > -1 && index < mSprites.Count)
                    {
                        return(mSprites[index]);
                    }

                    // The sprite index was out of range -- perhaps the sprite was removed? Rebuild the indices.
                    MarkSpriteListAsChanged();

                    // Try to look up the index again
                    return(mSpriteIndices.TryGetValue(name, out index) ? mSprites[index] : null);
                }
            }

            // Sequential O(N) lookup.
            for (int i = 0, imax = mSprites.Count; i < imax; ++i)
            {
                UISpriteData s = mSprites[i];

                // string.Equals doesn't seem to work with Flash export
                if (!string.IsNullOrEmpty(s.name) && name == s.name)
                {
#if UNITY_EDITOR
                    if (!Application.isPlaying)
                    {
                        return(s);
                    }
#endif
                    // If this point was reached then the sprite is present in the non-indexed list,
                    // so the sprite indices should be updated.
                    MarkSpriteListAsChanged();
                    return(s);
                }
            }
        }
        return(null);
    }
Example #26
0
    /// <summary>
    /// Validate this symbol, given the specified atlas.
    /// </summary>
    public bool Validate(UIAtlas atlas)
    {
        if (atlas == null) return false;

        #if UNITY_EDITOR
        if (!Application.isPlaying || mSprite == null)
        #else
        if (mSprite == null)
        #endif
        {
            if (string.IsNullOrEmpty(spriteName)) return false;

            mSprite = (atlas != null) ? atlas.GetSprite(spriteName) : null;

            if (mSprite != null)
            {
                Texture tex = atlas.texture;

                if (tex == null)
                {
                    mSprite = null;
                }
                else
                {
                    Rect inner = mSprite.inner;
                    Rect outer = mSprite.outer;
                    mUV = outer;

                    if (atlas.coordinates == UIAtlas.Coordinates.Pixels)
                    {
                        mUV = NGUIMath.ConvertToTexCoords(mUV, tex.width, tex.height);
                    }
                    else
                    {
                        inner = NGUIMath.ConvertToPixels(inner, tex.width, tex.height, true);
                        outer = NGUIMath.ConvertToPixels(outer, tex.width, tex.height, true);
                    }

                    mOffsetX = Mathf.RoundToInt(outer.x - inner.x);
                    mOffsetY = Mathf.RoundToInt(outer.y - inner.y);
                    mOuterWidth = Mathf.RoundToInt(outer.width);
                    mOuterHeight = Mathf.RoundToInt(outer.height);
                    mInnerWidth = Mathf.RoundToInt(inner.width);
                }
            }
        }
        return (mSprite != null);
    }
Example #27
0
        private void SetIconSpriteName(UISprite itemIcon, UILabel labNum, int itemIndex)
        {
            string  spriteName = "100000";
            UIAtlas atlas      = Singleton <AtlasManager> .Instance.GetAtlas("PropIcon");

            string  borderSpriteName = "pz_1";
            UIAtlas borderAtlas      = Singleton <AtlasManager> .Instance.GetAtlas("common");

            uint goodsId = Singleton <LuckDrawMode> .Instance.RewardList[itemIndex].id;
            byte num     = Singleton <LuckDrawMode> .Instance.RewardList[itemIndex].num;

            if (goodsId > 100000)              //物品道具
            {
                borderAtlas = Singleton <AtlasManager> .Instance.GetAtlas("common");

                SysItemVo vo = BaseDataMgr.instance.GetDataById <SysItemVo>(goodsId);

                if (null != vo)
                {
                    spriteName = string.Empty + vo.icon;
                    atlas      = Singleton <AtlasManager> .Instance.GetAtlas("GemIcon");

                    borderSpriteName = "pz_" + vo.color;

                    if (atlas.GetSprite(spriteName) == null)
                    {
                        if (vo.type == GoodsConst.SMELT_GOODS)
                        {
                            atlas = Singleton <AtlasManager> .Instance.GetAtlas("GemIcon");
                        }
                        else
                        {
                            atlas = Singleton <AtlasManager> .Instance.GetAtlas("PropIcon");
                        }

                        if (atlas == null || atlas.GetSprite(spriteName) == null)
                        {
                            spriteName = "100000";
                        }
                    }
                }
            }
            else              //装备
            {
                borderAtlas = Singleton <AtlasManager> .Instance.GetAtlas("EquipIcon");

                SysEquipVo vo = BaseDataMgr.instance.GetDataById <SysEquipVo>(goodsId);

                if (null != vo)
                {
                    spriteName       = string.Empty + vo.icon;
                    borderSpriteName = "pz_" + vo.color;

                    atlas = Singleton <AtlasManager> .Instance.GetAtlas("EquipIcon");

                    if (atlas.GetSprite(spriteName) == null)
                    {
                        if (vo.type == GoodsConst.SMELT_GOODS)
                        {
                            atlas = Singleton <AtlasManager> .Instance.GetAtlas("GemIcon");
                        }
                        else
                        {
                            atlas = Singleton <AtlasManager> .Instance.GetAtlas("PropIcon");
                        }

                        if (atlas == null || atlas.GetSprite(spriteName) == null)
                        {
                            spriteName = "100000";
                        }
                    }
                }
            }

            itemIcon.atlas      = atlas;
            itemIcon.spriteName = spriteName;

            UISprite sprBorder = itemIcon.transform.parent.FindChild("border").GetComponent <UISprite>();

            sprBorder.atlas      = borderAtlas;
            sprBorder.spriteName = borderSpriteName;
            labNum.text          = num.ToString();
        }
Example #28
0
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

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

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

        GUILayout.Space(6f);

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

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

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

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

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

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

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

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

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

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

            mAtlas.MarkAsChanged();
        }

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

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

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

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

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

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

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

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

                Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D;

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

                    NGUIEditorTools.BeginContents();

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

                    GUI.changed = false;

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

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

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

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

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

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

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

                        MarkSpriteAsDirty();
                    }

                    GUILayout.Space(3f);

                    GUILayout.BeginHorizontal();

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

                    if (GUILayout.Button("Save As..."))
                    {
                        string path = EditorUtility.SaveFilePanel("Save As",
                                                                  NGUISettings.currentPath, sprite.name + ".png", "png");

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

                            if (se != null)
                            {
                                byte[] bytes = se.tex.EncodeToPNG();
                                File.WriteAllBytes(path, bytes);
                                AssetDatabase.ImportAsset(path);
                                if (se.temporaryTexture)
                                {
                                    DestroyImmediate(se.tex);
                                }
                            }
                        }
                    }
                    if (GUILayout.Button("Save As All"))
                    {
                        string path = EditorUtility.SaveFolderPanel("Save As All",
                                                                    NGUISettings.currentPath, "");
                        if (!string.IsNullOrEmpty(path))
                        {
                            var list = mAtlas.spriteList;
                            foreach (var uiSpriteData in list)
                            {
                                var spName = uiSpriteData.name;
                                NGUISettings.currentPath = path;
                                UIAtlasMaker.SpriteEntry se = UIAtlasMaker.ExtractSprite(mAtlas, spName);
                                if (se != null)
                                {
                                    var    filePath = string.Format("{0}/{1}.png", path, spName);
                                    byte[] bytes    = se.tex.EncodeToPNG();
                                    File.WriteAllBytes(filePath, bytes);
                                    AssetDatabase.ImportAsset(filePath);
                                    if (se.temporaryTexture)
                                    {
                                        DestroyImmediate(se.tex);
                                    }
                                }
                            }
                        }
                    }
                    GUILayout.EndHorizontal();
                    NGUIEditorTools.EndContents();
                }

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

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

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

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

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

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

                    NGUIEditorTools.EndContents();
                }

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

                    if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name))
                    {
                        NGUIEditorTools.SelectPrevious();
                    }
                    GUI.backgroundColor = Color.white;
                }
            }
        }
    }
Example #29
0
	/// <summary>
	/// Extract the specified sprite from the atlas.
	/// </summary>

	static public SpriteEntry ExtractSprite (UIAtlas atlas, string spriteName)
	{
		if (atlas.texture == null) return null;
		UISpriteData sd = atlas.GetSprite(spriteName);
		if (sd == null) return null;

		Texture2D tex = NGUIEditorTools.ImportTexture(atlas.texture, true, true, false);
		SpriteEntry se = ExtractSprite(sd, tex);
		NGUIEditorTools.ImportTexture(atlas.texture, false, false, !atlas.premultipliedAlpha);
		return se;
	}
        void UpdateResources()
        {
            string texturefile = null;

            switch (m_NodeType)
            {
            case enumNodeType.ENodeType_Fill:    //实心死路
                texturefile = "tile2-hd";
                break;

            case enumNodeType.ENodeType_NoWay:    //空心无通路
                if (m_OldNodeType != m_NodeType)
                {
                    System.Random rd = new System.Random();
                    texturefile = (rd.Next(2) == 1) ? "tile3-hd" : "tile4-hd";
                }
                break;

            case enumNodeType.ENodeType_1WayRight:    //右侧单通路
                texturefile = "tile5-hd";
                break;

            case enumNodeType.ENodeType_1WayLeft:    //左侧单通路
                texturefile = "tile6-hd";
                break;

            case enumNodeType.ENodeType_1WayUp:    //上侧单通路
                texturefile = "tile7-hd";
                break;

            case enumNodeType.ENodeType_1WayDown:    //下侧单通路
                texturefile = "tile8-hd";
                break;

            case enumNodeType.ENodeType_2WayUpDown:    //上下侧双通路
                texturefile = "tile9-hd";
                break;

            case enumNodeType.ENodeType_2WayLeftRight:    //左右侧双通路
                texturefile = "tile10-hd";
                break;

            case enumNodeType.ENodeType_2WayDownRight:    //下右侧双通路
                texturefile = "tile11-hd";
                break;

            case enumNodeType.ENodeType_2WayDownLeft:    //下左侧双通路
                texturefile = "tile12-hd";
                break;

            case enumNodeType.ENodeType_2WayTopRight:    //上右侧双通路
                texturefile = "tile13-hd";
                break;

            case enumNodeType.ENodeType_2WayTopLeft:    //下左侧双通路
                texturefile = "tile14-hd";
                break;

            case enumNodeType.ENodeType_3WayRight:    //右侧3通路
                texturefile = "tile15-hd";
                break;

            case enumNodeType.ENodeType_3WayLeft:    //左3通路
                texturefile = "tile16-hd";
                break;

            case enumNodeType.ENodeType_3WayDown:    //下侧3通路
                texturefile = "tile17-hd";
                break;

            case enumNodeType.ENodeType_3WayUp:    //上侧3通路
                texturefile = "tile18-hd";
                break;

            default:
                break;
            }
            if (!string.IsNullOrEmpty(texturefile))
            {
                m_BackImage.sprite = UIAtlas.GetSprite(ownerMap.mapData.MapAtlas, texturefile);
                m_BackImage.SetNativeSize();
                m_BackImage.gameObject.SetActive(true);
            }
            else if (enumNodeType.ENodeType_None == m_NodeType)
            {
                //m_BackImage.setIsVisible(false);
                m_BackImage.gameObject.SetActive(false);
            }
        }
Example #31
0
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

    void OnGUI()
    {
        EditorGUIUtility.LookLikeControls(80f);

        if (mAtlas == null)
        {
            GUILayout.Label("No Atlas selected.", "LODLevelNotifyText");
        }
        else
        {
            GUILayout.Label(mAtlas.name + " Sprites", "LODLevelNotifyText");
            NGUIEditorTools.DrawSeparator();

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

            string before = NGUISettings.partialSprite;
            string after  = EditorGUILayout.TextField("", before, "SearchTextField");
            NGUISettings.partialSprite = after;

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

            BetterList <string> sprites = mAtlas.GetListOfSprites(NGUISettings.partialSprite);
            Texture2D           tex     = mAtlas.texture as Texture2D;

            float size    = 80f;
            float padded  = size + 10f;
            int   columns = Mathf.FloorToInt(Screen.width / padded);
            if (columns < 1)
            {
                columns = 1;
            }

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

            GUILayout.Space(10f);
            mPos = GUILayout.BeginScrollView(mPos);

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

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

                        // Button comes first
                        if (GUI.Button(rect, "") && spriteName != sprite.name)
                        {
                            if (mSprite != null)
                            {
                                NGUIEditorTools.RegisterUndo("Atlas Selection", mSprite);
                                mSprite.spriteName = sprite.name;
                                mSprite.MakePixelPerfect();
                                EditorUtility.SetDirty(mSprite.gameObject);
                            }
                            else if (mCallback != null)
                            {
                                mName = sprite.name;
                                mCallback(sprite.name);
                            }
                        }

                        // On top of the button we have a checkboard grid
                        NGUIEditorTools.DrawTiledTexture(rect, NGUIEditorTools.backdropTexture);

                        Rect uv = sprite.outer;
                        if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels)
                        {
                            uv = NGUIMath.ConvertToTexCoords(uv, tex.width, tex.height);
                        }

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

                        // Stretch the sprite so that it will appear proper
                        float aspect = scaleY / scaleX;
                        if (aspect < 1f)
                        {
                            scaleX *= aspect;
                        }
                        else
                        {
                            scaleY /= aspect;
                        }

                        Rect clipRect = rect;

                        if (aspect != 1f)
                        {
                            if (aspect < 1f)
                            {
                                // The sprite is taller than it is wider
                                float padding = size * (1f - aspect) * 0.5f;
                                clipRect.xMin += padding;
                                clipRect.xMax -= padding;
                            }
                            else
                            {
                                // The sprite is wider than it is taller
                                float padding = size * (1f - 1f / aspect) * 0.5f;
                                clipRect.yMin += padding;
                                clipRect.yMax -= padding;
                            }
                        }

                        GUI.DrawTextureWithTexCoords(clipRect, tex, uv);

                        // Draw the selection
                        if (spriteName == sprite.name)
                        {
                            NGUIEditorTools.DrawOutline(rect, new Color(0.4f, 1f, 0f, 1f));
                        }

                        if (++col >= columns)
                        {
                            ++offset;
                            break;
                        }
                        rect.x += padded;
                    }
                }
                GUILayout.EndHorizontal();
                GUILayout.Space(padded);
                rect.y += padded;
            }
            GUILayout.EndScrollView();
        }
    }
Example #32
0
 void UpdateItem()
 {
     _lock.SetActive(_locked);
     _img.sprite = UIAtlas.GetSprite("Main", _data.Icon);
     _text.text  = Helper.GetLanguage(_data.LanguageId);
 }
    private NvUIImage buildAnImage( UIAtlas atlas, string spriteName )
    {
        if ( atlas != null )
        {
            UIAtlas.Sprite sprite = atlas.GetSprite( spriteName );

            if ( sprite != null )
            {

                Rect rc = sprite.outer;

                GameObject obj = new GameObject("ImageSlot (" + spriteName + ")");
                obj.transform.parent = transform;
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localRotation = Quaternion.identity;
                obj.transform.localScale = new Vector3( rc.width, rc.height, 1.0f );
                obj.layer = gameObject.layer;

                UISprite spr = obj.AddComponent<UISprite>();
                spr.atlas = atlas;

                NvUIImage image = obj.AddComponent<NvUIImage>();
                image.spriteName = spriteName;
                return image;
            }
        }
        return null;
    }
Example #34
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;
                }
            }
        }
    }
    /// <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();
                    }
                }
            }
        }
    }
Example #36
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);
    }
Example #37
0
    private void setupKey()
    {
        //----------------------------------------
        // プレイヤー情報に持っているクエストキーリストを取得
        // ※サーバーで有効期限の近い順でソートしてくれている
        //----------------------------------------
        PacketStructQuestKey[] cKeyList = UserDataAdmin.Instance.m_StructPlayer.quest_key_list;

        //--------------------------------
        // ぬるチェック
        //--------------------------------
        if (cKeyList == null ||
            cKeyList.Length <= 0)
        {
            return;
        }

        //--------------------------------
        // 所持キーリストをアイテムリストに追加
        // ※クエストキーはサーバーでソートしてくれてるのでそのまま追加
        //--------------------------------
        for (int iKey = 0; iKey < cKeyList.Length; iKey++)
        {
            if (cKeyList[iKey] == null ||
                cKeyList[iKey].quest_key_cnt <= 0)
            {
                continue;
            }

            //クエストキーマスタ取得
            MasterDataQuestKey cQuestKeyMaster = MasterFinder <MasterDataQuestKey> .Instance.Find((int)cKeyList[iKey].quest_key_id);

            if (cQuestKeyMaster == null)
            {
                Debug.LogError("QuestKey MasterData None! - " + cKeyList[iKey].quest_key_id);
                continue;
            }

            //クエストキーマスタに設定あり、クエストキー定義単位で表示
            bool bRet = MainMenuUtil.ChkQuestKeyPlayableFromId(cQuestKeyMaster);
            if (bRet == false)
            {
                continue;
            }

            UIAtlas cIconAtlas    = SceneObjReferMainMenu.Instance.m_MainMenuAtlas;
            string  strSpriteName = "mm_quest_key";

            //----------------------------------------
            // ここまできたらチェックOK。表示対象としてリストに登録
            //----------------------------------------
            ItemKeyContext _newKey = new ItemKeyContext();
            _newKey.Category   = ItemKeyContext.CategoryType.QuestKey;
            _newKey.IconImage  = cIconAtlas.GetSprite(strSpriteName);
            _newKey.keyMaster  = cQuestKeyMaster;
            _newKey.Name       = cQuestKeyMaster.key_name;
            _newKey.Count      = cKeyList[iKey].quest_key_cnt.ToString();
            _newKey.IsViewTime = false;
            _newKey.Time       = "";
            _newKey.timing_end = cQuestKeyMaster.timing_end;
            if (cQuestKeyMaster.timing_end != 0)
            {
                string   timeFormat = GameTextUtil.GetText("common_expirationdate");
                DateTime endTime    = TimeUtil.GetDateTime(cQuestKeyMaster.timing_end).SubtractAMinute();
                _newKey.Time       = string.Format(timeFormat, endTime.ToString("yyyy/MM/dd (HH:mm)"));
                _newKey.IsViewTime = true;
            }
            _newKey.DidSelectItemKey = OnSelectKey;

            //リストに登録
            m_Item.KeyList.Add(_newKey);
        }

        //期間でソート
        m_Item.KeyList.Sort((a, b) => (int)a.timing_end - (int)b.timing_end);
    }
Example #38
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();
                    }
                }
            }
        }
    }
Example #39
0
	/// <summary>
	/// Duplicate the specified sprite.
	/// </summary>

	static public SpriteEntry DuplicateSprite (UIAtlas atlas, string spriteName)
	{
		if (atlas == null || atlas.texture == null) return null;
		UISpriteData sd = atlas.GetSprite(spriteName);
		if (sd == null) return null;

		Texture2D tex = NGUIEditorTools.ImportTexture(atlas.texture, true, true, false);
		SpriteEntry se = ExtractSprite(sd, tex);

		if (se != null)
		{
			se.name = se.name + " (Copy)";

			List<UIAtlasMaker.SpriteEntry> sprites = new List<UIAtlasMaker.SpriteEntry>();
			UIAtlasMaker.ExtractSprites(atlas, sprites);
			sprites.Add(se);
			UIAtlasMaker.UpdateAtlas(atlas, sprites);
			if (se.temporaryTexture) DestroyImmediate(se.tex);
		}
		else NGUIEditorTools.ImportTexture(atlas.texture, false, false, !atlas.premultipliedAlpha);
		return se;
	}
Example #40
0
        IEnumerator DoScrollText(int idx, char t, float d)
        {
            UISprite     sp      = this[idx];
            string       imgName = NGUIUtil.Number2ImageName(t, mFormat);
            UISpriteData spData  = mAtlas.GetSprite(imgName);

            if (null != spData)
            {
                if (null == sp)
                {
                    sp       = NGUITools.AddSprite(gameObject, mAtlas, "");
                    sp.pivot = pivot;
                    sp.color = color;
                    //Vector2 size = Vector2.zero;
                    if (mSizeX <= 0 || mSizeY <= 0)
                    {
                        sp.type   = UISprite.Type.Simple;
                        sp.width  = spData.width;
                        sp.height = spData.height;
                    }
                    else
                    {
                        sp.type   = UISprite.Type.Sliced;
                        sp.width  = mSizeX;
                        sp.height = mSizeY;
                    }
                    Vector3 pos = Vector3.zero;
                    if (mSpriteList.Count > 0)
                    {
                        UISprite lastSp = mSpriteList[mSpriteList.Count - 1];
                        switch (pivot)
                        {
                        case Pivot.Bottom:
                        case Pivot.Top:
                        case Pivot.Center:
                            pos.x += lastSp.width * 0.5f;
                            break;

                        case Pivot.Right:
                        case Pivot.BottomRight:
                        case Pivot.TopRight:
                            pos.x += lastSp.width;
                            break;
                        }
                    }
                    switch (pivot)
                    {
                    case Pivot.Bottom:
                    case Pivot.Top:
                    case Pivot.Center:
                        pos.x += sp.width * 0.5f;
                        break;

                    case Pivot.Right:
                    case Pivot.BottomRight:
                    case Pivot.TopRight:
                        pos.x += sp.width;
                        break;
                    }
                    sp.transform.localPosition = pos;
                    mSpriteList.Add(sp);
                }
                if (null != sp)
                {
                    UISprite sprite = NGUITools.AddSprite(gameObject, mAtlas, imgName);
                    if (null != sprite)
                    {
                        sprite.pivot = pivot;
                        sprite.color = color;
                        if (mSizeX <= 0 || mSizeY <= 0)
                        {
                            sprite.type   = UISprite.Type.Simple;
                            sprite.width  = spData.width;
                            sprite.height = spData.height;
                        }
                        else
                        {
                            sprite.type   = UISprite.Type.Sliced;
                            sprite.width  = mSizeX;
                            sprite.height = mSizeY;
                        }
                        sprite.MakePixelPerfect();
                        Vector3 pos     = sp.transform.localPosition;
                        Vector3 spPos   = pos;
                        float   oriPosY = pos.y;
                        switch (pivot)
                        {
                        case Pivot.Bottom:
                        case Pivot.BottomLeft:
                        case Pivot.BottomRight:
                            pos.y -= sprite.height;
                            break;

                        case Pivot.Top:
                        case Pivot.TopLeft:
                        case Pivot.TopRight:
                            pos.y -= sp.height;
                            break;

                        case Pivot.Center:
                        case Pivot.Left:
                        case Pivot.Right:
                            pos.y -= (sp.height + sprite.height) * 0.5f;
                            break;
                        }
                        sprite.transform.localPosition = pos;
                        float dist = oriPosY - pos.y;
                        while (pos.y < oriPosY)
                        {
                            yield return(0);

                            if (null == sp)
                            {
                                break;
                            }
                            float deltaDist = Time.deltaTime * (dist / d);
                            pos.y   += deltaDist;
                            spPos.y += deltaDist;
                            sp.transform.localPosition     = spPos;
                            sprite.transform.localPosition = pos;
                        }
                        pos.y = oriPosY;
                        sprite.transform.localPosition = pos;
                        this[idx] = sprite;
                    }
                }
            }
        }
	/// <summary>
	/// Draw the inspector widget.
	/// </summary>

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

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

		GUILayout.Space(6f);

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

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

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

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

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

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

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

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

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

			mAtlas.MarkAsChanged();
		}

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

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

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

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

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

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

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

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

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

					NGUIEditorTools.BeginContents();

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

					GUI.changed = false;

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

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

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

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

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

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

						MarkSpriteAsDirty();
					}

					GUILayout.Space(3f);

					GUILayout.BeginHorizontal();

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

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

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

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

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

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

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

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

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

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

					NGUIEditorTools.EndContents();
				}

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

					if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name))
					{
						NGUIEditorTools.SelectPrevious();
					}
					GUI.backgroundColor = Color.white;
				}
			}
		}
	}
Example #42
0
    /// <summary>
    /// Sprite selection callback.
    /// </summary>

    void SelectSprite(string spriteName)
    {
        mSprite = mAtlas.GetSprite(spriteName);
        EditorPrefs.SetString("NGUI Selected Sprite", spriteName);
        Repaint();
    }
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

    void OnGUI()
    {
        NGUIEditorTools.SetLabelWidth(80f);

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

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

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

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

            Texture2D tex = atlas.texture as Texture2D;

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

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

            float size    = 80f;
            float padded  = size + 10f;
            int   columns = Mathf.FloorToInt(Screen.width / padded);
            if (columns < 1)
            {
                columns = 1;
            }

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

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

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

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

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

                                if (NGUISettings.selectedSprite != sprite.name)
                                {
                                    if (mSprite != null)
                                    {
                                        NGUIEditorTools.RegisterUndo("Atlas Selection", mSprite);
                                        mSprite.MakePixelPerfect();
                                        EditorUtility.SetDirty(mSprite.gameObject);
                                    }

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

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

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

                            // Stretch the sprite so that it will appear proper
                            float aspect   = (scaleY / scaleX) / ((float)tex.height / tex.width);
                            Rect  clipRect = rect;

                            if (aspect != 1f)
                            {
                                if (aspect < 1f)
                                {
                                    // The sprite is taller than it is wider
                                    float padding = size * (1f - aspect) * 0.5f;
                                    clipRect.xMin += padding;
                                    clipRect.xMax -= padding;
                                }
                                else
                                {
                                    // The sprite is wider than it is taller
                                    float padding = size * (1f - 1f / aspect) * 0.5f;
                                    clipRect.yMin += padding;
                                    clipRect.yMax -= padding;
                                }
                            }

                            GUI.DrawTextureWithTexCoords(clipRect, tex, uv);

                            // Draw the selection
                            if (NGUISettings.selectedSprite == sprite.name)
                            {
                                NGUIEditorTools.DrawOutline(rect, new Color(0.4f, 1f, 0f, 1f));
                            }
                        }

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

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

            if (close)
            {
                Close();
            }
        }
    }
Example #44
0
        /// <summary>
        /// 确保goodsId是存在的,0表示锁的icon ,1表示空白
        /// </summary>
        /// <param name="item"></param>
        /// <param name="goodsId"></param>
        /// <param name="type"></param>
        public void InitItem(UIWidgetContainer item, uint goodsId, ItemType type)
        {
            UISprite sprite;
            int      color      = 1;     //默认为 空白 1
            string   spriteName = string.Empty;
            UIAtlas  atlas      = null;

            //背景

            //高光
            //InitSpriteHelp(item, "gaoguang", "common", "gaoguang");
            InitSpriteHelp(item, "border", string.Empty, string.Empty);
            if (goodsId == this.LOCK_ICON)                //icon 锁
            {
                InitSpriteHelp(item, "background", "common", "pz_1");
                InitSpriteHelp(item, "icon", "common", "suo23");
            }
            else if (goodsId == this.EMPTY_ICON)             //空格子
            {
                InitSpriteHelp(item, "background", "common", "pz_1");
                InitSpriteHelp(item, "icon", "common", "pzkong");
            }
            else if (goodsId == this.ADD_ICON)
            {
                InitSpriteHelp(item, "background", "common", "pz_1");
                InitSpriteHelp(item, "icon", "common", "add");
            }

            else if (goodsId > 100000)      //物品道具
            {
                SysItemVo vo = BaseDataMgr.instance.GetDataById <SysItemVo>(goodsId);
                color      = vo.color;
                spriteName = string.Empty + vo.icon;
                if (vo.type == GoodsConst.SMELT_GOODS)
                {
                    atlas = Singleton <AtlasManager> .Instance.GetAtlas("GemIcon");
                }
                else
                {
                    atlas = Singleton <AtlasManager> .Instance.GetAtlas("PropIcon");
                }
                if (atlas == null || atlas.GetSprite(spriteName) == null)
                {
                    spriteName = "100000";
                }
                item.gameObject.SetChildActive("color", false);
                InitSpriteHelp(item, "icon", atlas, spriteName);
                InitSpriteHelp(item, "background", "common", "pz_" + color);
            }
            else             //装备
            {
                SysEquipVo vo = BaseDataMgr.instance.GetDataById <SysEquipVo>(goodsId);
                color      = vo.color;
                spriteName = string.Empty + vo.icon;
                atlas      = Singleton <AtlasManager> .Instance.GetAtlas("EquipIcon");

                if (atlas.GetSprite(spriteName) == null)
                {
                    InitSpriteHelp(item, "icon", "PropIcon", "100000");
                }
                else
                {
                    InitSpriteHelp(item, "icon", "EquipIcon", spriteName);
                }
                InitSpriteHelp(item, "background", "EquipIcon", "pz_" + color);
                item.gameObject.SetChildActive("color", false);
            }
        }