(client) feat:健康给予,路径优化,结算界面,商店界面 (#60)

Co-authored-by: m0_75251201 <m0_75251201@noreply.gitcode.com>
Reviewed-on: http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite/pulls/60
This commit is contained in:
2025-10-10 14:08:23 +08:00
parent 9a797479ff
commit 16b49f3d3a
1900 changed files with 114053 additions and 34157 deletions

View File

@@ -1,98 +1,77 @@
using System;
using UnityEngine;
namespace Base
{
/// <summary>
/// 抽象基类,用于封装通用动画逻辑。
/// 所有的动画播放器都应继承自该类,并实现其抽象方法来控制具体的显示组件。
/// 抽象基类,用于封装通用动画逻辑。
/// 所有的动画播放器都应继承自该类,并实现其抽象方法来控制具体的显示组件。
/// </summary>
public abstract class BaseAnimator : MonoBehaviour, ITick
{
// 通用公开字段(可在编辑器中设置)
/// <summary>
/// 动画播放的精灵序列。
/// 动画播放的精灵序列。
/// </summary>
[SerializeField] protected Sprite[] _sprites;
[SerializeField] protected Sprite[] _sprites;
/// <summary>
/// 每秒帧数,控制动画播放速度。
/// 每秒帧数,控制动画播放速度。
/// </summary>
[SerializeField] protected float _fps = 2;
[SerializeField] protected float _fps = 2;
/// <summary>
/// 动画暂停时显示的静态精灵。
/// 如果未设置,则默认保持当前帧。
/// 动画暂停时显示的静态精灵。
/// 如果未设置,则默认保持当前帧。
/// </summary>
[SerializeField] protected Sprite _staticSprite;
[SerializeField] protected Sprite _staticSprite;
/// <summary>
/// 当前正在显示的精灵帧索引。
/// </summary>
protected int _currentFrameIndex;
/// <summary>
/// 用于跟踪当前帧播放时间的计时器。
/// </summary>
protected float _frameTimer;
/// <summary>
/// 指示动画是否为一次性播放模式。
/// </summary>
protected bool _isOneShot;
// 通用内部状态
/// <summary>
/// 指示动画是否处于暂停状态。
/// 指示动画是否处于暂停状态。
/// </summary>
protected bool _isPaused;
/// <summary>
/// 用于跟踪当前帧播放时间的计时器。
/// </summary>
protected float _frameTimer;
/// <summary>
/// 当前正在显示的精灵帧索引。
/// </summary>
protected int _currentFrameIndex;
/// <summary>
/// 指示动画是否为一次性播放模式。
/// </summary>
protected bool _isOneShot;
protected bool _isPaused;
/// <summary>
/// 抽象方法:子类必须实现此方法以获取并验证其特有的显示组件
/// 例如SpriteRenderer、Image等
/// </summary>
protected abstract void ValidateComponent();
/// <summary>
/// 抽象方法:子类必须实现此方法以将给定的精灵设置到实际显示组件上。
/// </summary>
/// <param name="sprite">要显示的精灵。</param>
protected abstract void SetDisplaySprite(Sprite sprite);
/// <summary>
/// Unity生命周期方法在脚本实例被加载时调用。
/// 主要用于初始化组件和动画的第一帧。
/// Unity生命周期方法在脚本实例被加载时调用
/// 主要用于初始化组件和动画的第一帧
/// </summary>
protected virtual void Awake()
{
// 子类获取并验证其显示组件
ValidateComponent();
ValidateComponent();
// 初始化动画的第一帧
ValidateStartFrame();
ValidateStartFrame();
}
/// <summary>
/// Unity生命周期方法在第一次帧更新之前调用。
/// 用于将当前动画器添加到全局计时器。
/// </summary>
private void Start()
private void OnEnable()
{
Clock.AddTick(this);
}
/// <summary>
/// Unity生命周期方法在MonoBehaviour被销毁时调用。
/// 用于将当前动画器从全局计时器中移除。
/// </summary>
private void OnDestroy()
private void OnDisable()
{
Clock.RemoveTick(this);
}
/// <summary>
/// ITick接口实现每帧更新动画状态。
/// ITick接口实现每帧更新动画状态。
/// </summary>
public void Tick()
{
@@ -113,7 +92,19 @@ namespace Base
}
/// <summary>
/// 验证并设置动画的初始帧
/// 抽象方法:子类必须实现此方法以获取并验证其特有的显示组件
/// 例如SpriteRenderer、Image等。
/// </summary>
protected abstract void ValidateComponent();
/// <summary>
/// 抽象方法:子类必须实现此方法以将给定的精灵设置到实际显示组件上。
/// </summary>
/// <param name="sprite">要显示的精灵。</param>
protected abstract void SetDisplaySprite(Sprite sprite);
/// <summary>
/// 验证并设置动画的初始帧。
/// </summary>
protected void ValidateStartFrame()
{
@@ -123,33 +114,31 @@ namespace Base
// 确保当前帧索引在有效范围内
_currentFrameIndex = Mathf.Clamp(_currentFrameIndex, 0, _sprites.Length - 1);
// 调用抽象方法设置当前帧的精灵
SetDisplaySprite(_sprites[_currentFrameIndex]);
SetDisplaySprite(_sprites[_currentFrameIndex]);
}
else
{
// 如果没有精灵,则调用抽象方法清空显示组件的精灵
SetDisplaySprite(null);
SetDisplaySprite(null);
// 如果没有精灵可显示,默认隐藏游戏对象
gameObject.SetActive(false);
gameObject.SetActive(false);
}
}
/// <summary>
/// 处理动画处于暂停状态时的显示逻辑。
/// 处理动画处于暂停状态时的显示逻辑。
/// </summary>
protected void HandlePausedState()
{
// 如果设置了静态精灵,则优先显示静态精灵
if (_staticSprite)
{
// 调用抽象方法显示静态精灵
SetDisplaySprite(_staticSprite);
}
SetDisplaySprite(_staticSprite);
// 否则保持当前显示的精灵SetDisplaySprite已在NextFrame或ValidateStartFrame中设置无需额外操作。
}
/// <summary>
/// 根据时间增量播放动画。
/// 根据时间增量播放动画。
/// </summary>
/// <param name="deltaTime">自上一帧以来的时间增量。</param>
protected void PlayAnimation(float deltaTime)
@@ -160,7 +149,7 @@ namespace Base
// 确保显示组件的精灵被清除
SetDisplaySprite(null);
// 没有精灵可显示,隐藏游戏对象
gameObject.SetActive(false);
gameObject.SetActive(false);
return;
}
@@ -175,15 +164,12 @@ namespace Base
_frameTimer -= frameDuration;
NextFrame();
// 如果是一次性播放模式,并且动画已播放到最后一帧并暂停,则跳出循环,防止在同一帧内进行二次处理
if (_isOneShot && _currentFrameIndex == _sprites.Length - 1 && _isPaused)
{
return;
}
if (_isOneShot && _currentFrameIndex == _sprites.Length - 1 && _isPaused) return;
}
}
/// <summary>
/// 切换到动画的下一帧。
/// 切换到动画的下一帧。
/// </summary>
protected void NextFrame()
{
@@ -200,45 +186,46 @@ namespace Base
if (_isOneShot)
{
// 播放到最后一帧后停止,并保持在最后一帧
_currentFrameIndex = _sprites.Length - 1;
_currentFrameIndex = _sprites.Length - 1;
// 确保显示最后一帧
SetDisplaySprite(_sprites[_currentFrameIndex]);
SetDisplaySprite(_sprites[_currentFrameIndex]);
// 调用一次性动画结束处理
FinishOneShotAnimation();
FinishOneShotAnimation();
return; // 停止进一步处理
}
else
{
// 循环播放模式下,重置到第一帧
_currentFrameIndex = 0;
}
// 循环播放模式下,重置到第一帧
_currentFrameIndex = 0;
}
// 调用抽象方法更新显示组件的精灵
SetDisplaySprite(_sprites[_currentFrameIndex]);
SetDisplaySprite(_sprites[_currentFrameIndex]);
}
/// <summary>
/// 处理一次性动画播放结束时的逻辑。
/// 处理一次性动画播放结束时的逻辑。
/// </summary>
protected void FinishOneShotAnimation()
{
_isOneShot = false; // 结束一次性播放模式
_isPaused = true; // 动画暂停
_isPaused = true; // 动画暂停
gameObject.SetActive(false); // 隐藏当前游戏对象
// TODO: 如果有动画结束回调,可以在这里触发
// _onOneShotFinished?.Invoke();
}
/// <summary>
/// 设置动画的暂停状态。
/// 设置动画的暂停状态。
/// </summary>
/// <param name="paused">如果为true动画将暂停如果为false动画将继续播放。</param>
public void SetPaused(bool paused) => _isPaused = paused;
public void SetPaused(bool paused)
{
_isPaused = paused;
}
/// <summary>
/// 设置新的精灵序列用于动画播放。
/// 设置后动画将从第一帧开始播放,并重置暂停和一次性播放模式。
/// 设置新的精灵序列用于动画播放。
/// 设置后动画将从第一帧开始播放,并重置暂停和一次性播放模式。
/// </summary>
/// <param name="newSprites">新的精灵数组。</param>
public void SetSprites(Sprite[] newSprites)
@@ -265,14 +252,14 @@ namespace Base
}
/// <summary>
/// 将动画状态恢复到初始设置。
/// 通常恢复到第一帧并暂停。
/// 将动画状态恢复到初始设置。
/// 通常恢复到第一帧并暂停。
/// </summary>
public void Restore()
{
_currentFrameIndex = 0;
_isOneShot = false; // 重置时取消一次性播放模式
_isPaused = true; // 默认恢复后暂停
_isPaused = true; // 默认恢复后暂停
_frameTimer = 0f;
// 如果有精灵序列且不为空,则恢复到第一帧并尝试显示
@@ -280,10 +267,7 @@ namespace Base
{
SetDisplaySprite(_sprites[_currentFrameIndex]); // 恢复到第一帧
// 如果游戏对象当前不活跃,则激活它使其可见
if (!gameObject.activeSelf)
{
gameObject.SetActive(true);
}
if (!gameObject.activeSelf) gameObject.SetActive(true);
}
else
{
@@ -293,8 +277,8 @@ namespace Base
}
/// <summary>
/// 以一次性播放模式播放动画。
/// 动画将从第一帧播放到最后一帧,然后停止并隐藏游戏对象。
/// 以一次性播放模式播放动画。
/// 动画将从第一帧播放到最后一帧,然后停止并隐藏游戏对象。
/// </summary>
public void PlayOneShot()
{
@@ -313,27 +297,29 @@ namespace Base
_frameTimer = 0f;
// 确保游戏对象处于激活状态才能播放
if (!gameObject.activeSelf)
{
gameObject.SetActive(true);
}
if (!gameObject.activeSelf) gameObject.SetActive(true);
// 立即显示第一帧
SetDisplaySprite(_sprites[_currentFrameIndex]);
}
/// <summary>
/// 设置动画的每秒帧数FPS
/// FPS值将被限制在最小0.1,以防止除零或过小的帧率。
/// 设置动画的每秒帧数FPS
/// FPS值将被限制在最小0.1,以防止除零或过小的帧率。
/// </summary>
/// <param name="newFPS">新的每秒帧数。</param>
public void SetFPS(float newFPS) => _fps = Mathf.Max(0.1f, newFPS);
public void SetFPS(float newFPS)
{
_fps = Mathf.Max(0.1f, newFPS);
}
/// <summary>
/// 设置动画暂停时显示的静态精灵。
/// 设置动画暂停时显示的静态精灵。
/// </summary>
/// <param name="sprite">新的静态精灵。</param>
public void SetStaticSprite(Sprite sprite) => _staticSprite = sprite;
public void SetStaticSprite(Sprite sprite)
{
_staticSprite = sprite;
}
}
}
}

