NGUI里有没有判断ngui 动态创建spritee是否在屏幕内的方法

unity NGUI(3)
学习了几天Unity3D强大的NGUI插件,MOMO觉得NGUI中最大的亮点之一就是Sprite精灵。我们先说说精灵是什么东西?它可以在一张大图中去截取一部分(大图就是整体图像集合,而截取的小图就是一个精灵),然后起一个精灵的名称,使用时通过精灵的名称就能直接绘制,并且精灵还可以播放动画。总之真的非常强大。本节我们学习如何创建自己的精灵文件。
首先我们在Project中创建一个Prefab对象,默认给它Transform变换属性,暂时我们给Prefab对象起名为MySprite。接着给Prefab挂上图像集合组件。在Unity导航菜单栏中选择Component-&NGUI-&UI-&Atlas(图像集合)即可。
常用的属性介绍:
Coordinates :UI坐标单位,用默认Pixels(像素)就行。
Pixel Size:像素的长度单位1。
Add/Delete:添加与删除精灵。
first就是当前精灵的名称,如果已添加了多个精灵都会出现在Sprite下拉列表中。再往下就是设置该精灵在大图的显示区域以及坐标,下图中我们可看到已经在整个大图中框选了一个区域,就是绿色的矩形,矩形中就是当前精灵中的内容。
& && & &下面我们将这张图片分为4块完全相等的精灵,接着我们学习在代码中如何控制这些精灵的绘制以及排序。创建脚本Test.cs并且将它挂在刚刚创建的精灵对象身上。
using UnityE
using System.C
using System.Collections.G
public class Test : MonoBehaviour {
void Start()
//得到精灵组件脚本
UISprite ui = gameObject.GetComponent&UISprite&();
//得到图像集合中精灵的数量
Debug.Log(&精灵的数量& + ui.atlas.spriteList.Count);
//得到图像集合数组中对应角标精灵对象的名称
string name = ui.atlas.spriteList[2].
//设置显示精灵的名称
ui.spriteName =
//设置精灵的深度,数值越大绘制的越靠前
ui.depth = 2;
如下图所示,精灵已经在代码中动态的修改完毕,并且设置了它的深度让它靠前显示。如果想修改它的X、Y位置,直接修改Transform即可。Z轴的话修改它的深度就行,不用修改Z轴坐标。
下面我们再来学习一下精灵动画,NGUI已经帮我们封装了专门播放精灵动画的类UISpriteAnimation 专门处理精灵动画。MOMO在网上随便找了一组2D主角的行走动画,按照上面介绍的方法将整个图片导入工程中。如下图所示,这组人物的行走动画我们选区取左移动的4帧。
这里我在强调一下,需要修改修改材质的着色器,让他支持背景透明,对当前材质的着色器我们选择Unlit/TransparentColored。
& && & 接着在Hierarchy视图中选择Sprite(NGUI)(刚刚创建的精灵对象),然后在Unity导航菜单栏中选择Component-&NGUI-&UI-&Sprite Animation(帧动画)组件即可。此时直接运行游戏我们就会发现主角已经按照刚刚裁剪的精灵顺序动了起来,这还没有完,我们下面学习在代码中如何动态的控制精灵。
& && & 本例我们的目的是使用NGUI在屏幕中创建一个按钮,第一次点击按钮主角播放行走动画,再次点击按钮主角动画将停止。上篇文章中我们已经学习了NGUI中按钮的相应事件,如果想在别的对象或者脚本中监听某个按钮的点击事件那么就可以使用传递消息的方式。NGUI帮我们封装了一次传递消息的类,Unity导航菜单栏中选择NGUI-》Interaction-&ButtonMessage。它的原理也是使用Unity的sendMessage,有兴趣的朋友可以去看看。接着说正题,创建脚本Test.cs挂在刚刚创建的按钮上。
using UnityE
using System.C
using System.Collections.G
public class Test : MonoBehaviour {
//播放动画与不播放
bool isPlayAnim =
//得到精灵对象
GameObject animObj =
void Start()
//得到精灵对象
animObj = GameObject.Find(&Sprite&);
void OnClick ()
if(isPlayAnim)
//停止动画
isPlayAnim =
//销毁UISpriteAnimation组件
Destroy(animObj.GetComponent&UISpriteAnimation&());
//动画停止后设置精灵默认的帧
UISprite ui = animObj.GetComponent&UISprite&();
string name = ui.atlas.spriteList[0].
ui.spriteName =
//播放播放
isPlayAnim =
//加入播放动画组件
animObj.AddComponent(&UISpriteAnimation&);
//设置播放动画的速度
//1-60之间数值越大播放速度越快
UISpriteAnimation uiAnim = animObj.GetComponent&UISpriteAnimation&();
uiAnim.framesPerSecond = 10;
如下图所示,运行游戏点击按钮。主角开始迈着大步开始行走了,再次点击主角将停止播放行走动画。嚯嚯。最后MOMO祝大家学习愉快啦啦啦!!
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1874次
排名:千里之外
原创:20篇
转载:27篇
(8)(3)(1)(2)(13)(13)(5)(2)(1)项目语言:C#
权限:read-only(如需更高权限请先加入项目)
Index: UIBasicSprite.cs
===================================================================
--- UIBasicSprite.cs (revision 0)
+++ UIBasicSprite.cs (revision 3)
@@ -0,0 +1,1091 @@
+using UnityE
+/// &summary&
+/// Functionality common to both NGUI and 2D sprites brought out into a single common parent.
+/// Mostly contains everything related to drawing the sprite.
+/// &/summary&
+public abstract class UIBasicSprite : UIWidget
+ public enum Type
+ public enum FillDirection
Horizontal,
Radial180,
Radial360,
+ public enum AdvancedType
Invisible,
+ public enum Flip
Horizontally,
Vertically,
+ [HideInInspector][SerializeField] protected Type mType = Type.S
+ [HideInInspector][SerializeField] protected FillDirection mFillDirection = FillDirection.Radial360;
+ [Range(0f, 1f)]
+ [HideInInspector][SerializeField] protected float mFillAmount = 1.0f;
+ [HideInInspector][SerializeField] protected bool mInvert =
+ [HideInInspector][SerializeField] protected Flip mFlip = Flip.N
+ // Cached to avoid allocations
+ [System.NonSerialized] Rect mInnerUV = new Rect();
+ [System.NonSerialized] Rect mOuterUV = new Rect();
+ /// &summary&
+ /// When the sprite type is advanced, this determines whether the center is tiled or sliced.
+ /// &/summary&
+ public AdvancedType centerType = AdvancedType.S
+ /// &summary&
+ /// When the sprite type is advanced, this determines whether the left edge is tiled or sliced.
+ /// &/summary&
+ public AdvancedType leftType = AdvancedType.S
+ /// &summary&
+ /// When the sprite type is advanced, this determines whether the right edge is tiled or sliced.
+ /// &/summary&
+ public AdvancedType rightType = AdvancedType.S
+ /// &summary&
+ /// When the sprite type is advanced, this determines whether the bottom edge is tiled or sliced.
+ /// &/summary&
+ public AdvancedType bottomType = AdvancedType.S
+ /// &summary&
+ /// When the sprite type is advanced, this determines whether the top edge is tiled or sliced.
+ /// &/summary&
+ public AdvancedType topType = AdvancedType.S
+ /// &summary&
+ /// How the sprite is drawn. It's virtual for legacy reasons (UISlicedSprite, UITiledSprite, UIFilledSprite).
+ /// &/summary&
+ public virtual Type type
if (mType != value)
MarkAsChanged();
+ /// &summary&
+ /// Sprite flip setting.
+ /// &/summary&
+ public Flip flip
if (mFlip != value)
MarkAsChanged();
+ /// &summary&
+ /// Direction of the cut procedure.
+ /// &/summary&
+ public FillDirection fillDirection
return mFillD
if (mFillDirection != value)
mFillDirection =
mChanged =
+ /// &summary&
+ /// Amount of the sprite shown. 0-1 range with 0 being nothing shown, and 1 being the full sprite.
+ /// &/summary&
+ public float fillAmount
return mFillA
float val = Mathf.Clamp01(value);
if (mFillAmount != val)
mFillAmount =
mChanged =
+ /// &summary&
+ /// Minimum allowed width for this widget.
+ /// &/summary&
+ override public int minWidth
if (type == Type.Sliced || type == Type.Advanced)
Vector4 b = border * pixelS
int min = Mathf.RoundToInt(b.x + b.z);
return Mathf.Max(base.minWidth, ((min & 1) == 1) ? min + 1 : min);
return base.minW
+ /// &summary&
+ /// Minimum allowed height for this widget.
+ /// &/summary&
+ override public int minHeight
if (type == Type.Sliced || type == Type.Advanced)
Vector4 b = border * pixelS
int min = Mathf.RoundToInt(b.y + b.w);
return Mathf.Max(base.minHeight, ((min & 1) == 1) ? min + 1 : min);
return base.minH
+ /// &summary&
+ /// Whether the sprite should be filled in the opposite direction.
+ /// &/summary&
+ public bool invert
if (mInvert != value)
mChanged =
+ /// &summary&
+ /// Whether the widget has a border for 9-slicing.
+ /// &/summary&
+ public bool hasBorder
Vector4 br =
return (br.x != 0f || br.y != 0f || br.z != 0f || br.w != 0f);
+ /// &summary&
+ /// Whether the sprite's material is using a pre-multiplied alpha shader.
+ /// &/summary&
+ public virtual bool premultipliedAlpha { get { } }
+ /// &summary&
+ /// Size of the pixel. Overwritten in the NGUI sprite to pull a value from the atlas.
+ /// &/summary&
+ public virtual float pixelSize { get { return 1f; } }
+#if UNITY_EDITOR
+ /// &summary&
+ /// Keep sane values.
+ /// &/summary&
+ protected override void OnValidate ()
base.OnValidate();
mFillAmount = Mathf.Clamp01(mFillAmount);
+#region Fill Functions
+ // Static variables to reduce garbage collection
+ static protected Vector2[] mTempPos = new Vector2[4];
+ static protected Vector2[] mTempUVs = new Vector2[4];
+ /// &summary&
+ /// Convenience function that returns the drawn UVs after flipping gets considered.
+ /// X = left, Y = bottom, Z = right, W = top.
+ /// &/summary&
+ Vector4 drawingUVs
switch (mFlip)
case Flip.Horizontally: return new Vector4(mOuterUV.xMax, mOuterUV.yMin, mOuterUV.xMin, mOuterUV.yMax);
case Flip.Vertically: return new Vector4(mOuterUV.xMin, mOuterUV.yMax, mOuterUV.xMax, mOuterUV.yMin);
case Flip.Both: return new Vector4(mOuterUV.xMax, mOuterUV.yMax, mOuterUV.xMin, mOuterUV.yMin);
default: return new Vector4(mOuterUV.xMin, mOuterUV.yMin, mOuterUV.xMax, mOuterUV.yMax);
+ /// &summary&
+ /// Final widget's color passed to the draw buffer.
+ /// &/summary&
+ Color32 drawingColor
Color colF =
colF.a = finalA
if (premultipliedAlpha) colF = NGUITools.ApplyPMA(colF);
if (QualitySettings.activeColorSpace == ColorSpace.Linear)
colF.r = Mathf.GammaToLinearSpace(colF.r);
colF.g = Mathf.GammaToLinearSpace(colF.g);
colF.b = Mathf.GammaToLinearSpace(colF.b);
return colF;
+ /// &summary&
+ /// Fill the draw buffers.
+ /// &/summary&
+ protected void Fill (BetterList&Vector3& verts, BetterList&Vector2& uvs, BetterList&Color32& cols, Rect outer, Rect inner)
mOuterUV =
mInnerUV =
switch (type)
case Type.Simple:
SimpleFill(verts, uvs, cols);
case Type.Sliced:
SlicedFill(verts, uvs, cols);
case Type.Filled:
FilledFill(verts, uvs, cols);
case Type.Tiled:
TiledFill(verts, uvs, cols);
case Type.Advanced:
AdvancedFill(verts, uvs, cols);
+ /// &summary&
+ /// Regular sprite fill function is quite simple.
+ /// &/summary&
+ void SimpleFill (BetterList&Vector3& verts, BetterList&Vector2& uvs, BetterList&Color32& cols)
Vector4 v = drawingD
Vector4 u = drawingUVs;
Color32 c = drawingC
verts.Add(new Vector3(v.x, v.y));
verts.Add(new Vector3(v.x, v.w));
verts.Add(new Vector3(v.z, v.w));
verts.Add(new Vector3(v.z, v.y));
uvs.Add(new Vector2(u.x, u.y));
uvs.Add(new Vector2(u.x, u.w));
uvs.Add(new Vector2(u.z, u.w));
uvs.Add(new Vector2(u.z, u.y));
cols.Add(c);
cols.Add(c);
cols.Add(c);
cols.Add(c);
+ /// &summary&
+ /// Sliced sprite fill function is more complicated as it generates 9 quads instead of 1.
+ /// &/summary&
+ void SlicedFill (BetterList&Vector3& verts, BetterList&Vector2& uvs, BetterList&Color32& cols)
Vector4 br = border * pixelS
if (br.x == 0f && br.y == 0f && br.z == 0f && br.w == 0f)
SimpleFill(verts, uvs, cols);
Color32 c = drawingC
Vector4 v = drawingD
mTempPos[0].x = v.x;
mTempPos[0].y = v.y;
mTempPos[3].x = v.z;
mTempPos[3].y = v.w;
if (mFlip == Flip.Horizontally || mFlip == Flip.Both)
mTempPos[1].x = mTempPos[0].x + br.z;
mTempPos[2].x = mTempPos[3].x - br.x;
mTempUVs[3].x = mOuterUV.xM
mTempUVs[2].x = mInnerUV.xM
mTempUVs[1].x = mInnerUV.xM
mTempUVs[0].x = mOuterUV.xM
mTempPos[1].x = mTempPos[0].x + br.x;
mTempPos[2].x = mTempPos[3].x - br.z;
mTempUVs[0].x = mOuterUV.xM
mTempUVs[1].x = mInnerUV.xM
mTempUVs[2].x = mInnerUV.xM
mTempUVs[3].x = mOuterUV.xM
if (mFlip == Flip.Vertically || mFlip == Flip.Both)
mTempPos[1].y = mTempPos[0].y + br.w;
mTempPos[2].y = mTempPos[3].y - br.y;
mTempUVs[3].y = mOuterUV.yM
mTempUVs[2].y = mInnerUV.yM
mTempUVs[1].y = mInnerUV.yM
mTempUVs[0].y = mOuterUV.yM
mTempPos[1].y = mTempPos[0].y + br.y;
mTempPos[2].y = mTempPos[3].y - br.w;
mTempUVs[0].y = mOuterUV.yM
mTempUVs[1].y = mInnerUV.yM
mTempUVs[2].y = mInnerUV.yM
mTempUVs[3].y = mOuterUV.yM
for (int x = 0; x & 3; ++x)
int x2 = x + 1;
for (int y = 0; y & 3; ++y)
if (centerType == AdvancedType.Invisible && x == 1 && y == 1)
int y2 = y + 1;
verts.Add(new Vector3(mTempPos[x].x, mTempPos[y].y));
verts.Add(new Vector3(mTempPos[x].x, mTempPos[y2].y));
verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y2].y));
verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y].y));
uvs.Add(new Vector2(mTempUVs[x].x, mTempUVs[y].y));
uvs.Add(new Vector2(mTempUVs[x].x, mTempUVs[y2].y));
uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[y2].y));
uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[y].y));
cols.Add(c);
cols.Add(c);
cols.Add(c);
cols.Add(c);
+ /// &summary&
+ /// Tiled sprite fill function.
+ /// &/summary&
+ void TiledFill (BetterList&Vector3& verts, BetterList&Vector2& uvs, BetterList&Color32& cols)
Texture tex = mainT
if (tex == null)
Vector2 size = new Vector2(mInnerUV.width * tex.width, mInnerUV.height * tex.height);
size *= pixelS
if (tex == null || size.x & 2f || size.y & 2f)
Color32 c = drawingC
Vector4 v = drawingD
if (mFlip == Flip.Horizontally || mFlip == Flip.Both)
u.x = mInnerUV.xM
u.z = mInnerUV.xM
u.x = mInnerUV.xM
u.z = mInnerUV.xM
if (mFlip == Flip.Vertically || mFlip == Flip.Both)
u.y = mInnerUV.yM
u.w = mInnerUV.yM
u.y = mInnerUV.yM
u.w = mInnerUV.yM
float x0 = v.x;
float y0 = v.y;
float u0 = u.x;
float v0 = u.y;
while (y0 & v.w)
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);
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);
verts.Add(new Vector3(x0, y0));
verts.Add(new Vector3(x0, y1));
verts.Add(new Vector3(x1, y1));
verts.Add(new Vector3(x1, y0));
uvs.Add(new Vector2(u0, v0));
uvs.Add(new Vector2(u0, v1));
uvs.Add(new Vector2(u1, v1));
uvs.Add(new Vector2(u1, v0));
cols.Add(c);
cols.Add(c);
cols.Add(c);
cols.Add(c);
x0 += size.x;
y0 += size.y;
+ /// &summary&
+ /// Filled sprite fill function.
+ /// &/summary&
+ void FilledFill (BetterList&Vector3& verts, BetterList&Vector2& uvs, BetterList&Color32& cols)
if (mFillAmount & 0.001f)
Vector4 v = drawingD
Vector4 u = drawingUVs;
Color32 c = drawingC
// Horizontal and vertical filled sprites are simple -- just end the sprite prematurely
if (mFillDirection == FillDirection.Horizontal || mFillDirection == FillDirection.Vertical)
if (mFillDirection == FillDirection.Horizontal)
float fill = (u.z - u.x) * mFillA
if (mInvert)
v.x = v.z - (v.z - v.x) * mFillA
u.x = u.z -
v.z = v.x + (v.z - v.x) * mFillA
u.z = u.x +
else if (mFillDirection == FillDirection.Vertical)
float fill = (u.w - u.y) * mFillA
if (mInvert)
v.y = v.w - (v.w - v.y) * mFillA
u.y = u.w -
v.w = v.y + (v.w - v.y) * mFillA
u.w = u.y +
mTempPos[0] = new Vector2(v.x, v.y);
mTempPos[1] = new Vector2(v.x, v.w);
mTempPos[2] = new Vector2(v.z, v.w);
mTempPos[3] = new Vector2(v.z, v.y);
mTempUVs[0] = new Vector2(u.x, u.y);
mTempUVs[1] = new Vector2(u.x, u.w);
mTempUVs[2] = new Vector2(u.z, u.w);
mTempUVs[3] = new Vector2(u.z, u.y);
if (mFillAmount & 1f)
if (mFillDirection == FillDirection.Radial90)
if (RadialCut(mTempPos, mTempUVs, mFillAmount, mInvert, 0))
for (int i = 0; i & 4; ++i)
verts.Add(mTempPos[i]);
uvs.Add(mTempUVs[i]);
cols.Add(c);
if (mFillDirection == FillDirection.Radial180)
for (int side = 0; side & 2; ++side)
float fx0, fx1, fy0, fy1;
if (side == 0) { fx0 = 0f; fx1 = 0.5f; }
else { fx0 = 0.5f; fx1 = 1f; }
mTempPos[0].x = Mathf.Lerp(v.x, v.z, fx0);
mTempPos[1].x = mTempPos[0].x;
mTempPos[2].x = Mathf.Lerp(v.x, v.z, fx1);
mTempPos[3].x = mTempPos[2].x;
mTempPos[0].y = Mathf.Lerp(v.y, v.w, fy0);
mTempPos[1].y = Mathf.Lerp(v.y, v.w, fy1);
mTempPos[2].y = mTempPos[1].y;
mTempPos[3].y = mTempPos[0].y;
mTempUVs[0].x = Mathf.Lerp(u.x, u.z, fx0);
mTempUVs[1].x = mTempUVs[0].x;
mTempUVs[2].x = Mathf.Lerp(u.x, u.z, fx1);
mTempUVs[3].x = mTempUVs[2].x;
mTempUVs[0].y = Mathf.Lerp(u.y, u.w, fy0);
mTempUVs[1].y = Mathf.Lerp(u.y, u.w, fy1);
mTempUVs[2].y = mTempUVs[1].y;
mTempUVs[3].y = mTempUVs[0].y;
float val = !mInvert ? fillAmount * 2f - side : mFillAmount * 2f - (1 - side);
if (RadialCut(mTempPos, mTempUVs, Mathf.Clamp01(val), !mInvert, NGUIMath.RepeatIndex(side + 3, 4)))
for (int i = 0; i & 4; ++i)
verts.Add(mTempPos[i]);
uvs.Add(mTempUVs[i]);
cols.Add(c);
if (mFillDirection == FillDirection.Radial360)
for (int corner = 0; corner & 4; ++corner)
float fx0, fx1, fy0, fy1;
if (corner & 2) { fx0 = 0f; fx1 = 0.5f; }
else { fx0 = 0.5f; fx1 = 1f; }
if (corner == 0 || corner == 3) { fy0 = 0f; fy1 = 0.5f; }
else { fy0 = 0.5f; fy1 = 1f; }
mTempPos[0].x = Mathf.Lerp(v.x, v.z, fx0);
mTempPos[1].x = mTempPos[0].x;
mTempPos[2].x = Mathf.Lerp(v.x, v.z, fx1);
mTempPos[3].x = mTempPos[2].x;
mTempPos[0].y = Mathf.Lerp(v.y, v.w, fy0);
mTempPos[1].y = Mathf.Lerp(v.y, v.w, fy1);
mTempPos[2].y = mTempPos[1].y;
mTempPos[3].y = mTempPos[0].y;
mTempUVs[0].x = Mathf.Lerp(u.x, u.z, fx0);
mTempUVs[1].x = mTempUVs[0].x;
mTempUVs[2].x = Mathf.Lerp(u.x, u.z, fx1);
mTempUVs[3].x = mTempUVs[2].x;
mTempUVs[0].y = Mathf.Lerp(u.y, u.w, fy0);
mTempUVs[1].y = Mathf.Lerp(u.y, u.w, fy1);
mTempUVs[2].y = mTempUVs[1].y;
mTempUVs[3].y = mTempUVs[0].y;
float val = mInvert ?
mFillAmount * 4f - NGUIMath.RepeatIndex(corner + 2, 4) :
mFillAmount * 4f - (3 - NGUIMath.RepeatIndex(corner + 2, 4));
if (RadialCut(mTempPos, mTempUVs, Mathf.Clamp01(val), mInvert, NGUIMath.RepeatIndex(corner + 2, 4)))
for (int i = 0; i & 4; ++i)
verts.Add(mTempPos[i]);
uvs.Add(mTempUVs[i]);
cols.Add(c);
// Fill the buffer with the quad for the sprite
for (int i = 0; i & 4; ++i)
verts.Add(mTempPos[i]);
uvs.Add(mTempUVs[i]);
cols.Add(c);
+ /// &summary&
+ /// Advanced sprite fill function. Contributed by Nicki Hansen.
+ /// &/summary&
+ void AdvancedFill (BetterList&Vector3& verts, BetterList&Vector2& uvs, BetterList&Color32& cols)
Texture tex = mainT
if (tex == null)
Vector4 br = border * pixelS
if (br.x == 0f && br.y == 0f && br.z == 0f && br.w == 0f)
SimpleFill(verts, uvs, cols);
Color32 c = drawingC
Vector4 v = drawingD
Vector2 tileSize = new Vector2(mInnerUV.width * tex.width, mInnerUV.height * tex.height);
tileSize *= pixelS
if (tileSize.x & 1f) tileSize.x = 1f;
if (tileSize.y & 1f) tileSize.y = 1f;
mTempPos[0].x = v.x;
mTempPos[0].y = v.y;
mTempPos[3].x = v.z;
mTempPos[3].y = v.w;
if (mFlip == Flip.Horizontally || mFlip == Flip.Both)
mTempPos[1].x = mTempPos[0].x + br.z;
mTempPos[2].x = mTempPos[3].x - br.x;
mTempUVs[3].x = mOuterUV.xM
mTempUVs[2].x = mInnerUV.xM
mTempUVs[1].x = mInnerUV.xM
mTempUVs[0].x = mOuterUV.xM
mTempPos[1].x = mTempPos[0].x + br.x;
mTempPos[2].x = mTempPos[3].x - br.z;
mTempUVs[0].x = mOuterUV.xM
mTempUVs[1].x = mInnerUV.xM
mTempUVs[2].x = mInnerUV.xM
mTempUVs[3].x = mOuterUV.xM
if (mFlip == Flip.Vertically || mFlip == Flip.Both)
mTempPos[1].y = mTempPos[0].y + br.w;
mTempPos[2].y = mTempPos[3].y - br.y;
mTempUVs[3].y = mOuterUV.yM
mTempUVs[2].y = mInnerUV.yM
mTempUVs[1].y = mInnerUV.yM
mTempUVs[0].y = mOuterUV.yM
mTempPos[1].y = mTempPos[0].y + br.y;
mTempPos[2].y = mTempPos[3].y - br.w;
mTempUVs[0].y = mOuterUV.yM
mTempUVs[1].y = mInnerUV.yM
mTempUVs[2].y = mInnerUV.yM
mTempUVs[3].y = mOuterUV.yM
for (int x = 0; x & 3; ++x)
int x2 = x + 1;
for (int y = 0; y & 3; ++y)
if (centerType == AdvancedType.Invisible && x == 1 && y == 1)
int y2 = y + 1;
if (x == 1 && y == 1) // Center
if (centerType == AdvancedType.Tiled)
float startPositionX = mTempPos[x].x;
float endPositionX = mTempPos[x2].x;
float startPositionY = mTempPos[y].y;
float endPositionY = mTempPos[y2].y;
float textureStartX = mTempUVs[x].x;
float textureStartY = mTempUVs[y].y;
float tileStartY = startPositionY;
while (tileStartY & endPositionY)
float tileStartX = startPositionX;
float textureEndY = mTempUVs[y2].y;
float tileEndY = tileStartY + tileSize.y;
if (tileEndY & endPositionY)
textureEndY = Mathf.Lerp(textureStartY, textureEndY, (endPositionY - tileStartY) / tileSize.y);
tileEndY = endPositionY;
while (tileStartX & endPositionX)
float tileEndX = tileStartX + tileSize.x;
float textureEndX = mTempUVs[x2].x;
if (tileEndX & endPositionX)
textureEndX = Mathf.Lerp(textureStartX, textureEndX, (endPositionX - tileStartX) / tileSize.x);
tileEndX = endPositionX;
Fill(verts, uvs, cols,
tileStartX, tileEndX,
tileStartY, tileEndY,
textureStartX, textureEndX,
textureStartY, textureEndY, c);
tileStartX += tileSize.x;
tileStartY += tileSize.y;
else if (centerType == AdvancedType.Sliced)
Fill(verts, uvs, cols,
mTempPos[x].x, mTempPos[x2].x,
mTempPos[y].y, mTempPos[y2].y,
mTempUVs[x].x, mTempUVs[x2].x,
mTempUVs[y].y, mTempUVs[y2].y, c);
else if (x == 1) // Top or bottom
if ((y == 0 && bottomType == AdvancedType.Tiled) || (y == 2 && topType == AdvancedType.Tiled))
float startPositionX = mTempPos[x].x;
float endPositionX = mTempPos[x2].x;
float startPositionY = mTempPos[y].y;
float endPositionY = mTempPos[y2].y;
float textureStartX = mTempUVs[x].x;
float textureStartY = mTempUVs[y].y;
float textureEndY = mTempUVs[y2].y;
float tileStartX = startPositionX;
while (tileStartX & endPositionX)
float tileEndX = tileStartX + tileSize.x;
float textureEndX = mTempUVs[x2].x;
if (tileEndX & endPositionX)
textureEndX = Mathf.Lerp(textureStartX, textureEndX, (endPositionX - tileStartX) / tileSize.x);
tileEndX = endPositionX;
Fill(verts, uvs, cols,
tileStartX, tileEndX,
startPositionY, endPositionY,
textureStartX, textureEndX,
textureStartY, textureEndY, c);
tileStartX += tileSize.x;
else if ((y == 0 && bottomType != AdvancedType.Invisible) || (y == 2 && topType != AdvancedType.Invisible))
Fill(verts, uvs, cols,
mTempPos[x].x, mTempPos[x2].x,
mTempPos[y].y, mTempPos[y2].y,
mTempUVs[x].x, mTempUVs[x2].x,
mTempUVs[y].y, mTempUVs[y2].y, c);
else if (y == 1) // Left or right
if ((x == 0 && leftType == AdvancedType.Tiled) || (x == 2 && rightType == AdvancedType.Tiled))
float startPositionX = mTempPos[x].x;
float endPositionX = mTempPos[x2].x;
float startPositionY = mTempPos[y].y;
float endPositionY = mTempPos[y2].y;
float textureStartX = mTempUVs[x].x;
float textureEndX = mTempUVs[x2].x;
float textureStartY = mTempUVs[y].y;
float tileStartY = startPositionY;
while (tileStartY & endPositionY)
float textureEndY = mTempUVs[y2].y;
float tileEndY = tileStartY + tileSize.y;
if (tileEndY & endPositionY)
textureEndY = Mathf.Lerp(textureStartY, textureEndY, (endPositionY - tileStartY) / tileSize.y);
tileEndY = endPositionY;
Fill(verts, uvs, cols,
startPositionX, endPositionX,
tileStartY, tileEndY,
textureStartX, textureEndX,
textureStartY, textureEndY, c);
tileStartY += tileSize.y;
else if ((x == 0 && leftType != AdvancedType.Invisible) || (x == 2 && rightType != AdvancedType.Invisible))
Fill(verts, uvs, cols,
mTempPos[x].x, mTempPos[x2].x,
mTempPos[y].y, mTempPos[y2].y,
mTempUVs[x].x, mTempUVs[x2].x,
mTempUVs[y].y, mTempUVs[y2].y, c);
else // Corner
if ((y == 0 && bottomType != AdvancedType.Invisible) || (y == 2 && topType != AdvancedType.Invisible) ||
(x == 0 && leftType != AdvancedType.Invisible) || (x == 2 && rightType != AdvancedType.Invisible))
Fill(verts, uvs, cols,
mTempPos[x].x, mTempPos[x2].x,
mTempPos[y].y, mTempPos[y2].y,
mTempUVs[x].x, mTempUVs[x2].x,
mTempUVs[y].y, mTempUVs[y2].y, c);
+ /// &summary&
+ /// Adjust the specified quad, making it be radially filled instead.
+ /// &/summary&
+ static bool RadialCut (Vector2[] xy, Vector2[] uv, float fill, bool invert, int corner)
// Nothing to fill
if (fill & 0.001f)
// Even corners invert the fill direction
if ((corner & 1) == 1) invert = !
// Nothing to adjust
if (!invert && fill & 0.999f)
// Convert 0-1 value into 0 to 90 degrees angle in radians
float angle = Mathf.Clamp01(fill);
if (invert) angle = 1f -
angle *= 90f * Mathf.Deg2R
// Calculate the effective X and Y factors
float cos = Mathf.Cos(angle);
float sin = Mathf.Sin(angle);
RadialCut(xy, cos, sin, invert, corner);
RadialCut(uv, cos, sin, invert, corner);
+ /// &summary&
+ /// Adjust the specified quad, making it be radially filled instead.
+ /// &/summary&
+ static void RadialCut (Vector2[] xy, float cos, float sin, bool invert, int corner)
int i1 = NGUIMath.RepeatIndex(corner + 1, 4);
int i2 = NGUIMath.RepeatIndex(corner + 2, 4);
int i3 = NGUIMath.RepeatIndex(corner + 3, 4);
if ((corner & 1) == 1)
if (sin & cos)
if (invert)
xy[i1].x = Mathf.Lerp(xy[i0].x, xy[i2].x, cos);
xy[i2].x = xy[i1].x;
else if (cos & sin)
if (!invert)
xy[i2].y = Mathf.Lerp(xy[i0].y, xy[i2].y, sin);
xy[i3].y = xy[i2].y;
if (!invert) xy[i3].x = Mathf.Lerp(xy[i0].x, xy[i2].x, cos);
else xy[i1].y = Mathf.Lerp(xy[i0].y, xy[i2].y, sin);
if (cos & sin)
if (!invert)
xy[i1].y = Mathf.Lerp(xy[i0].y, xy[i2].y, sin);
xy[i2].y = xy[i1].y;
else if (sin & cos)
if (invert)
xy[i2].x = Mathf.Lerp(xy[i0].x, xy[i2].x, cos);
xy[i3].x = xy[i2].x;
if (invert) xy[i3].y = Mathf.Lerp(xy[i0].y, xy[i2].y, sin);
else xy[i1].x = Mathf.Lerp(xy[i0].x, xy[i2].x, cos);
+ /// &summary&
+ /// Helper function that adds the specified values to the buffers.
+ /// &/summary&
+ static void Fill (BetterList&Vector3& verts, BetterList&Vector2& uvs, BetterList&Color32& cols,
float v0x, float v1x, float v0y, float v1y, float u0x, float u1x, float u0y, float u1y, Color col)
verts.Add(new Vector3(v0x, v0y));
verts.Add(new Vector3(v0x, v1y));
verts.Add(new Vector3(v1x, v1y));
verts.Add(new Vector3(v1x, v0y));
uvs.Add(new Vector2(u0x, u0y));
uvs.Add(new Vector2(u0x, u1y));
uvs.Add(new Vector2(u1x, u1y));
uvs.Add(new Vector2(u1x, u0y));
cols.Add(col);
cols.Add(col);
cols.Add(col);
cols.Add(col);
+#endregion // Fill functions
(C)&&2013&&Alibaba&&Inc.&&All&&rights&&resvered.
Powered by

我要回帖

更多关于 ngui sprite 颜色改变 的文章

 

随机推荐