Files
2025-01-25 04:38:09 +08:00

194 lines
5.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}