Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of sequence frame playback component of Unity Custom component

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to solve the Unity custom component sequence frame playback component problem", the content is easy to understand, clear, hope to help you solve the doubt, the following let Xiaobian lead you to study and learn "how to solve the Unity custom component sequence frame playback component problem" this article.

We know that there are two ways to play sequence frame animation in unity. The first is to use the animation component of Unity to play. We just need to select all the pictures we need to play in the project directory, drag them to Hiercarchy, and Unity will help us automatically create an animation clip, and we can use animation components to control our animation, but the pictures created in this way are of Sprite Renderer type. The second way is to create an Image component, use the code to create a sprite, write a piece of code to use the Update function to replace Image's sprite frame by frame to achieve animation playback. This kind of words may be troublesome, but with a higher degree of freedom, you can write code according to your own needs.

As a result of the recent project needs to use a large number of sequence frame animation and logic processing, originally intended to use Unity's own Animation components to achieve, but because Party A needs to change again and again, need to deal with too much logic, in order to facilitate modification and expansion, so according to the needs of their own project customized a sequence frame playback component to assist development. First paste the picture to see the effect, such as the following figure is the ImageAnimation script component, we just need to create an Image, and then mount the script, will need to play the picture to the script Sprite array, according to their own needs in the editing panel to set the image loop playback mode, playback speed, and whether to automatically play, but also according to their own needs to add callback function. It's convenient.

All right, no more nonsense, on the code, the script is open to play Play (), pause Pause (), stop Stop (), replay Replay () four public methods, as well as a callback function, according to their own needs to directly call these four methods and callbacks to control their own picture playback control. The script is not very complex, mainly hope to provide you with an idea, in the project development can be based on their own needs to encapsulate some functions to assist their own development, in order to improve efficiency. This is the end of this sharing, you can communicate and discuss with me if you have any questions and opinions, and learn and make progress together.

/ * * Description: describes this as a picture sequence frame playback script * Mountpoint: Mount it on the Image component at the mount point * Date:2019.07.11* Version:unity version 2017.2.0f3 * Author:LJF**/using System.Collections Using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using LjfLog;using UnityEngine.Events Namespace LJF {/ / canonical naming, adding comments, reasonable encapsulation, restricting access rights, exception handling public class ImageAnimation: MonoBehaviour {public enum State {idle, playing, pause} public enum State1 {once Loop} [Header ("playback method (Loop, single)")] / / default single public State1 condition=State1.once [Header ("Auto play")] / / does not play automatically by default public bool Play_Awake = false; / / playback status (default, playing, paused) private State play_state; private Image manimg; [Header ("frames per second (integer)")] public float frame_number=30 [Header ("sprite array")] public Sprite [] sprit_arr; / / callback event public UnityEvent onCompleteEvent; private int index; private float tim; private float waittim; private bool isplay; void Awake () {manimg = GetComponent (); tim = 0; index = 0 Waittim = 1 / frame_number; play_state = State.idle; isplay = false; if (manimg = = null) {Debuger.LogWarning ("Image is empty, please add Image component!") ; return;} if (sprit_arr.Length= waittim) {tim = 0; index++; if (index > = sprit_arr.Length) {index = 0 Manimg.sprite = sprit_ arr [index]; isplay = false; play_state = State.idle / / you can add the ending callback function if (onCompleteEvent! = null) {onCompleteEvent.Invoke (); return here }} manimg.sprite = sprit_ arr [index] } / / play if in a loop (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; / / you can add the ending callback function} manimg.sprite = sprit_ arr [index] } / play / public void Play () {isplay = true } / pause / public void Pause () {isplay = false; play_state = State.pause;} / stop / public void Stop () {isplay = false Play_state = State.idle; index = 0; tim = 0; if (manimg = = null) {Debuger.LogWarning ("Image is empty, please assign a value"); return;} manimg.sprite = sprit_ arr [index] } / replay / public void Replay () {isplay = true; play_state = State.playing; index = 0; tim = 0 } these are all the contents of this article entitled "how to solve the problem of sequence frame playback components of Unity custom components". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 226

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report