NGUI 图片字体 颜色渐变 或者 透明

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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值