天天看點

NGUI支援TexturePacker導出有旋轉的Sprite,無需Transform旋轉版本

在 ulua&tolua技術交流群①中群号 341746602,有位大神分析了一份讓NGUI圖集支援旋轉sprite的技術文章。

傳送門:http://www.maosongliang.com/archives/324?bsh_bid=1506423853

近幾天潛心研究了下,發現一個很蛋疼問題。第一點sprite渲染出來不是旋轉好的,雖然大神寫了自動旋轉的代碼,但是遇到旋轉之後的Sprite當一個旋轉節點的子節點的時候,出了各種問題。同時遇到Anchor自适應的時候也是各種搞。

于是想到一個解決方案,基于繪制區域的選擇,而不是基于transform

貼上c#代碼(利用ulua群中毛松亮大神的代碼之處仍然保留其注釋)

讀取配置部分

UISpriteData.cs

// [Modify] by maosongliang, begin
    public bool rotated = false;
    // [Modify] by maosongliang, end
           

NGUIJson.cs

static void LoadSpriteData (UIAtlas atlas, Hashtable decodedHash)
	{
		if (decodedHash == null || atlas == null) return;
		List<UISpriteData> oldSprites = atlas.spriteList;
		atlas.spriteList = new List<UISpriteData>();

		Hashtable frames = (Hashtable)decodedHash["frames"];

		foreach (System.Collections.DictionaryEntry item in frames)
		{
			UISpriteData newSprite = new UISpriteData();
			newSprite.name = item.Key.ToString();

			bool exists = false;

			// Check to see if this sprite exists
			foreach (UISpriteData oldSprite in oldSprites)
			{
				if (oldSprite.name.Equals(newSprite.name, StringComparison.OrdinalIgnoreCase))
				{
					exists = true;
					break;
				}
			}

			// Get rid of the extension if the sprite doesn't exist
			// The extension is kept for backwards compatibility so it's still possible to update older atlases.
			if (!exists)
			{
				newSprite.name = newSprite.name.Replace(".png", "");
				newSprite.name = newSprite.name.Replace(".tga", "");
			}

			// Extract the info we need from the TexturePacker json file, mainly uvRect and size
			Hashtable table = (Hashtable)item.Value;
			Hashtable frame = (Hashtable)table["frame"];

			int frameX = int.Parse(frame["x"].ToString());
			int frameY = int.Parse(frame["y"].ToString());
			int frameW = int.Parse(frame["w"].ToString());
			int frameH = int.Parse(frame["h"].ToString());

            // [Modify] by maosongliang, begin
            // Read the rotation value
            newSprite.rotated = (bool)table["rotated"];
            // [Modify] by maosongliang, end

            newSprite.x = frameX;
			newSprite.y = frameY;
            newSprite.width = frameW;
            newSprite.height = frameH;

            // Support for trimmed sprites
            Hashtable sourceSize = (Hashtable)table["sourceSize"];
			Hashtable spriteSize = (Hashtable)table["spriteSourceSize"];

			if (spriteSize != null && sourceSize != null)
			{
				// TODO: Account for rotated sprites
				if (frameW > 0)
				{
					int spriteX = int.Parse(spriteSize["x"].ToString());
					int spriteW = int.Parse(spriteSize["w"].ToString());
					int sourceW = int.Parse(sourceSize["w"].ToString());

					newSprite.paddingLeft = spriteX;
					newSprite.paddingRight = sourceW - (spriteX + spriteW);
				}

				if (frameH > 0)
				{
					int spriteY = int.Parse(spriteSize["y"].ToString());
					int spriteH = int.Parse(spriteSize["h"].ToString());
					int sourceH = int.Parse(sourceSize["h"].ToString());

					newSprite.paddingTop = spriteY;
					newSprite.paddingBottom = sourceH - (spriteY + spriteH);
				}
			}

            // [Modify] by maosongliang, begin
            if (newSprite.rotated)
            {
                int temp = newSprite.width;
                newSprite.width = newSprite.height;
                newSprite.height = temp;

                temp = newSprite.paddingLeft;
                newSprite.paddingLeft = newSprite.paddingTop;
                newSprite.paddingTop = temp;

                temp = newSprite.paddingRight;
                newSprite.paddingRight = newSprite.paddingBottom;
                newSprite.paddingBottom = temp;
            }
            // [Modify] by maosongliang, end

            // If the sprite was present before, see if we can copy its inner rect
            foreach (UISpriteData oldSprite in oldSprites)
			{
				if (oldSprite.name.Equals(newSprite.name, StringComparison.OrdinalIgnoreCase))
				{
					newSprite.borderLeft = oldSprite.borderLeft;
					newSprite.borderRight = oldSprite.borderRight;
					newSprite.borderBottom = oldSprite.borderBottom;
					newSprite.borderTop = oldSprite.borderTop;
				}
			}

			// Add this new sprite
			atlas.spriteList.Add(newSprite);
		}

		// Sort imported sprites alphabetically
		atlas.spriteList.Sort(CompareSprites);
		Debug.Log("Imported " + atlas.spriteList.Count + " sprites");
	}
           

