Since the assembly sequence is defined Unity frame playback component

  We know that playing in unity in frame animation sequence in two ways, first is to use animation components Unity comes to play, we just need to select the Select All we need to play all pictures in the project directory, drag it to the on Hiercarchy, Unity will help us to automatically create an animation clip, we can use animation components to control our animation, but the picture Sprite Renderer type created in this way. The second way is to create an Image component, use the code to create a sprite, write a piece of code using the Update function to replace the Image of the sprite frame by frame to achieve the animation. Such words may be trouble spots, but the degree of freedom that can be controlled to write code according to their needs

  Due to recent project requires a great deal of logic and sequence frame of the animation process, had wanted to use Unity comes Animation components to achieve, but because the Party needs to change again and again, you need to deal with too much logic, in order to facilitate the modification and expansion , so the project according to the needs of their own custom components play a sequence of frames to assist in the development. First affix pictures to see the effect, as shown below is this ImageAnimation script component, we only need to create a Image, then mount on the script, you will need to play an array of images assigned to Sprite on the script, according to their own needs can edit panel pictures set loop mode, playback speed, and whether auto play, you can add the callback function according to their needs. very convenient.

  Well, ado, on the code, open the script came out to play Play (), pause Pause (), Stop Stop (), Replay Replay () 4 public method, and a callback function, according to their needs directly this four calls and callback methods to control their own picture playback controls just fine. The script is not very complicated, mainly we hope to provide an idea can be encapsulated in project development according to their own needs out some of the features to supplement their own development in order to improve efficiency. Share this on here, you have any questions or comments can communicate with me, discuss, learn together and progress.

/ * ********************************** 
* the Description: Description This is a picture frame sequence playback scripts 
* mountpoint: mount point mount it on the Image component 
* a Date: 2019.07.11 
* version: Unity version 2017.2.0f3 
* Author: LJF 
******************* *************** * / 
the using the System.Collections;
 the using the System.Collections.Generic;
 the using UnityEngine;
 the using UnityEngine.UI;
 the using LjfLog;
 the using UnityEngine.Events; 

namespace LJF 
{ 
    // naming specification, add comments, rational packaging, restrict access, exception handling     
    public  class ImageAnimation: MonoBehaviour 
    { 
        public enum State 
        { 
            IDLE, 
            Playing, 
            PAUSE 
        } 
        public  enum Statel 
        { 
            Once, 
            Loop 
        } 
       
        [Header ( " play mode (circulation, single) " )] // default single 
        public Statel for condition Condition = State1.once; 
        [Header ( " Auto play " )] // default does not automatically play 
        public  BOOL Play_Awake = to false ;
         // playback status (default, playback, pause) 
        Private  Play_state State;
         Private Image manimg; 
        [Header ( " frames each second (integer) " )]
         public  float Frame_Number = 30 ; 
        [Header ( " the sprite was dropped Array " )]
         public Sprite [] sprit_arr;
         // callback 
        public UnityEvent onCompleteEvent;
         Private  int index;
         Private  a float Tim;
         Private  a float waittim;
         Private  BOOL DisplayRID;
         void the Awake () 
        {
            manimg = GetComponent<Image>();
            tim = 0;
            index = 0;
            waittim = 1 / frame_number;
            play_state = State.idle;
            isplay = false;
            if (manimg == null)
            {
                Debuger.LogWarning("Image为空,请添加Image组件!!!");
                return;
            }
            if(sprit_arr.Length < . 1 ) 
            { 
                Debuger.LogWarning ( " sprite array is 0, add elements to the array sprite !!! " ); 
            } 
            manimg.sprite = sprit_arr [ 0 ];
             IF (Play_Awake) 
            { 
                Play (); 
            } 
        } 
        void the Update () 
        { 
            // test 
            IF (Input.GetKeyDown (KeyCode.A)) 
            { 
                Play (); 
            } 
            IF (Input.GetKeyDown (KeyCode.S)) 
            { 
                Replay (); 
            } 
            IF (Input.GetKeyDown(KeyCode.D))
            {
                Stop();
            }
            if (Input.GetKeyDown(KeyCode.P))
            {
                Pause();
            }
            UpMove();
           
        }

        private void UpMove()
        {
            //单播
            if (condition == State1.once)
            {
                if (play_state == State.idle && isplay)
                {
                    play_state = State.playing;
                    index = 0;
                    tim = 0;
                }
                if (play_state == State.pause && isplay)
                {
                    play_state = State.playing;
                    tim = 0;
                }
                if (play_state == State.playing && isplay)
                {
                    tim += Time.deltaTime;
                    if (tim >= waittim)
                    {
                        tim = 0 ; 
                        index ++ ;
                         IF (index> = sprit_arr.Length) 
                        { 
                            index = 0 ; 
                            manimg.sprite = sprit_arr [index]; 
                            DisplayRID = to false ; 
                            play_state = State.idle;
                             // here to add callback end 
                            IF ( onCompleteEvent! = null ) 
                            { 
                                onCompleteEvent .Invoke (); 
                                return ; 
                            } 
                        }
                        manimg.sprite = sprit_arr[index];
                    }
                }
            }
            //循环播放
            if (condition == State1.loop)
            {
                if (play_state == State.idle && isplay)
                {
                    play_state = State.playing;
                    index = 0;
                    tim = 0;
                }
                if (play_state == State.pause && isplay)
                {
                    play_state = State.playing;
                    tim = 0;
                }
                if (play_state == State.playing && isplay)
                {
                    tim += Time.deltaTime;
                    if (tim >= waittim)
                    {
                        tim = 0;
                        index++;
                        if (index >= sprit_arr.Length)
                        {
                            index = 0;
                             // here to add callback function ends 
                        } 
                        manimg.sprite = sprit_arr [index]; 
                    } 
                } 
            } 
        } 
        ///  <Summary> 
        /// play
         ///  </ Summary> 
        public  void Play () 
        { 
            DisplayRID = to true ; 
        } 
        ///  <Summary> 
        /// suspended
         ///  </ Summary> 
        public  void the pause () 
        {
            isplay = false;
            play_state = State.pause;
        }
        /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
            isplay = false;
            play_state = State.idle;
            index = 0;
            tim = 0;
            if (manimg == null)
            {
                Debuger.LogWarning("Image is empty, the assignment " );
                 return ; 
            } 
            manimg.sprite = sprit_arr [index]; 
        } 
        ///  <Summary> 
        /// replay
         ///  </ Summary> 
        public  void Replay () 
        { 
            DisplayRID = to true ; 
            play_state = State.playing; 
            index = 0 ; 
            Tim = 0 ; 
        } 

    } 
}

 

Guess you like

Origin www.cnblogs.com/linkshow/p/11498661.html
Recommended