View File

@@ -1,10 +1,28 @@
using UI;
using Entity;
using Managers;
using UnityEngine;
namespace Prefab
{
public class BuffIconUI: MonoBehaviour
public class BuffIconUI : MonoBehaviour
{
public UIImageAnimator icon;
public void Init(Hediff hediff)
{
var stage = hediff.CurrentStage;
if (stage is { textures: { Length: > 0 } })
{
if (stage.textures == null) return;
var sprites = PackagesImageManager.Instance.GetSprites(stage.textures);
icon.SetSprites(sprites);
}
else
{
if (hediff.def.textures == null) return;
var sprites = PackagesImageManager.Instance.GetSprites(hediff.def.textures);
icon.SetSprites(sprites);
}
}
}
}

View File

@@ -12,8 +12,8 @@ namespace Prefab
public string Label
{
get { return text.text; }
set { text.text = value; }
get => text.text;
set => text.text = value;
}
public void AddListener(UnityAction callback)
@@ -21,6 +21,4 @@ namespace Prefab
button.onClick.AddListener(callback);
}
}
}

View File

@@ -1,9 +1,9 @@
using Base;
using Data;
using Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using Base;
using Data;
using Entity;
using UnityEngine;
namespace Prefab
@@ -15,22 +15,15 @@ namespace Prefab
public Vector3 Position
{
get
{
return transform.position;
}
set
{
transform.position = value;
}
get => transform.position;
set => transform.position = value;
}
public void Init(Data.EntityDef entityDef)
public void Init(EntityDef entityDef)
{
entity.Init(entityDef);
outline.Init();
outline.Hide();
}
public void DefaultInit()
@@ -41,16 +34,17 @@ namespace Prefab
{
var orientationDict = new Dictionary<Orientation, ITick[]>();
foreach (Orientation orientation in Enum.GetValues(typeof(Orientation)))
{
orientationDict[orientation] = inf; // 所有值都指向同一个列表
}
}
outline.Init();
outline.Hide();
}
public override string ToString()
{
return $"实体定义: {entity.entityDef.defName}" +
$"实体位置: {entity.Position.x},{entity.Position.y},{entity.Position.z}";
}
}
}

