您的位置:首页 > 产品设计 > UI/UE

NGUI支持TexturePacker导出有旋转的Sprite,无需Transform旋转版本

2016-10-18 15:49 736 查看
在 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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Unity NGUI 图集 atlas 旋转
相关文章推荐