194 lines
5.1 KiB
C#
194 lines
5.1 KiB
C#
using Thousandto.Code.Center;
|
||
using Thousandto.Plugins.Common;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
|
||
using System.Text;
|
||
using UnityEngine;
|
||
|
||
namespace Thousandto.GameUI.Form
|
||
{
|
||
/// <summary>
|
||
/// 直接为UISprite的对象上挂接这个脚本
|
||
/// 挂上后,sprite会有帧动画显示(如果name可以连续读取)
|
||
/// sprite在atlas中的名字必须是如下格式: name_index (sprite_0 sprite_1 sprite_2 ...)
|
||
/// </summary>
|
||
public class UISpriteAnim : MonoCacheBase
|
||
{
|
||
#region 私有成员变量
|
||
//当前sprite组件
|
||
private UISprite _currentSprite;
|
||
|
||
//sprite在atlas中的名字
|
||
private string _spName;
|
||
|
||
//如果当前是sprite组件,则支持动画
|
||
private bool _supportAnim;
|
||
|
||
//是否循环播放
|
||
private bool _loop;
|
||
|
||
//如果不是loop,则会显示指定的随机帧
|
||
private int _randomFrame;
|
||
|
||
//指定循环播放几次
|
||
private int _loopTimes;
|
||
|
||
//是否指定循环播放
|
||
private bool _limitLoop;
|
||
|
||
//是否开始播放
|
||
private bool _play;
|
||
|
||
//设置sprite最大支持的动画帧数
|
||
private int _animImgMaxCount = 100;
|
||
|
||
//播放的sprite序列号
|
||
private int _animImgIndex = 0;
|
||
|
||
//sprite轮换速度,即播放动画的速率,这里是0.1秒播放1帧
|
||
private float _speed = 0.1f;
|
||
|
||
//update函数在播放一帧动画前累计的时间,如果比较卡,update刷新一次就可能超过0.1秒
|
||
//所有就需要用累积时间计算当前应该播放的动画帧,可能会跳过几帧动画
|
||
private float _timeDelta = 0;
|
||
|
||
//sprite在altas中所有一系列的名字,用于播放动画
|
||
private List<string> _spAnimNameList = new List<string>();
|
||
#endregion
|
||
|
||
|
||
#region//继承MonoBehaviour
|
||
private void Awake()
|
||
{
|
||
Initialize();
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
}
|
||
|
||
//初始化
|
||
private void OnEnable()
|
||
{
|
||
_timeDelta = 0;
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (_supportAnim && _play && _spAnimNameList.Count > 0)
|
||
{
|
||
_timeDelta += Time.deltaTime;
|
||
|
||
//计算需要播放的是后续的第几帧动画,当count>0才播放
|
||
int count = (int)(_timeDelta / _speed);
|
||
if (count == 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
playNextAnim(count);
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
public void Initialize()
|
||
{
|
||
_spAnimNameList.Clear();
|
||
|
||
_currentSprite = transform.GetComponent<UISprite>();
|
||
if (_currentSprite != null)
|
||
{
|
||
_spName = _currentSprite.spriteName;
|
||
|
||
//有"_"作分割,说明是有系列sprite,能播放动画
|
||
if (!_spName.Contains("_"))
|
||
{
|
||
return;
|
||
}
|
||
_supportAnim = true;
|
||
|
||
string spriteHeadName = _spName.Split('_')[0];
|
||
string spriteAnimName = null;
|
||
for (int i = 1; i < _animImgMaxCount; ++i)
|
||
{
|
||
spriteAnimName = string.Format("{0}_{1}", spriteHeadName, i);
|
||
if (_currentSprite.atlas.GetSprite(spriteAnimName) == null)
|
||
{
|
||
break;
|
||
}
|
||
|
||
_spAnimNameList.Add(spriteAnimName);
|
||
}
|
||
}
|
||
_loop = true;
|
||
}
|
||
|
||
public void Play()
|
||
{
|
||
_play = true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置最后展示的动画帧
|
||
/// </summary>
|
||
public void SetShowFrame(int index)
|
||
{
|
||
_randomFrame = index;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置循环次数, 0 为不循环, 默认3次
|
||
/// </summary>
|
||
public void SetLoopTimes(int times = 3)
|
||
{
|
||
_loopTimes = times;
|
||
_limitLoop = true;
|
||
}
|
||
|
||
public void Pause()
|
||
{
|
||
_play = false;
|
||
}
|
||
|
||
public void Stop()
|
||
{
|
||
_play = false;
|
||
_animImgIndex = 0;
|
||
_timeDelta = 0;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 播放后续第几帧动画
|
||
/// </summary>
|
||
/// <param name="count">第几帧</param>
|
||
private void playNextAnim(int count)
|
||
{
|
||
_animImgIndex += count;
|
||
|
||
|
||
//循环次数
|
||
if (_limitLoop && _animImgIndex == _spAnimNameList.Count - 1)
|
||
{
|
||
_loopTimes -= 1;
|
||
if (_loopTimes < 0)
|
||
{
|
||
_animImgIndex = _randomFrame;
|
||
Pause();
|
||
}
|
||
}
|
||
|
||
if (_animImgIndex >= _spAnimNameList.Count)
|
||
{
|
||
_animImgIndex = 0;
|
||
}
|
||
|
||
_currentSprite.spriteName = _spAnimNameList[_animImgIndex];
|
||
_currentSprite.color = Color.white;
|
||
//当播放了下一帧动画后,累积时间清0
|
||
_timeDelta = 0;
|
||
}
|
||
}
|
||
}
|