View File

@@ -8,6 +8,7 @@ namespace Prefab
public class HoverButtonPrefab : MonoBehaviour
{
public TMP_Text text;
public Button button;
// public CircleCollider2D circleCollider;
@@ -20,15 +21,6 @@ namespace Prefab
// circleCollider.radius=startRadius;
}
public void OnMouseOver()
{
var dir = (Input.mousePosition - transform.position).magnitude;
var color = button.image.color;
color.a = Mathf.Min((startRadius - dir) / (startRadius - endRadius), 1);
button.image.color = color;
}
public void OnMouseEnter()
{
button.gameObject.SetActive(true);
@@ -38,5 +30,14 @@ namespace Prefab
{
button.gameObject.SetActive(false);
}
public void OnMouseOver()
{
var dir = (Input.mousePosition - transform.position).magnitude;
var color = button.image.color;
color.a = Mathf.Min((startRadius - dir) / (startRadius - endRadius), 1);
button.image.color = color;
}
}
}

View File

@@ -2,7 +2,6 @@ using UnityEngine;
namespace Prefab
{
[RequireComponent(typeof(SpriteRenderer))]
public class ImagePrefab : MonoBehaviour
{
@@ -18,26 +17,18 @@ namespace Prefab
Debug.LogError("SpriteRenderer组件未找到请确保预制体包含该组件");
return;
}
if (defaultSprite != null)
{
spriteRenderer.sprite = defaultSprite;
}
if (defaultSprite != null) spriteRenderer.sprite = defaultSprite;
}
public void SetSprite(Sprite newSprite)
{
if (spriteRenderer != null && newSprite != null)
{
spriteRenderer.sprite = newSprite;
}
if (spriteRenderer && newSprite) spriteRenderer.sprite = newSprite;
}
public void SetColor(Color newColor)
{
if (spriteRenderer != null)
{
spriteRenderer.color = newColor;
}
if (spriteRenderer != null) spriteRenderer.color = newColor;
}
}
}

