(client)feat:实现动画组件和图片组件预制体
This commit is contained in:
144
Client/Assets/Scripts/Prefab/ImagePrefab.cs
Normal file
144
Client/Assets/Scripts/Prefab/ImagePrefab.cs
Normal file
@ -0,0 +1,144 @@
|
||||
using Base;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
[RequireComponent(typeof(SpriteRenderer))]
|
||||
public class ImagePrefab : MonoBehaviour
|
||||
{
|
||||
[Header("Display Settings")] [SerializeField]
|
||||
private Sprite _defaultSprite;
|
||||
|
||||
[SerializeField] private Color _tintColor = Color.white;
|
||||
[SerializeField, Range(0, 1)] private float _alpha = 1f;
|
||||
[SerializeField] private bool _preserveAspect = true;
|
||||
|
||||
private SpriteRenderer _renderer;
|
||||
private Vector2 _originalSize;
|
||||
private bool _isInitialized;
|
||||
|
||||
public Sprite DisplayedSprite => _renderer.sprite;
|
||||
public Color CurrentColor => _renderer.color;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
InitializeRenderer();
|
||||
}
|
||||
|
||||
private void InitializeRenderer()
|
||||
{
|
||||
if (_isInitialized) return;
|
||||
|
||||
_renderer = GetComponent<SpriteRenderer>();
|
||||
|
||||
// 初始尺寸记录
|
||||
if (_defaultSprite != null)
|
||||
{
|
||||
_originalSize = new Vector2(
|
||||
_defaultSprite.rect.width / _defaultSprite.pixelsPerUnit,
|
||||
_defaultSprite.rect.height / _defaultSprite.pixelsPerUnit
|
||||
);
|
||||
}
|
||||
|
||||
// 设置初始状态
|
||||
ApplyVisualSettings();
|
||||
_isInitialized = true;
|
||||
}
|
||||
|
||||
private void ApplyVisualSettings()
|
||||
{
|
||||
if (!_isInitialized) InitializeRenderer();
|
||||
|
||||
// 设置精灵和颜色
|
||||
_renderer.sprite = _defaultSprite;
|
||||
_renderer.color = new Color(_tintColor.r, _tintColor.g, _tintColor.b, _alpha);
|
||||
|
||||
// 保持原始纵横比
|
||||
if (_preserveAspect && _defaultSprite != null)
|
||||
{
|
||||
MaintainAspectRatio();
|
||||
}
|
||||
}
|
||||
|
||||
private void MaintainAspectRatio()
|
||||
{
|
||||
Vector2 currentSize = transform.lossyScale;
|
||||
float aspectRatio = _originalSize.x / _originalSize.y;
|
||||
|
||||
if (currentSize.x == 0 || currentSize.y == 0) return;
|
||||
|
||||
float currentAspect = currentSize.x / currentSize.y;
|
||||
|
||||
if (Mathf.Abs(currentAspect - aspectRatio) > 0.01f)
|
||||
{
|
||||
Vector2 newScale = currentSize;
|
||||
if (currentAspect > aspectRatio)
|
||||
{
|
||||
newScale.x = currentSize.y * aspectRatio;
|
||||
}
|
||||
else
|
||||
{
|
||||
newScale.y = currentSize.x / aspectRatio;
|
||||
}
|
||||
|
||||
transform.localScale = newScale;
|
||||
}
|
||||
}
|
||||
|
||||
// 公共控制方法
|
||||
public void SetSprite(Sprite newSprite)
|
||||
{
|
||||
_defaultSprite = newSprite;
|
||||
if (_defaultSprite != null)
|
||||
{
|
||||
_originalSize = new Vector2(
|
||||
_defaultSprite.rect.width / _defaultSprite.pixelsPerUnit,
|
||||
_defaultSprite.rect.height / _defaultSprite.pixelsPerUnit
|
||||
);
|
||||
}
|
||||
|
||||
ApplyVisualSettings();
|
||||
}
|
||||
|
||||
public void SetColor(Color newColor)
|
||||
{
|
||||
_tintColor = newColor;
|
||||
ApplyVisualSettings();
|
||||
}
|
||||
|
||||
public void SetAlpha(float newAlpha)
|
||||
{
|
||||
_alpha = Mathf.Clamp01(newAlpha);
|
||||
ApplyVisualSettings();
|
||||
}
|
||||
|
||||
public void ToggleVisibility(bool isVisible)
|
||||
{
|
||||
if (!_isInitialized) InitializeRenderer();
|
||||
_renderer.enabled = isVisible;
|
||||
}
|
||||
|
||||
public void SetSortingLayer(string layerName, int order)
|
||||
{
|
||||
if (!_isInitialized) InitializeRenderer();
|
||||
_renderer.sortingLayerName = layerName;
|
||||
_renderer.sortingOrder = order;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private void OnValidate()
|
||||
{
|
||||
// 编辑器模式下实时预览变化
|
||||
if (UnityEditor.EditorApplication.isPlaying)
|
||||
{
|
||||
ApplyVisualSettings();
|
||||
}
|
||||
else if (GetComponent<SpriteRenderer>() != null)
|
||||
{
|
||||
GetComponent<SpriteRenderer>().sprite = _defaultSprite;
|
||||
GetComponent<SpriteRenderer>().color = new Color(_tintColor.r, _tintColor.g, _tintColor.b, _alpha);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Prefab/ImagePrefab.cs.meta
Normal file
3
Client/Assets/Scripts/Prefab/ImagePrefab.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0c360a9f1baf4eaf9afa85617c395b0d
|
||||
timeCreated: 1753331527
|
90
Client/Assets/Scripts/Prefab/SpriteAnimator.cs
Normal file
90
Client/Assets/Scripts/Prefab/SpriteAnimator.cs
Normal file
@ -0,0 +1,90 @@
|
||||
using Base;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
[RequireComponent(typeof(SpriteRenderer))]
|
||||
public class SpriteAnimator : MonoBehaviour, ITick
|
||||
{
|
||||
// 公开字段(可在编辑器中设置)
|
||||
[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()
|
||||
{
|
||||
_renderer = GetComponent<SpriteRenderer>();
|
||||
ValidateStartFrame();
|
||||
}
|
||||
|
||||
// ITick接口实现
|
||||
public void Tick()
|
||||
{
|
||||
var deltaTime=Time.deltaTime;
|
||||
if (_isPaused)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandlePausedState()
|
||||
{
|
||||
// 优先使用静态精灵,否则保持当前帧
|
||||
if (_staticSprite)
|
||||
{
|
||||
_renderer.sprite = _staticSprite;
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayAnimation(float deltaTime)
|
||||
{
|
||||
if (_sprites == null || _sprites.Length == 0) return;
|
||||
|
||||
// 更新帧计时器
|
||||
_frameTimer += deltaTime;
|
||||
float 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;
|
||||
public void SetFPS(float newFPS) => _fps = Mathf.Max(0.1f, newFPS);
|
||||
public void SetStaticSprite(Sprite sprite) => _staticSprite = sprite;
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Prefab/SpriteAnimator.cs.meta
Normal file
3
Client/Assets/Scripts/Prefab/SpriteAnimator.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 713a1742950a45d8b4be258a82321e45
|
||||
timeCreated: 1753282330
|
Reference in New Issue
Block a user