清理各个 Component 里的判断以及调用规范

This commit is contained in:
2026-09-14 18:35:02 +08:00
parent 5fbeec2476
commit 17ee268416
30 changed files with 769 additions and 1326 deletions
@@ -20,22 +20,12 @@ namespace SepCore.HPBar
private void Start()
{
if (_hpBarInstanceRoot == null)
{
Log.Error("You must set HP bar instance root first.");
return;
}
_cachedCanvas = _hpBarInstanceRoot.GetComponent<Canvas>();
_hpBarItemObjectPool =
GameEntry.ObjectPool.CreateSingleSpawnObjectPool<HPBarItemObject>("HPBarItem", _instancePoolCapacity);
_activeHPBarItems = new List<HPBarItem>();
}
private void OnDestroy()
{
}
private void Update()
{
for (int i = _activeHPBarItems.Count - 1; i >= 0; i--)
@@ -54,14 +44,14 @@ namespace SepCore.HPBar
{
if (entity == null)
{
Log.Warning("Entity is invalid.");
Log.Error("Entity is invalid.");
return;
}
HPBarItem hpBarItem = GetActiveHPBarItem(entity);
if (hpBarItem == null)
{
hpBarItem = CreateHPBarItem(entity);
hpBarItem = CreateHPBarItem();
_activeHPBarItems.Add(hpBarItem);
}
@@ -77,11 +67,6 @@ namespace SepCore.HPBar
private HPBarItem GetActiveHPBarItem(EntityBase entity)
{
if (entity == null)
{
return null;
}
for (int i = 0; i < _activeHPBarItems.Count; i++)
{
if (_activeHPBarItems[i].Owner == entity)
@@ -93,7 +78,7 @@ namespace SepCore.HPBar
return null;
}
private HPBarItem CreateHPBarItem(EntityBase entity)
private HPBarItem CreateHPBarItem()
{
HPBarItem hpBarItem = null;
HPBarItemObject hpBarItemObject = _hpBarItemObjectPool.Spawn();
@@ -104,7 +89,7 @@ namespace SepCore.HPBar
else
{
hpBarItem = Instantiate(_hpBarItemTemplate);
Transform transform = hpBarItem.GetComponent<Transform>();
Transform transform = hpBarItem.transform;
transform.SetParent(_hpBarInstanceRoot);
transform.localScale = Vector3.one;
_hpBarItemObjectPool.Register(HPBarItemObject.Create(hpBarItem), true);
@@ -14,13 +14,13 @@ namespace SepCore.HPBar
private const float KeepSeconds = 0.4f;
private const float FadeOutSeconds = 0.3f;
[FormerlySerializedAs("m_HPBar")] [SerializeField] private Slider _hPBar = null;
[FormerlySerializedAs("m_HPBar")] [SerializeField] private Slider _hpBar = null;
private Canvas _parentCanvas = null;
private RectTransform _cachedTransform = null;
private CanvasGroup _cachedCanvasGroup = null;
private EntityBase _owner = null;
private int m_OwnerId = 0;
private int _ownerId = 0;
public EntityBase Owner => _owner;
@@ -38,11 +38,11 @@ namespace SepCore.HPBar
StopAllCoroutines();
_cachedCanvasGroup.alpha = 1f;
if (_owner != owner || m_OwnerId != owner.Id)
if (_owner != owner || _ownerId != owner.Id)
{
_hPBar.value = fromHPRatio;
_hpBar.value = fromHPRatio;
_owner = owner;
m_OwnerId = owner.Id;
_ownerId = owner.Id;
}
Refresh();
@@ -57,7 +57,7 @@ namespace SepCore.HPBar
return false;
}
if (_owner != null && Owner.Available && Owner.Id == m_OwnerId)
if (_owner != null && Owner.Available && Owner.Id == _ownerId)
{
Vector3 worldPosition = _owner.CachedTransform.position + Vector3.forward;
Vector3 screenPosition = GameEntry.Scene.MainCamera.WorldToScreenPoint(worldPosition);
@@ -78,7 +78,7 @@ namespace SepCore.HPBar
{
StopAllCoroutines();
_cachedCanvasGroup.alpha = 1f;
_hPBar.value = 1f;
_hpBar.value = 1f;
_owner = null;
gameObject.SetActive(false);
}
@@ -86,23 +86,12 @@ namespace SepCore.HPBar
private void Awake()
{
_cachedTransform = GetComponent<RectTransform>();
if (_cachedTransform == null)
{
Log.Error("RectTransform is invalid.");
return;
}
_cachedCanvasGroup = GetComponent<CanvasGroup>();
if (_cachedCanvasGroup == null)
{
Log.Error("CanvasGroup is invalid.");
return;
}
}
private IEnumerator HPBarCo(float value, float animationDuration, float keepDuration, float fadeOutDuration)
{
yield return _hPBar.SmoothValue(value, animationDuration);
yield return _hpBar.SmoothValue(value, animationDuration);
yield return new WaitForSeconds(keepDuration);
yield return _cachedCanvasGroup.FadeToAlpha(0f, fadeOutDuration);
}