(client) feat:状态UI
This commit is contained in:
138
Client/Assets/Scripts/Prefab/BaseAnimator.cs
Normal file
138
Client/Assets/Scripts/Prefab/BaseAnimator.cs
Normal file
@ -0,0 +1,138 @@
|
||||
// Base/BaseAnimator.cs
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using Base; // 假设ITick接口在这个命名空间
|
||||
|
||||
namespace Base
|
||||
{
|
||||
// 抽象基类,封装通用动画逻辑
|
||||
public abstract class BaseAnimator : MonoBehaviour, ITick
|
||||
{
|
||||
// 通用公开字段(可在编辑器中设置)
|
||||
[SerializeField] protected Sprite[] _sprites; // 动画精灵序列
|
||||
[SerializeField] protected float _fps = 2; // 每秒帧数
|
||||
[SerializeField] protected Sprite _staticSprite; // 暂停时的静态精灵
|
||||
|
||||
// 通用内部状态
|
||||
protected bool _isPaused; // 暂停状态
|
||||
protected float _frameTimer; // 帧计时器
|
||||
protected int _currentFrameIndex; // 当前帧索引
|
||||
|
||||
// 抽象方法:子类必须实现以获取并验证其特有的显示组件
|
||||
protected abstract void ValidateComponent();
|
||||
|
||||
// 抽象方法:子类必须实现以设置实际显示组件的Sprite
|
||||
protected abstract void SetDisplaySprite(Sprite sprite);
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
ValidateComponent(); // 子类获取组件
|
||||
ValidateStartFrame(); // 初始化第一帧
|
||||
}
|
||||
|
||||
// ITick接口实现
|
||||
public void Tick()
|
||||
{
|
||||
var deltaTime = Time.deltaTime;
|
||||
if (_isPaused)
|
||||
{
|
||||
HandlePausedState();
|
||||
return;
|
||||
}
|
||||
|
||||
PlayAnimation(deltaTime);
|
||||
}
|
||||
|
||||
protected void ValidateStartFrame()
|
||||
{
|
||||
// 确保有精灵时可显示有效帧
|
||||
if (_sprites != null && _sprites.Length > 0)
|
||||
{
|
||||
_currentFrameIndex = Mathf.Clamp(_currentFrameIndex, 0, _sprites.Length - 1);
|
||||
SetDisplaySprite(_sprites[_currentFrameIndex]); // 调用抽象方法设置Sprite
|
||||
}
|
||||
else
|
||||
{
|
||||
SetDisplaySprite(null); // 调用抽象方法清空Sprite
|
||||
}
|
||||
}
|
||||
|
||||
protected void HandlePausedState()
|
||||
{
|
||||
// 优先使用静态精灵,否则保持当前帧
|
||||
if (_staticSprite)
|
||||
{
|
||||
SetDisplaySprite(_staticSprite); // 调用抽象方法设置Sprite
|
||||
}
|
||||
// 否则,保持当前显示的Sprite,不需要额外操作(SetDisplaySprite已在NextFrame中设置)
|
||||
}
|
||||
|
||||
protected void PlayAnimation(float deltaTime)
|
||||
{
|
||||
if (_sprites == null || _sprites.Length == 0)
|
||||
{
|
||||
// 如果没有精灵,确保显示组件的Sprite被清除
|
||||
SetDisplaySprite(null);
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新帧计时器
|
||||
_frameTimer += deltaTime;
|
||||
var frameDuration = 1f / _fps;
|
||||
|
||||
// 检查帧切换条件
|
||||
while (_frameTimer >= frameDuration)
|
||||
{
|
||||
_frameTimer -= frameDuration;
|
||||
NextFrame();
|
||||
}
|
||||
}
|
||||
|
||||
protected void NextFrame()
|
||||
{
|
||||
if (_sprites == null || _sprites.Length == 0) return;
|
||||
|
||||
// 循环播放动画
|
||||
_currentFrameIndex = (_currentFrameIndex + 1) % _sprites.Length;
|
||||
SetDisplaySprite(_sprites[_currentFrameIndex]); // 调用抽象方法更新Sprite
|
||||
}
|
||||
|
||||
// 外部控制方法
|
||||
public void SetPaused(bool paused) => _isPaused = paused;
|
||||
|
||||
public void SetSprites(Sprite[] newSprites)
|
||||
{
|
||||
_sprites = newSprites;
|
||||
|
||||
// 如果有新的精灵数组,则立即显示第一帧
|
||||
if (_sprites != null && _sprites.Length > 0)
|
||||
{
|
||||
_currentFrameIndex = 0; // 重置当前帧索引为第一帧
|
||||
SetDisplaySprite(_sprites[_currentFrameIndex]); // 立即显示第一帧
|
||||
}
|
||||
else
|
||||
{
|
||||
SetDisplaySprite(null); // 如果没有精灵,则清空渲染器
|
||||
}
|
||||
|
||||
// 重置帧计时器,以确保从头开始播放
|
||||
_frameTimer = 0f;
|
||||
}
|
||||
|
||||
public void Restore()
|
||||
{
|
||||
_currentFrameIndex = 0;
|
||||
if (_sprites != null && _sprites.Length > 0)
|
||||
{
|
||||
SetDisplaySprite(_sprites[_currentFrameIndex]); // 恢复到第一帧
|
||||
}
|
||||
else
|
||||
{
|
||||
SetDisplaySprite(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetFPS(float newFPS) => _fps = Mathf.Max(0.1f, newFPS);
|
||||
public void SetStaticSprite(Sprite sprite) => _staticSprite = sprite;
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Prefab/BaseAnimator.cs.meta
Normal file
3
Client/Assets/Scripts/Prefab/BaseAnimator.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 84dda25302c44253949e7818cd62b7e7
|
||||
timeCreated: 1756778358
|
10
Client/Assets/Scripts/Prefab/BuffIconUI.cs
Normal file
10
Client/Assets/Scripts/Prefab/BuffIconUI.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using UI;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
public class BuffIconUI: MonoBehaviour
|
||||
{
|
||||
public UIImageAnimator icon;
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Prefab/BuffIconUI.cs.meta
Normal file
3
Client/Assets/Scripts/Prefab/BuffIconUI.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 17587b13f9d4467dbff77cf9762dc8fe
|
||||
timeCreated: 1756780876
|
@ -1,115 +1,36 @@
|
||||
using System;
|
||||
using Base;
|
||||
using UnityEngine;
|
||||
using Base; // 引入Base命名空间以使用BaseAnimator
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
[RequireComponent(typeof(SpriteRenderer))]
|
||||
public class SpriteAnimator : MonoBehaviour, ITick
|
||||
public class SpriteAnimator : BaseAnimator
|
||||
{
|
||||
// 公开字段(可在编辑器中设置)
|
||||
[SerializeField] private Sprite[] _sprites; // 动画精灵序列
|
||||
[SerializeField] private float _fps = 2; // 每秒帧数
|
||||
[SerializeField] private Sprite _staticSprite; // 暂停时的静态精灵
|
||||
|
||||
private SpriteRenderer _renderer; // 渲染器组件
|
||||
private bool _isPaused; // 暂停状态
|
||||
private float _frameTimer; // 帧计时器
|
||||
private int _currentFrameIndex; // 当前帧索引
|
||||
|
||||
private void Awake()
|
||||
protected override void ValidateComponent()
|
||||
{
|
||||
_renderer = GetComponent<SpriteRenderer>();
|
||||
ValidateStartFrame();
|
||||
}
|
||||
|
||||
// ITick接口实现
|
||||
public void Tick()
|
||||
{
|
||||
var deltaTime=Time.deltaTime;
|
||||
if (_isPaused)
|
||||
if (_renderer == null)
|
||||
{
|
||||
HandlePausedState();
|
||||
return;
|
||||
}
|
||||
|
||||
PlayAnimation(deltaTime);
|
||||
}
|
||||
|
||||
private void ValidateStartFrame()
|
||||
{
|
||||
// 确保有精灵时可显示有效帧
|
||||
if (_sprites != null && _sprites.Length > 0)
|
||||
{
|
||||
_currentFrameIndex = Mathf.Clamp(_currentFrameIndex, 0, _sprites.Length - 1);
|
||||
_renderer.sprite = _sprites[_currentFrameIndex];
|
||||
}
|
||||
else
|
||||
{
|
||||
_renderer.sprite = null;
|
||||
Debug.LogError("SpriteAnimator requires a SpriteRenderer component.", this);
|
||||
enabled = false; // 禁用脚本如果组件缺失
|
||||
}
|
||||
}
|
||||
|
||||
private void HandlePausedState()
|
||||
protected override void SetDisplaySprite(Sprite sprite)
|
||||
{
|
||||
// 优先使用静态精灵,否则保持当前帧
|
||||
if (_staticSprite)
|
||||
if (_renderer != null)
|
||||
{
|
||||
_renderer.sprite = _staticSprite;
|
||||
_renderer.sprite = sprite;
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayAnimation(float deltaTime)
|
||||
{
|
||||
if (_sprites == null || _sprites.Length == 0) return;
|
||||
|
||||
// 更新帧计时器
|
||||
_frameTimer += deltaTime;
|
||||
var frameDuration = 1f / _fps;
|
||||
|
||||
// 检查帧切换条件
|
||||
while (_frameTimer >= frameDuration)
|
||||
{
|
||||
_frameTimer -= frameDuration;
|
||||
NextFrame();
|
||||
}
|
||||
}
|
||||
|
||||
private void NextFrame()
|
||||
{
|
||||
// 循环播放动画
|
||||
_currentFrameIndex = (_currentFrameIndex + 1) % _sprites.Length;
|
||||
_renderer.sprite = _sprites[_currentFrameIndex];
|
||||
}
|
||||
|
||||
// 外部控制方法
|
||||
public void SetPaused(bool paused) => _isPaused = paused;
|
||||
public void SetSprites(Sprite[] newSprites)
|
||||
{
|
||||
_sprites = newSprites;
|
||||
|
||||
// 如果有新的精灵数组,则立即显示第一帧
|
||||
if (_sprites != null && _sprites.Length > 0)
|
||||
{
|
||||
_currentFrameIndex = 0; // 重置当前帧索引为第一帧
|
||||
_renderer.sprite = _sprites[_currentFrameIndex]; // 立即显示第一帧
|
||||
}
|
||||
else
|
||||
{
|
||||
_renderer.sprite = null; // 如果没有精灵,则清空渲染器
|
||||
}
|
||||
|
||||
// 重置帧计时器,以确保从头开始播放
|
||||
_frameTimer = 0f;
|
||||
}
|
||||
|
||||
public void Restore()
|
||||
{
|
||||
_currentFrameIndex = 0;
|
||||
_renderer.sprite = _sprites[_currentFrameIndex];
|
||||
}
|
||||
|
||||
public void SetFPS(float newFPS) => _fps = Mathf.Max(0.1f, newFPS);
|
||||
public void SetStaticSprite(Sprite sprite) => _staticSprite = sprite;
|
||||
// [可选] 如果SpriteAnimator需要任何Awake后特有的初始化逻辑,可以在这里重写
|
||||
// protected override void Awake()
|
||||
// {
|
||||
// base.Awake(); // 确保调用基类的Awake
|
||||
// // 子类特有的初始化
|
||||
// }
|
||||
}
|
||||
}
|
38
Client/Assets/Scripts/Prefab/UIImageAnimator.cs
Normal file
38
Client/Assets/Scripts/Prefab/UIImageAnimator.cs
Normal file
@ -0,0 +1,38 @@
|
||||
// UI/UIImageAnimator.cs
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using Base; // 引入Base命名空间以使用BaseAnimator
|
||||
|
||||
namespace UI
|
||||
{
|
||||
[RequireComponent(typeof(Image))]
|
||||
public class UIImageAnimator : BaseAnimator // 继承BaseAnimator
|
||||
{
|
||||
private Image _image; // UI Image组件
|
||||
|
||||
protected override void ValidateComponent()
|
||||
{
|
||||
_image = GetComponent<Image>();
|
||||
if (_image == null)
|
||||
{
|
||||
Debug.LogError("UIImageAnimator requires an Image component.", this);
|
||||
enabled = false; // 禁用脚本如果组件缺失
|
||||
}
|
||||
}
|
||||
|
||||
protected override void SetDisplaySprite(Sprite sprite)
|
||||
{
|
||||
if (_image)
|
||||
{
|
||||
_image.sprite = sprite;
|
||||
}
|
||||
}
|
||||
|
||||
// [可选] 如果UIImageAnimator需要任何Awake后特有的初始化逻辑,可以在这里重写
|
||||
// protected override void Awake()
|
||||
// {
|
||||
// base.Awake(); // 确保调用基类的Awake
|
||||
// // 子类特有的初始化
|
||||
// }
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Prefab/UIImageAnimator.cs.meta
Normal file
3
Client/Assets/Scripts/Prefab/UIImageAnimator.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c6f899d1c5ef450bb6f3e670fa55cffd
|
||||
timeCreated: 1756778168
|
Reference in New Issue
Block a user