View File

@@ -0,0 +1,17 @@
using UnityEngine;
using UnityEngine.UI;
namespace Prefab
{
[RequireComponent(typeof(Image))]
public class ImagePrefabUI:MonoBehaviour
{
public Image image;
public Sprite Icon
{
get => image.sprite;
set => image.sprite = value;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6e92cf4311a7407e82a00d366737978a
timeCreated: 1759924504

View File

@@ -5,13 +5,15 @@ namespace Prefab
public class ProgressBarPrefab : MonoBehaviour
{
public GameObject _progress;
public float Progress
{
get => _progress.transform.localScale.x;
set
{
var x = Mathf.Clamp01(value);
_progress.transform.localScale = new Vector3(x, _progress.transform.localScale.y, _progress.transform.localScale.z);
_progress.transform.localScale =
new Vector3(x, _progress.transform.localScale.y, _progress.transform.localScale.z);
}
}
}

View File

@@ -10,6 +10,11 @@ namespace Prefab
public GameObject menu;
public ButtonPrefab buttonPrefab;
public void OnPointerExit(PointerEventData eventData)
{
Hide();
}
public void Show()
{
gameObject.SetActive(true);
@@ -17,7 +22,7 @@ namespace Prefab
public void Hide()
{
Destroy(this.gameObject);
Destroy(gameObject);
}
public void Init(List<(string name, UnityAction callback)> buttons)
@@ -27,6 +32,7 @@ namespace Prefab
Debug.LogError("Menu or ButtonPrefab is not assigned!");
return;
}
foreach (var (label, callback) in buttons)
{
// 实例化按钮预制体
@@ -56,9 +62,5 @@ namespace Prefab
}
}
}
public void OnPointerExit(PointerEventData eventData)
{
Hide();
}
}
}

