- 规范 UI 设计成为:

- UIForm 负责展示
    - Controller 负责控制
    - UseCase 负责业务逻辑
    - RawData 负责提供原始数据
    - Context 负责为 UIForm 提供数据
- 添加和补充一系列 UI 功能
This commit is contained in:
SepComet
2026-02-16 19:03:09 +08:00
parent d650dd63e7
commit 85b0205c73
251 changed files with 12705 additions and 5220 deletions
@@ -0,0 +1,127 @@
//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------
using GameFramework.ObjectPool;
using System.Collections.Generic;
using Entity;
using StarForce;
using UnityEngine;
using UnityEngine.Serialization;
using UnityGameFramework.Runtime;
namespace CustomComponent
{
public class HPBarComponent : GameFrameworkComponent
{
[FormerlySerializedAs("m_HPBarItemTemplate")] [SerializeField]
private HPBarItem _hpBarItemTemplate = null;
[FormerlySerializedAs("m_HPBarInstanceRoot")] [SerializeField]
private Transform _hpBarInstanceRoot = null;
[FormerlySerializedAs("m_InstancePoolCapacity")] [SerializeField]
private int _instancePoolCapacity = 16;
private IObjectPool<HPBarItemObject> _hpBarItemObjectPool = null;
private List<HPBarItem> _activeHPBarItems = null;
private Canvas _cachedCanvas = null;
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--)
{
HPBarItem hpBarItem = _activeHPBarItems[i];
if (hpBarItem.Refresh())
{
continue;
}
HideHPBar(hpBarItem);
}
}
public void ShowHPBar(EntityBase entity, float fromHPRatio, float toHPRatio)
{
if (entity == null)
{
Log.Warning("Entity is invalid.");
return;
}
HPBarItem hpBarItem = GetActiveHPBarItem(entity);
if (hpBarItem == null)
{
hpBarItem = CreateHPBarItem(entity);
_activeHPBarItems.Add(hpBarItem);
}
hpBarItem.Init(entity, _cachedCanvas, fromHPRatio, toHPRatio);
}
private void HideHPBar(HPBarItem hpBarItem)
{
hpBarItem.Reset();
_activeHPBarItems.Remove(hpBarItem);
_hpBarItemObjectPool.Unspawn(hpBarItem);
}
private HPBarItem GetActiveHPBarItem(EntityBase entity)
{
if (entity == null)
{
return null;
}
for (int i = 0; i < _activeHPBarItems.Count; i++)
{
if (_activeHPBarItems[i].Owner == entity)
{
return _activeHPBarItems[i];
}
}
return null;
}
private HPBarItem CreateHPBarItem(EntityBase entity)
{
HPBarItem hpBarItem = null;
HPBarItemObject hpBarItemObject = _hpBarItemObjectPool.Spawn();
if (hpBarItemObject != null)
{
hpBarItem = (HPBarItem)hpBarItemObject.Target;
}
else
{
hpBarItem = Instantiate(_hpBarItemTemplate);
Transform transform = hpBarItem.GetComponent<Transform>();
transform.SetParent(_hpBarInstanceRoot);
transform.localScale = Vector3.one;
_hpBarItemObjectPool.Register(HPBarItemObject.Create(hpBarItem), true);
}
return hpBarItem;
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 879e921586721514f9f85de777050af4
timeCreated: 1528026155
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,122 @@
//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------
using System.Collections;
using Entity;
using UI;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
namespace StarForce
{
public class HPBarItem : MonoBehaviour
{
private const float AnimationSeconds = 0.3f;
private const float KeepSeconds = 0.4f;
private const float FadeOutSeconds = 0.3f;
[SerializeField]
private Slider m_HPBar = null;
private Canvas m_ParentCanvas = null;
private RectTransform m_CachedTransform = null;
private CanvasGroup m_CachedCanvasGroup = null;
private EntityBase m_Owner = null;
private int m_OwnerId = 0;
public EntityBase Owner
{
get
{
return m_Owner;
}
}
public void Init(EntityBase owner, Canvas parentCanvas, float fromHPRatio, float toHPRatio)
{
if (owner == null)
{
Log.Error("Owner is invalid.");
return;
}
m_ParentCanvas = parentCanvas;
gameObject.SetActive(true);
StopAllCoroutines();
m_CachedCanvasGroup.alpha = 1f;
if (m_Owner != owner || m_OwnerId != owner.Id)
{
m_HPBar.value = fromHPRatio;
m_Owner = owner;
m_OwnerId = owner.Id;
}
Refresh();
StartCoroutine(HPBarCo(toHPRatio, AnimationSeconds, KeepSeconds, FadeOutSeconds));
}
public bool Refresh()
{
if (m_CachedCanvasGroup.alpha <= 0f)
{
return false;
}
if (m_Owner != null && Owner.Available && Owner.Id == m_OwnerId)
{
Vector3 worldPosition = m_Owner.CachedTransform.position + Vector3.forward;
Vector3 screenPosition = GameEntry.Scene.MainCamera.WorldToScreenPoint(worldPosition);
Vector2 position;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)m_ParentCanvas.transform, screenPosition,
m_ParentCanvas.worldCamera, out position))
{
m_CachedTransform.localPosition = position;
}
}
return true;
}
public void Reset()
{
StopAllCoroutines();
m_CachedCanvasGroup.alpha = 1f;
m_HPBar.value = 1f;
m_Owner = null;
gameObject.SetActive(false);
}
private void Awake()
{
m_CachedTransform = GetComponent<RectTransform>();
if (m_CachedTransform == null)
{
Log.Error("RectTransform is invalid.");
return;
}
m_CachedCanvasGroup = GetComponent<CanvasGroup>();
if (m_CachedCanvasGroup == null)
{
Log.Error("CanvasGroup is invalid.");
return;
}
}
private IEnumerator HPBarCo(float value, float animationDuration, float keepDuration, float fadeOutDuration)
{
yield return m_HPBar.SmoothValue(value, animationDuration);
yield return new WaitForSeconds(keepDuration);
yield return m_CachedCanvasGroup.FadeToAlpha(0f, fadeOutDuration);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9bcc0608685184647b097f4838a8e9f6
timeCreated: 1528026155
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,34 @@
//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------
using GameFramework;
using GameFramework.ObjectPool;
using UnityEngine;
namespace StarForce
{
public class HPBarItemObject : ObjectBase
{
public static HPBarItemObject Create(object target)
{
HPBarItemObject hpBarItemObject = ReferencePool.Acquire<HPBarItemObject>();
hpBarItemObject.Initialize(target);
return hpBarItemObject;
}
protected override void Release(bool isShutdown)
{
HPBarItem hpBarItem = (HPBarItem)Target;
if (hpBarItem == null)
{
return;
}
Object.Destroy(hpBarItem.gameObject);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 8e6d2d8655da0094bb6063ce38ed582d
timeCreated: 1528026155
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: