赞
踩
效果1:从头播放到尾
效果2:重复播放
效果3:正放、倒放交替播放
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class XuLieZhen : MonoBehaviour { [Header("序列帧路径")] public string Path; [Header("更换间隔")] public float time = 0.01f; float delTime; [Header("是否循环播放")] bool IsLoop; [Header("从第几张开始循环")] public int LoopIndex; [Header("来回效果,勾选isloop无效")] bool IsPingPang; int index = 0; List<Sprite> sprites = new List<Sprite>(); bool IsPlay; bool iscanPingPang;//记录初始状态 public enum LoopModle { NoLoop, CommonLoop, PingPang } public LoopModle loopPattern = new LoopModle(); /// <summary> /// 开始读取 /// </summary> private void Awake() { delTime = time; Resources.UnloadUnusedAssets(); for (int i = 0; i < Resources.LoadAll<Sprite>(Path).Length; i++) { sprites.Add(Resources.LoadAll<Sprite>(Path)[i]); } iscanPingPang = IsPingPang; } /// <summary> /// 开始时 初始化状态 /// </summary> private void OnEnable() { IsPingPang = iscanPingPang; //if (!IsPingPang) IsPlay = true; //if (IsPingPang) StartCoroutine("PingPang"); switch (loopPattern) { case LoopModle.NoLoop: IsPlay = true; IsLoop = false; break; case LoopModle.CommonLoop: IsPlay = true; IsLoop = true; break; case LoopModle.PingPang: IsPingPang = true; StartCoroutine("PingPang"); break; } } /// <summary> /// 结束时 初始化状态 /// </summary> private void OnDisable() { index = 0; transform.GetComponent<Image>().sprite = sprites[0]; } void Update() { if (IsPlay) ChangePictures(IsLoop); } /// <summary> /// 播放序列帧 /// </summary> /// <param name="isLoop">是否循环播放</param> void ChangePictures(bool isLoop) { time -= Time.deltaTime; if (time < 0) { transform.GetComponent<Image>().sprite = sprites[index]; time = delTime; index++; if (index >= sprites.Count) { if (isLoop) { index = LoopIndex; } else { index = sprites.Count - 1; IsPlay = false; } } } } /// <summary> /// pingpang效果 /// </summary> /// <returns></returns> IEnumerator PingPang() { transform.GetComponent<Image>().sprite = sprites[index]; yield return new WaitForSeconds(delTime); if (IsPingPang) { index++; if (index > sprites.Count - 1) { index = sprites.Count - 1; IsPingPang = false; } } else { index--; if (index < LoopIndex) { index = LoopIndex; IsPingPang = true; } } StopCoroutine("PingPang"); StartCoroutine("PingPang"); } }
代码很简单,怎么使用直接看代码就能明白。关键是要填好面板上的信息。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。