Unity Play sequence Frames

Source: Internet
Author: User

I wrote a small script that played a sequence frame a while ago, and thought it was a temporary use, but later found that the script could be used frequently elsewhere in the project, so I decided to optimize it.
This script refers to Ngui's ui2dspriteanimation script, which has a lot of good ideas, just doing some tinkering.

Just attach the script to any game object, Animimage variable is the Ugui Image to play the sequence frame
OK, on the script

Using unityengine;using unityengine.ui;///<summary>///play 2D sequence frames///</summary>public class SpriteAnimation  : monobehaviour{///<summary>///////for playing the sequence frame UI image///</summary> [Serializefield] Private Image    Animimage;    <summary>///The index of the currently played frame in the array.    The number must not be greater than or equal to the sequence frame array index when it is playing forward and not looping.    The number cannot be less than or equal to 0//</summary> public int frameindex = 0 in reverse playback without looping; <summary>///per second how many frames (not 0)////number greater than 0 for positive playback, number less than 0 for reverse play///</summary> [Serializefield] pr    ivate int framerate = 20;    <summary>//Whether affected by timescale///</summary> public bool Ignoretimescale = true;    <summary>///Whether the loop plays///</summary> public bool Isloop = true;    <summary>//////storage of all sequence frame Sprite arrays///</summary> public sprite[] frames;    <summary>///Sequence frame update time///</summary> private float mupdate = 0f; <summary>//Continue Playing The animation.        If the animation has reached the end, it'll restart from beginning//</summary> public void Play () { if (!enabled &&!isloop) {//decide to play forward or reverse the int newindex = framerate > 0? fra            Meindex + 1:frameindex-1; if (NewIndex < 0 | | NewIndex >= frames. Length) {FrameIndex = framerate < 0? frames.            Length-1:0;        }} enabled = True;    Updatesprite ();    }///<summary>///Pause play///</summary> public void Pause () {enabled = false; }///<summary>///back to the original sequence frame, waiting to play//</summary> public void resettobeginning () {Framei Ndex = framerate < 0? Frames.        Length-1:0;    Updatesprite (); } void Update () {//Gets the current in-game time, which is used to compare with the sequence frame update time?        Time.unscaledTime:Time.time; This will be the current game time and the last updated sequence frame time(mupdate) For comparison//if mupdate<time, Description: The time before the update sequence frame + sequence frame two intervals is less than the in-game time, you need to unify two timelines, and then update the sequence frame picture//If mupdate> =time, Description: Update sequence frames before the time + sequence frame two time intervals greater than equal to the in-game time, need to wait for in-game time to continue to grow, do not update the sequence frame picture//This procedure can guarantee the maximum program between two sequence frames between the interval is approximately equal to 1/second play frame number if (m            Update < time) {mupdate = time; Determine if you are playing forward or reverse the int newindex = framerate > 0?            FrameIndex + 1:frameindex-1; If the current index is less than 0, the forward play Sequence frame index is greater than the total length of the sequence frame (!isloop && (Newin) if the playback sequence frame does not loop Dex < 0 | | NewIndex >= frames.                Length) {enabled = false;            Return }//Determine if the sequence frame index is out of bounds FrameIndex = Repeatindex (NewIndex, frames.            Length);        Updatesprite (); }}///<summary>//Update sequence frame picture///</summary> void Updatesprite () {Float time = Ignor Etimescale?        Time.unscaledTime:Time.time; Each time the sequence frame picture is updated, the sequence frame time is updated//The new sequence frame time is: currentIn-game time + interval between every two frames (interval is 1 seconds/sequence frame number) Mupdate = time + mathf.abs (1f/framerate);    Update sequence frame picture animimage.sprite = Frames[frameindex];        }//used to determine if the sequence frame index is out of bounds public int repeatindex (int val, int max) {if (Max < 1) return 0;        while (Val < 0) val + = max;        while (Val >= max) Val-= max;    return Val; }}

Unity Play sequence frames

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.