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;
    }
}

你可能感兴趣的:(NGUI中texture序列帧图片播放)