NGUI 图片字体 颜色渐变 或者 透明
using
UnityEngine;
using
System.Collections;
///
<summary>
///
颜色渐变类,本类使用方式,直接调用 不用挂任何物体
///
</summary>
public
class
ColorTransform
:
MonoBehaviour
{
private
static
int
index = 0;
private
Color
curColor;
private
Object
main;
///
<summary>
///
渐变颜色的Color.r值
///
</summary>
///
<param name="
main
">
传UILabel 或者 UISprite
</param>
///
<param name="
fromColor
">
0-255
</param>
///
<param name="
toColor
">
0-255
</param>
///
<param name="
useTime
">
变化到目标颜色用时,单位秒
</param>
///
<param name="
delayTime
">
延迟多久开始,单位秒
</param>
public
static
void
TransformR(
Object
main,
float
fromColor,
float
toColor,
float
useTime,
float
delayTime)
{
GameObject
go =
new
GameObject
((index++) +
". ColorTransform"
);
ColorTransform
ct =go.AddComponent<
ColorTransform
>();
ct.Play_r(main,fromColor,toColor,useTime,delayTime);
}
public
void
Play_r(
Object
main,
float
fromColor,
float
toColor,
float
useTime,
float
delayTime)
{
this
.main = main;
Hashtable
htColor =
new
Hashtable
();
htColor.Add(
"from"
, fromColor);
htColor.Add(
"to"
, toColor);
htColor.Add(
"time"
, useTime);
htColor.Add(
"delay"
, delayTime);
htColor.Add(
"onupdate"
,
"UpdateColor"
);
htColor.Add(
"onupdatetarget"
,
this
.gameObject);
iTween
.ValueTo(
this
.gameObject, htColor);
GameObject
.DestroyObject(gameObject, useTime + delayTime + 0.3f);
}
///
<summary>
///
渐变颜色的Color.a值 透明值
///
</summary>
///
<param name="
main
">
传UILabel 或者 UISprite
</param>
///
<param name="
fromColor
">
0-255
</param>
///
<param name="
toColor
">
0-255
</param>
///
<param name="
useTime
">
变化到目标颜色用时,单位秒
</param>
///
<param name="
delayTime
">
延迟多久开始,单位秒
</param>
public
static
void
TransformAlpha(
Object
main,
float
fromColor,
float
toColor,
float
useTime,
float
delayTime)
{
GameObject
go =
new
GameObject
((index++) +
". ColorTransform"
);
ColorTransform
ct = go.AddComponent<
ColorTransform
>();
ct.PlayAlpha(main, fromColor, toColor, useTime, delayTime);
}
public
void
PlayAlpha(
Object
main,
float
fromColor,
float
toColor,
float
useTime,
float
delayTime)
{
this
.main= main;
Hashtable
htColor =
new
Hashtable
();
htColor.Add(
"from"
, fromColor);
htColor.Add(
"to"
, toColor);
htColor.Add(
"time"
, useTime);
htColor.Add(
"delay"
, delayTime);
htColor.Add(
"onupdate"
,
"UpdateAlpha"
);
htColor.Add(
"onupdatetarget"
,
this
.gameObject);
iTween
.ValueTo(
this
.gameObject, htColor);
GameObject
.DestroyObject(gameObject,useTime+delayTime+0.3f);
}
void
UpdateAlpha(
float
t)
{
Color
r = SetColor(main,
false
);
r.a = t / 255;
curColor = r;
SetColor(main);
}
///
<summary>
///
判断类型,然后设置颜色,返回当前控件颜色
///
</summary>
///
<param name="
obj
"></param>
///
<param name="
isSet
">
是否设置颜色,是则设置成curColor颜色
</param>
///
<returns></returns>
Color
SetColor(
Object
obj,
bool
isSet=
true
)
{
if
(obj ==
null
)
{
}
else
{
if
(obj.GetType() ==
typeof
(
UILabel
))
{
UILabel
uiLabel = (
UILabel
) obj;
if
(isSet) uiLabel.color = curColor;
return
uiLabel.color;
}
if
(obj.GetType() ==
typeof
(
UISprite
))
{
UISprite
uiLabel = (
UISprite
)obj;
if
(isSet) uiLabel.color = curColor;
return
uiLabel.color;
}
}
return
Color
.white;
}
void
UpdateColor(
float
t)
{
Color
r = SetColor(main,
false
);
r.r = t / 255;
curColor = r;
SetColor(main);
}
}