当我们工程里有许多对称的图的时候,可以利用镜像来实现同样的效果显示。并且减少了图集的大小占用。一方面减少了资源占用,另一方面运行内存也会降低一些。
在UIBasicSprite.cs 脚本中添加我们的代码:
<span style="white-space:pre"> </span>public enum Type { Simple, Sliced, Tiled, Filled, Advanced, Mirrored, }
调用方法:
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; case Type.Mirrored: MirroredFill(verts, uvs, cols); break; } }
具体实现代码:
void MirroredFill (BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols) { Texture tex = mainTexture; if (tex == null) return; Vector2 size = new Vector2(mInnerUV.width * tex.width, mInnerUV.height * tex.height); size *= pixelSize; if (tex == null || size.x < 2f || size.y < 2f) return; Color32 c = drawingColor; Vector4 v = drawingDimensions; Vector4 u=new Vector4(mInnerUV.xMin,mInnerUV.yMin,mInnerUV.xMax,mInnerUV.yMax); float x0 = v.x; float y0 = v.y; float u0 = u.x; float v0 = u.y; bool yb = true; while (y0 < v.w) { x0 = v.x; float y1 = y0 + size.y; float v1 = u.w; if (y1 > v.w) { v1 = Mathf.Lerp(u.y, u.w, (v.w - y0) / size.y); y1 = v.w; } bool b=true; while (x0 < v.z) { float x1 = x0 + size.x; float u1 = u.z; if (x1 > v.z) { u1 = Mathf.Lerp(u.x, u.z, (v.z - x0) / size.x); x1 = v.z; } float offset=0.3f; verts.Add(new Vector3(x0-offset, y0-offset)); verts.Add(new Vector3(x0-offset, y1+offset)); verts.Add(new Vector3(x1+offset, y1+offset)); verts.Add(new Vector3(x1+offset, y0-offset)); float yoffset = size.y != 1 ? 0.001f : 0.0f; float xoffset = size.x != 1 ? 0.001f : 0.0f; if(yb) { if(b) { uvs.Add(new Vector2(u0-xoffset, v0)); uvs.Add(new Vector2(u0-xoffset, v1+yoffset)); uvs.Add(new Vector2(u1, v1+yoffset)); uvs.Add(new Vector2(u1, v0)); } else { uvs.Add(new Vector2(u1, v0)); uvs.Add(new Vector2(u1, v1+yoffset)); uvs.Add(new Vector2(u0-xoffset, v1+yoffset)); uvs.Add(new Vector2(u0-xoffset, v0)); } } else { if(b) { uvs.Add(new Vector2(u0-xoffset, v1+yoffset)); uvs.Add(new Vector2(u0-xoffset, v0)); uvs.Add(new Vector2(u1, v0)); uvs.Add(new Vector2(u1, v1+yoffset)); } else { uvs.Add(new Vector2(u1, v1+yoffset)); uvs.Add(new Vector2(u1, v0)); uvs.Add(new Vector2(u0-xoffset, v0)); uvs.Add(new Vector2(u0-xoffset, v1+yoffset)); } } cols.Add(c); cols.Add(c); cols.Add(c); cols.Add(c); x0 += size.x; b=!b; } yb=!yb; y0 += size.y; } }
扩张后就可以实现镜像效果了。
时间: 2024-11-01 10:33:33