144 lines
4.2 KiB
C#
144 lines
4.2 KiB
C#
![]() |
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
|
||
|
}
|
||
|
}
|