UIBasicSprite.cs

//modify by Amumu begin
	private Quaternion vertsAngle = Quaternion.Euler(0, 180, -90);
	protected bool rotated = false;
	//modify by Amumu end
	protected void Fill (BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols, Rect outer, Rect inner)
	{
		mOuterUV = outer;
		mInnerUV = inner;

		switch (type)
		{
			case Type.Simple:
			SimpleFill(verts, uvs, cols);
			break;

			case Type.Sliced:
			SlicedFill(verts, uvs, cols);
			break;

			case Type.Filled:
			FilledFill(verts, uvs, cols);
			break;

			case Type.Tiled:
			TiledFill(verts, uvs, cols);
			break;

			case Type.Advanced:
			AdvancedFill(verts, uvs, cols);
			break;
		}
		//modify by Amumu begin
		if (rotated) {
			Vector4 v = drawingDimensions;
			float centerX = v.x + v.z;
			float centerY = v.y + v.w;

			for (int i = 0; i < verts.size; i++) {
				Vector3 vert = vertsAngle * verts[i];
				vert.x += centerY;
				vert.y += centerX;
				verts[i] = vert;
			}
		}
		//modify by Amumu end
	}
           

UISprite.cs

public override Vector4 drawingDimensions
	{
		get
		{
                   .....
			//modify by Amumu begin
                        if(rotated){
			    return new Vector4(vw, vx, vy, vz);                        
			}
                        else{
			    return new Vector4(vx, vy, vz, vw);   
                        }
			//modify by Amumu end
		}
	}
	protected void SetAtlasSprite (UISpriteData sp)
	{
		mChanged = true;
		mSpriteSet = true;

		if (sp != null)
		{
			mSprite = sp;
			mSpriteName = mSprite.name;
			//modify by Amumu begin
			rotated = mSprite.rotated;
			//modify by Amumu end
		}
		else
		{
			mSpriteName = (mSprite != null) ? mSprite.name : "";
			mSprite = sp;
		}
	}
	public override void MakePixelPerfect ()
	{
		if (!isValid) return;
		base.MakePixelPerfect();
		if (mType == Type.Tiled) return;

		UISpriteData sp = GetAtlasSprite();
		if (sp == null) return;

		Texture tex = mainTexture;
		if (tex == null) return;

		if (mType == Type.Simple || mType == Type.Filled || !sp.hasBorder)
		{
			if (tex != null)
			{
				int x = Mathf.RoundToInt(pixelSize * (sp.width + sp.paddingLeft + sp.paddingRight));
				int y = Mathf.RoundToInt(pixelSize * (sp.height + sp.paddingTop + sp.paddingBottom));
				
				if ((x & 1) == 1) ++x;
				if ((y & 1) == 1) ++y;
				//modify by Amumu begin
				if (mSprite != null && mSprite.rotated) {
					int tmp = x;
					x = y;
					y = tmp;
				}
				//modify by Amumu end
				width = x;
				height = y;
			}
		}
	}