View File

@@ -2,6 +2,5 @@ namespace Prefab
{
public class SkillTreeNodeProfab
{
}
}

View File

@@ -1,17 +1,19 @@
using Base;
using UnityEngine;
using Base; // 引入Base命名空间以使用BaseAnimator
// 引入Base命名空间以使用BaseAnimator
namespace Prefab
{
[RequireComponent(typeof(SpriteRenderer))]
public class SpriteAnimator : BaseAnimator
public class SpriteAnimator : BaseAnimator
{
private SpriteRenderer _renderer; // 渲染器组件
protected override void ValidateComponent()
{
_renderer = GetComponent<SpriteRenderer>();
if (_renderer != null) return;
if (_renderer) return;
Debug.LogError("SpriteAnimator requires a SpriteRenderer component.", this);
enabled = false; // 禁用脚本如果组件缺失
}

View File

@@ -9,17 +9,15 @@ namespace Prefab
[Tooltip("动画持续时间。当小于或等于0时动画将无限持续物体不会自动销毁。")]
public float lifeTime = 3;
private float _timer; // 每次启用时会重置为0
// 使用属性限制外部只能获取和一次性设置动画函数
[Tooltip("定义X轴偏移的函数 (时间 -> X偏移量)")]
public Func<float, float> GetXPosition { get; private set; }
[Tooltip("定义Y轴偏移的函数 (时间 -> Y偏移量)")]
public Func<float, float> GetYPosition { get; private set; }
private Vector3 _originalPosition;
private float _timer; // 每次启用时会重置为0
// 使用属性限制外部只能获取和一次性设置动画函数
[Tooltip("定义X轴偏移的函数 (时间 -> X偏移量)")] public Func<float, float> GetXPosition { get; private set; }
[Tooltip("定义Y轴偏移的函数 (时间 -> Y偏移量)")] public Func<float, float> GetYPosition { get; private set; }
private void Start()
{
_originalPosition = transform.localPosition; // 初始位置只需在组件生命周期中获取一次
@@ -57,15 +55,12 @@ namespace Prefab
transform.localPosition = new Vector3(xPos, yPos, _originalPosition.z);
// 只有当lifeTime > 0时才判断是否需要销毁
if (lifeTime > 0 && _timer >= lifeTime)
{
Destroy(this.gameObject);
}
if (lifeTime > 0 && _timer >= lifeTime) Destroy(gameObject);
}
/// <summary>
/// 设置动画的X和Y轴位移函数。
/// 此方法通常在实例化TemporaryAnimator后立即调用以配置其动画行为。
/// 设置动画的X和Y轴位移函数。
/// 此方法通常在实例化TemporaryAnimator后立即调用以配置其动画行为。
/// </summary>
/// <param name="getXPosFunc">时间到X轴位移的函数。</param>
/// <param name="getYPosFunc">时间到Y轴位移的函数。</param>
@@ -75,4 +70,4 @@ namespace Prefab
GetYPosition = getYPosFunc;
}
}
}
}

