Wednesday, September 24, 2014

AnimationController for the frames not in one texture

using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
    public Texture [] frames;
    public bool direction=true;//head direction
    public bool destroy=false;//Destroy the animation after finish playing
    public int lastFrameNo=0//stop at a certain frame
    public bool oneTime=false;//do not repeat animation

    private int frameNumber=0;
    private int index=0;
    private float myTime=0;//time counter
    private int myIndex=0;

    // Update is called once per frame
    void Update () {
        frameNumber = frames.Length;

        if(!oneTime){
            myTime+=Time.deltaTime;
            myIndex=(int)(myTime*(frameNumber-1));//(frameNumber-1makes all animations played in 1s
            index=myIndex%frameNumber;
        }
        renderer.material.mainTexture=frames[index];//assign the frame to object

        if (direction) {
            renderer.material.mainTextureScale=new Vector2(1.0f,1);//set the heading direction
        }else{
            renderer.material.mainTextureScale=new Vector2(1.0f,1);
        }

        if (index == frameNumber - 1 && destroy) {
            //Destroy the animation
            Destroy(gameObject);
        }

        if (lastFrameNo != 0) {
            //stay on the certain frame
            if(index==lastFrameNo-1){
                oneTime=true;
            }
        }
    }
}

No comments:

Post a Comment