- 规范 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,9 @@
fileFormatVersion: 2
guid: 33ba0562010e2da46883baefb271f573
folderAsset: yes
timeCreated: 1528026146
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,77 @@
//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------
using Definition.DataStruct;
using GameFramework;
using StarForce;
using UI;
using UnityEngine;
using UnityGameFramework.Runtime;
namespace CustomComponent
{
public class BuiltinDataComponent : GameFrameworkComponent
{
[SerializeField]
private TextAsset m_BuildInfoTextAsset = null;
[SerializeField]
private TextAsset m_DefaultDictionaryTextAsset = null;
[SerializeField]
private UpdateResourceForm m_UpdateResourceFormTemplate = null;
private BuildInfo m_BuildInfo = null;
public BuildInfo BuildInfo
{
get
{
return m_BuildInfo;
}
}
public UpdateResourceForm UpdateResourceFormTemplate
{
get
{
return m_UpdateResourceFormTemplate;
}
}
public void InitBuildInfo()
{
if (m_BuildInfoTextAsset == null || string.IsNullOrEmpty(m_BuildInfoTextAsset.text))
{
Log.Info("Build info can not be found or empty.");
return;
}
m_BuildInfo = Utility.Json.ToObject<BuildInfo>(m_BuildInfoTextAsset.text);
if (m_BuildInfo == null)
{
Log.Warning("Parse build info failure.");
return;
}
}
public void InitDefaultDictionary()
{
if (m_DefaultDictionaryTextAsset == null || string.IsNullOrEmpty(m_DefaultDictionaryTextAsset.text))
{
Log.Info("Default dictionary can not be found or empty.");
return;
}
if (!GameEntry.Localization.ParseData(m_DefaultDictionaryTextAsset.text))
{
Log.Warning("Parse default dictionary failure.");
return;
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: cfb0b001fcb30fe45ac95ddb8c28ba57
timeCreated: 1528026146
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 259253e364fe32a4195e182f78b8cc54
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,190 @@
using System.Collections.Generic;
using DataTable;
using Entity;
using Entity.EntityData;
using GameFramework.Event;
using Procedure;
using StarForce;
using UnityEngine;
using UnityGameFramework.Runtime;
using Random = UnityEngine.Random;
namespace CustomComponent
{
public class EnemyManagerComponent : GameFrameworkComponent
{
private EntityComponent _entity;
private List<EntityBase> _enemies;
public List<EntityBase> Enemies => _enemies;
private float _spawnEnemyTimer;
private int _spawnEnemyMaxCount = 5000;
private int _currentEnemyCount;
private int _spawnDistanceFromPlayer = 20;
private int _currentSpawnEnemyId;
private int _currentLevel;
private float[] _spawnEnemyIntervals;
private int[] _spawnEnemyIds;
private int[] _spawnEnemyCounts;
private float _duration;
private float[] _nextSpawnTimes;
private Transform _player;
private GameStateBattle _battle;
#region FSM
private void Start()
{
_entity = GameEntry.Entity;
_enemies = new List<EntityBase>();
GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
GameEntry.Event.Subscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete);
}
private void OnDestroy()
{
GameEntry.Event.Unsubscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
GameEntry.Event.Unsubscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete);
_enemies = null;
_entity = null;
}
public void OnInit(int level, GameStateBattle battle)
{
_battle = battle;
_currentLevel = level;
DRLevel levelData = GameEntry.DataTable.GetDataTableRow<DRLevel>(_currentLevel);
_spawnEnemyIntervals = levelData.Intervals;
_spawnEnemyIds = levelData.EntityIds;
_spawnEnemyCounts = levelData.EntityCounts;
_duration = levelData.Duration;
_nextSpawnTimes = (float[])_spawnEnemyIntervals.Clone();
_currentSpawnEnemyId = 0;
}
public void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
_spawnEnemyTimer += elapseSeconds;
// if (_spawnEnemyTimer < _spawnEnemyInterval) return;
// SpawnEnemy();
// _spawnEnemyTimer = 0;
// _spawnEnemyInterval = Mathf.Max(0.1f, _spawnEnemyInterval - _spawnEnemyAccelerate);
if (_spawnEnemyTimer > _duration)
{
_battle.LevelOver();
return;
}
for (int i = 0; i < _nextSpawnTimes.Length; i++)
{
float nextSpawnTime = _nextSpawnTimes[i];
if (_spawnEnemyTimer < nextSpawnTime) continue;
for (int j = 0; j < _spawnEnemyCounts[i]; j++)
{
SpawnEnemy(_spawnEnemyIds[i]);
}
_nextSpawnTimes[i] += _spawnEnemyIntervals[i];
}
}
public void OnReset()
{
_currentEnemyCount = 0;
_spawnEnemyTimer = 0;
_currentSpawnEnemyId = 0;
_currentLevel = 0;
_spawnEnemyIntervals = null;
_spawnEnemyIds = null;
_spawnEnemyCounts = null;
_duration = 0;
_nextSpawnTimes = null;
_battle = null;
ClearEnemies();
}
#endregion
private void SpawnEnemy(int entityId)
{
if (_player == null) return;
if (_currentEnemyCount >= _spawnEnemyMaxCount) return;
int entityPoolId = _currentSpawnEnemyId % _spawnEnemyMaxCount;
var enemyData = new EnemyData(entityPoolId, entityId)
{
Position = GetRandomPosition()
};
_entity.ShowEnemy(enemyData);
_currentSpawnEnemyId++;
}
private Vector3 GetRandomPosition()
{
float x = Random.Range(-1f, 1f);
float z = Random.Range(-1f, 1f);
Vector3 dir = new Vector3(x, 0, z).normalized;
return _player.position + dir * _spawnDistanceFromPlayer;
}
public void ClearEnemies()
{
foreach (var enemy in _enemies)
{
if (enemy == null || !enemy.Available) continue;
_entity.HideEntity(enemy);
}
_enemies.Clear();
}
#region Event Handler
private void OnShowEntitySuccess(object sender, GameEventArgs e)
{
if (!(e is ShowEntitySuccessEventArgs ne)) return;
if (ne.Entity.Logic is EnemyBase enemy)
{
_currentEnemyCount++;
enemy.SetTarget(_player);
_enemies.Add(enemy);
}
if (ne.EntityLogicType == typeof(Player))
{
_player = ne.Entity.transform;
}
}
private void OnHideEntityComplete(object sender, GameEventArgs e)
{
if (e is HideEntityCompleteEventArgs ne)
{
if (ne.EntityGroup.Name == "Enemy")
{
_currentEnemyCount--;
}
}
}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8d840df1b53db744cb522b82426f2cd5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: f75cb14d28565904e89eb5765829c210
folderAsset: yes
timeCreated: 1528026155
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
@@ -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:
@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using Definition.Enum;
using UnityGameFramework.Runtime;
using UI;
namespace CustomComponent
{
public class UIRouterComponent : GameFrameworkComponent
{
private readonly Dictionary<UIFormType, IUIFormController> _routeControllers = new();
public void BindUIUseCase(UIFormType uiFormType, IUIUseCase useCase)
{
IUIFormController controller = GetOrCreateController(uiFormType);
controller.BindUseCase(useCase);
}
public int? OpenUI(UIFormType uiFormType, object userData = null)
{
IUIFormController controller = GetOrCreateController(uiFormType);
return controller.OpenUI(userData);
}
public void CloseUI(UIFormType uiFormType)
{
IUIFormController controller = GetOrCreateController(uiFormType);
controller.CloseUI();
}
private IUIFormController GetOrCreateController(UIFormType uiFormType)
{
if (_routeControllers.TryGetValue(uiFormType, out IUIFormController controller))
{
return controller;
}
string typename = $"UI.{uiFormType}Controller";
Type controllerType = Type.GetType(typename);
if (controllerType == null)
{
controller = new DefaultUIFormController(uiFormType);
}
else
{
controller = (IUIFormController)Activator.CreateInstance(controllerType);
}
_routeControllers.Add(uiFormType, controller);
return controller;
}
private void OnDestroy()
{
foreach (KeyValuePair<UIFormType, IUIFormController> pair in _routeControllers)
{
pair.Value.CloseUI();
}
_routeControllers.Clear();
}
private class DefaultUIFormController : IUIFormController
{
private readonly UIFormType _uiFormType;
private int? _lastSerialId;
public DefaultUIFormController(UIFormType uiFormType)
{
_uiFormType = uiFormType;
}
public int? OpenUI(object userData = null)
{
_lastSerialId = GameEntry.UI.OpenUIForm(_uiFormType, userData);
return _lastSerialId;
}
public void CloseUI()
{
if (_lastSerialId.HasValue)
{
GameEntry.UI.CloseUIForm(_lastSerialId.Value);
_lastSerialId = null;
return;
}
UGuiForm uiForm = GameEntry.UI.GetUIForm(_uiFormType);
if (uiForm != null)
{
GameEntry.UI.CloseUIForm(uiForm);
}
}
public void BindUseCase(IUIUseCase useCase)
{
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b3695ff420be99d44ab812d7e1863044
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: