NGUI中texture序列帧图片播放

using UnityEngine;
using System.Collections;

public class FullTextureAnim : MonoBehaviour {

    public int mFPS = 30;
    public bool loop;
    public bool mActive = true;
    public bool isPlayback = false;
    public string path = "";
    public Texture[] textures;
    public bool isFinished = false;

    private UITexture uitexture;

    float mDelta = 0f;
    int mIndex = 0;

    private string temppath;

    private float camMove = 0;
    private TweenAlpha ta;
    void Start()
    {
        uitexture = GetComponent<UITexture>();

        if (textures[0] == null)
        {
            for (int i = 0; i < textures.Length; i++)
            {
                temppath = path + IntToStr(i);
                //Debug.Log(temppath);
                textures[i] = Resources.Load<Texture>(temppath);
            }
        }

        ta = GetComponent<TweenAlpha>();

    }
    private Vector3 posText;
    void OnEnable()
    {
        if (uitexture)
        {
            Debug.Log("OnEnable: " + gameObject.name);
            uitexture.enabled = true;

            //mActive = true;
            ResetTexAnim();
        }

        
    }
    void OnDisable()
    {
        if (uitexture)
        {
            uitexture.enabled = false;
            mActive = false;
        }

        for (int i = 0; i < textures.Length; i++)
        {
            //Debug.Log("OnDisable");
            Resources.UnloadAsset(textures[i]);
        }
    }
    string IntToStr(int num)
    {
        if (num.ToString().Length == 1)
        {
            return "0000" + num.ToString();
        }
        else if (num.ToString().Length == 2)
        {
            return "000" + num.ToString();
        }
        else if (num.ToString().Length == 3)
        {
            return "00" + num.ToString();
        }
        return "";
    }
    /// <summary>
    /// Advance the sprite animation process.
    /// </summary>

    void Update()
    {
        posText = new Vector3(Camera.main.transform.position[0], transform.position[1], transform.position[2]);
        transform.position = posText;

        if (mActive && textures.Length > 1 && Application.isPlaying && mFPS > 0f)
        {
            //mDelta += RealTime.deltaTime;
            mDelta += Time.deltaTime;
            float rate = 1f / mFPS;

            if (rate < mDelta)
            {

                mDelta = (rate > 0f) ? mDelta - rate : 0f;
                if (++mIndex >= textures.Length)
                {
                    mIndex = 0;
                    mActive = loop;
                    isFinished = true;
                    Debug.Log("end:" + Time.time);
                }

                if (mActive && !isPlayback) //forward
                {
                    uitexture.mainTexture = textures[mIndex];
                    isFinished = false;
                    //Debug.Log("forward");
                }
                else if (mActive && isPlayback)
                {
                    Debug.Log(textures.Length - mIndex);
                    uitexture.mainTexture = textures[textures.Length - mIndex];
                    isFinished = false;
                }
            }         
        }
    }

    public void ResetTexAnim()
    {
        posText = new Vector3(Camera.main.transform.position[0], transform.position[1], transform.position[2]);
        transform.position = posText;

        mActive = true;
        isPlayback = false;
        mIndex = 0;
        camMove = Camera.main.transform.position.x;

        if (uitexture != null && textures.Length > 0)
        {
            uitexture.mainTexture = textures[mIndex];
        }

    }

    public void ResetTexAnimBackplay()
    {
        mActive = true;
        isPlayback = true;
        mIndex = 0;
        camMove = Camera.main.transform.position.x;

        if (uitexture != null && textures.Length > 0)
        {
            //uitexture.mainTexture = textures[mIndex];
        }

    }

    public void holdFirst()
    {
        mActive = false;
        uitexture.mainTexture = textures[0];
    }

    public void setEnable()
    {
        uitexture.enabled = true;
    }

    public void setDisable()
    {
        uitexture.enabled = false;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值