View File

@@ -1,14 +1,13 @@
using UnityEngine;
using UnityEngine.UI;
namespace Prefab
{
public class TemporaryAnimatorImageUI:TemporaryAnimator
public class TemporaryAnimatorImageUI : TemporaryAnimator
{
public UIImageAnimator imageAnimator;
public RectTransform rectTransform;
public void Init(Sprite[] sprite,float fps=3)
public void Init(Sprite[] sprite, float fps = 3)
{
if (imageAnimator == null)
imageAnimator = GetComponentInChildren<UIImageAnimator>();

View File

@@ -2,15 +2,13 @@ using UnityEngine;
namespace Prefab
{
public class TemporaryAnimatorSprite:TemporaryAnimator
public class TemporaryAnimatorSprite : TemporaryAnimator
{
[SerializeField]
private SpriteAnimator spriteAnimator;
[SerializeField] private SpriteAnimator spriteAnimator;
public void Init(Sprite[] textures,float fps=3)
public void Init(Sprite[] textures, float fps = 3)
{
if(!spriteAnimator)
if (!spriteAnimator)
spriteAnimator = GetComponentInChildren<SpriteAnimator>();
if (!spriteAnimator) return;
spriteAnimator.SetSprites(textures);

View File

@@ -1,50 +1,48 @@
using Base;
using TMPro;
using UnityEngine;
namespace Prefab
{
/// <summary>
/// 临时动画文本组件,用于在指定时间内逐帧显示一系列文本动画。
/// 该组件假设 TemporaryAnimator 实现了 ITickable 接口,因此可以在游戏循环中接收更新。
/// 临时动画文本组件,用于在指定时间内逐帧显示一系列文本动画。
/// 该组件假设 TemporaryAnimator 实现了 ITickable 接口,因此可以在游戏循环中接收更新。
/// </summary>
public class TemporaryAnimatorText : TemporaryAnimator
{
/// <summary>
/// 用于显示动画文本的 TMP_Text 组件。
/// 允许在编辑器中赋值,以增加灵活性和鲁棒性。
/// 用于显示动画文本的 TMP_Text 组件。
/// 允许在编辑器中赋值,以增加灵活性和鲁棒性。
/// </summary>
[SerializeField] public TMP_Text text;
public RectTransform rectTransform;
/// <summary>
/// 当前播放的动画帧索引
/// </summary>
private int currentFrameIndex = 0;
/// <summary>
/// 存储所有动画帧文本的字符串数组。每个元素代表一帧动画内容。
/// 存储所有动画帧文本的字符串数组。每个元素代表一帧动画内容
/// </summary>
private string[] animationsKey;
/// <summary>
/// 每帧动画的持续时间。根据帧率FPS计算得出
/// 当前播放的动画帧索引
/// </summary>
private int currentFrameIndex;
/// <summary>
/// 每帧动画的持续时间。根据帧率FPS计算得出。
/// </summary>
private float frameDuration;
/// <summary>
/// 用于跟踪当前帧已持续时间的计时器。
/// 用于跟踪当前帧已持续时间的计时器。
/// </summary>
private float timer;
/// <summary>
/// 初始化动画文本组件。
/// 初始化动画文本组件。
/// </summary>
/// <param name="s">包含所有动画帧文本的字符串,多个帧之间用逗号分隔。</param>
/// <param name="fps">动画播放的帧率Frames Per Second。</param>
public void Init(string s, float fps=3)
public void Init(string s, float fps = 3)
{
// 如果文本组件未赋值,尝试在子对象中查找。
// 如果仍未找到,则记录错误并提前退出。
@@ -54,7 +52,7 @@ namespace Prefab
if (!text)
{
Debug.LogError("TemporaryAnimatorText: 未在子对象中找到或未指定 TMP_Text 组件。动画无法进行。", this);
return; // 提前退出 Init 方法,避免后续操作使用未初始化的 text
}
}
@@ -64,7 +62,7 @@ namespace Prefab
if (string.IsNullOrEmpty(s))
{
Debug.LogWarning("TemporaryAnimatorText: 输入的动画字符串为空或为 null。动画将显示空字符串或停止。", this);
animationsKey = new string[] { "" }; // 确保 animationsKey 不为 null 且至少包含一个空字符串元素
animationsKey = new[] { "" }; // 确保 animationsKey 不为 null 且至少包含一个空字符串元素
}
else
{
@@ -73,7 +71,7 @@ namespace Prefab
if (animationsKey.Length == 0)
{
Debug.LogWarning("TemporaryAnimatorText: 输入的动画字符串导致动画帧数组为空。动画将显示空字符串或停止。", this);
animationsKey = new string[] { "" };
animationsKey = new[] { "" };
}
}
@@ -82,11 +80,11 @@ namespace Prefab
if (fps <= 0)
{
Debug.LogError("TemporaryAnimatorText: FPS 必须是大于 0 的正值。将动画帧持续时间设置为 1 秒。", this);
this.frameDuration = 1f; // 默认每帧1秒实际效果是动画暂停或非常慢
frameDuration = 1f; // 默认每帧1秒实际效果是动画暂停或非常慢
}
else
{
this.frameDuration = 1f / fps;
frameDuration = 1f / fps;
}
// 重新初始化动画时,将当前帧索引重置为第一帧。
@@ -94,21 +92,16 @@ namespace Prefab
// 在 animationsKey 准备好后,设置初始文本为第一帧的内容。
if (text && animationsKey.Length > 0)
{
text.text = animationsKey[currentFrameIndex];
}
// 如果动画帧数组为空,但文本组件存在,则清空文本显示。
else if (text)
{
text.text = "";
}
else if (text) text.text = "";
// 重置计时器,从零开始计算新帧的持续时间。
timer = 0f;
}
/// <summary>
/// 每帧更新动画显示。该方法将在游戏循环中被 Clock 调用。
/// 每帧更新动画显示。该方法将在游戏循环中被 Clock 调用。
/// </summary>
public override void Tick()
{
@@ -122,10 +115,8 @@ namespace Prefab
// 检查动画帧数组是否已初始化或为空。
// 如果没有动画帧,则无需更新,直接返回。
if (animationsKey == null || animationsKey.Length == 0)
{
// Debug.LogWarning("TemporaryAnimatorText: 动画键未初始化或为空。跳过 Tick 更新。", this); // 这条日志已被注释掉,避免频繁输出不必要的警告
return; // 没有动画帧,直接返回
}
// 增加计时器。
timer += Time.deltaTime;
@@ -140,14 +131,10 @@ namespace Prefab
currentFrameIndex += 1;
// 如果当前帧索引超出动画帧数组的范围,则循环回到第一帧。
if (currentFrameIndex >= animationsKey.Length)
{
currentFrameIndex = 0;
}
if (currentFrameIndex >= animationsKey.Length) currentFrameIndex = 0;
// 更新文本组件显示为当前帧的文本内容。
text.text = animationsKey[currentFrameIndex];
}
}
}

View File

@@ -9,9 +9,8 @@ namespace Prefab
public string Label
{
get { return text.text; }
set { text.text = value; }
get => text.text;
set => text.text = value;
}
}
}

View File

@@ -16,7 +16,7 @@ namespace Prefab
protected override void ValidateComponent()
{
_image = GetComponent<Image>();
if (_image == null)
if (!_image)
{
Debug.LogError("UIImageAnimator requires an Image component.", this);
enabled = false; // 禁用脚本如果组件缺失
@@ -27,6 +27,5 @@ namespace Prefab
{
_image.sprite = sprite;
}
}
}