第一批重构,主要是物理迁移脚本和部分命名空间的调整
- 结构上迁移文件,将脚本分成 Base/Runtime/Presentation/Procedure/Editor 五大文件夹 - 调整 DataTable 和 Definition 下脚本的命名空间 - 依据 UI-5层架构设计规范 拆分原先的 UI 到 Runtime 和 Presentation 下
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using DG.Tweening;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace CustomComponent
|
||||
{
|
||||
public class DamageTextItem : MonoBehaviour
|
||||
{
|
||||
private const float RiseDistance = 80f;
|
||||
private const float RiseDuration = 0.55f;
|
||||
private const float FadeDelay = 0.2f;
|
||||
private const float FadeDuration = 0.35f;
|
||||
private static readonly Color NormalColor = new Color(1f, 0.32f, 0.23f, 1f);
|
||||
|
||||
[SerializeField] private RectTransform _cachedTransform;
|
||||
|
||||
[SerializeField] private CanvasGroup _canvasGroup;
|
||||
|
||||
[SerializeField] private TMP_Text _text;
|
||||
|
||||
private Sequence _sequence;
|
||||
private Action<DamageTextItem> _onComplete;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (_cachedTransform == null) _cachedTransform = GetComponent<RectTransform>();
|
||||
if (_canvasGroup == null) _canvasGroup = GetComponent<CanvasGroup>();
|
||||
if (_text == null) _text = GetComponent<TMP_Text>();
|
||||
}
|
||||
|
||||
public void Show(Vector3 worldPosition, int damage, Canvas canvas, Action<DamageTextItem> onComplete)
|
||||
{
|
||||
if (canvas == null || GameEntry.Scene.MainCamera == null) return;
|
||||
|
||||
_onComplete = onComplete;
|
||||
gameObject.SetActive(true);
|
||||
KillSequence();
|
||||
|
||||
_canvasGroup.alpha = 1f;
|
||||
_cachedTransform.localScale = Vector3.one;
|
||||
_text.color = NormalColor;
|
||||
_text.text = damage.ToString();
|
||||
|
||||
Vector3 screenPos = GameEntry.Scene.MainCamera.WorldToScreenPoint(worldPosition);
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)canvas.transform, screenPos,
|
||||
canvas.worldCamera, out Vector2 localPos);
|
||||
localPos.x += UnityEngine.Random.Range(-24f, 24f);
|
||||
_cachedTransform.anchoredPosition = localPos;
|
||||
|
||||
_sequence = DOTween.Sequence();
|
||||
_sequence.Append(_cachedTransform.DOAnchorPosY(localPos.y + RiseDistance, RiseDuration)
|
||||
.SetEase(Ease.OutQuad));
|
||||
_sequence.Join(_cachedTransform.DOPunchScale(new Vector3(0.15f, 0.15f, 0f), 0.18f, 6, 0.75f));
|
||||
_sequence.Join(_canvasGroup.DOFade(0f, FadeDuration).SetDelay(FadeDelay));
|
||||
_sequence.OnComplete(() => _onComplete?.Invoke(this));
|
||||
}
|
||||
|
||||
public void ResetItem()
|
||||
{
|
||||
KillSequence();
|
||||
_onComplete = null;
|
||||
_canvasGroup.alpha = 1f;
|
||||
_text.text = string.Empty;
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
KillSequence();
|
||||
}
|
||||
|
||||
private void KillSequence()
|
||||
{
|
||||
if (_sequence == null) return;
|
||||
_sequence.Kill();
|
||||
_sequence = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user