添加ui管理内容

pull/1/head
zhangxl 2024-07-19 10:51:48 +08:00
parent 4e98414b80
commit 3feca978f6
21 changed files with 464 additions and 12 deletions

View File

@ -1,3 +1,8 @@
fileFormatVersion: 2
guid: a798f7d3bd41482e8a7315550d8fe3bb
timeCreated: 1721006032
fileFormatVersion: 2
guid: 26159df8fe6ea2c48a8a17dd6cbbdc9d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using Sirenix.Utilities;
namespace ZC
{
public static class AssemblyManager
{
private static List<Type> _types;
public static void Initialize()
{
var types = typeof(AssemblyManager).Assembly.GetTypes();
_types = new List<Type>(types);
}
public static void GetTypesInhertType<T>(Type baseType, List<(Type, T)> types) where T : Attribute
{
foreach (var type in _types)
{
if (!baseType.IsAssignableFrom(type))
continue;
var attribute = type.GetCustomAttribute<T>();
if (attribute == null)
continue;
types.Add((type, attribute));
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: db72c038a608450b86ea74714730cdbe
timeCreated: 1712243832

View File

@ -0,0 +1,19 @@
using UnityEngine;
namespace ZC
{
public interface IUI
{
bool isPause { get; }
bool isActive { get; }
GameObject self { get; }
void Init();
void Open();
void Update(float dateTime);
void Close();
void Pause();
void Resume();
void Dispose();
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9578a65c4319423e84d612d3bf472a9e
timeCreated: 1712025643

View File

@ -0,0 +1,22 @@
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace ZC
{
public interface IUIManager
{
TMP_FontAsset font { get; set; }
IUI CreateUI(UIType uiType, string path, UILayer uiLayer);
IUI ShowUI(UIType uiType);
T GetUI<T>(UIType uiType) where T : UIBase;
bool HideUI(UIType uiType);
IUI CloseLast();
void CloseAll();
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 00fd583eb08d477f9c5c2c72ff8721e8
timeCreated: 1712025554

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fdc33b6bb92f4da98bde09c49e21be36
timeCreated: 1712381775

View File

@ -0,0 +1,48 @@
using UnityEngine.UI;
using ZGame;
namespace ZC
{
[UIType(UIType.GameUI)]
public class GameUI : UIBase
{
private Button btnLoad1;
private Button btnLoad2;
private Button btnDelete1;
private Button btnDelete2;
public override void Init()
{
base.Init();
btnLoad1 = self.transform.FindChildDeep<Button>("btnLoadModel1");
btnLoad2 = self.transform.FindChildDeep<Button>("btnLoadModel2");
btnDelete1 = self.transform.FindChildDeep<Button>("btnDeleteModel1");
btnDelete2 = self.transform.FindChildDeep<Button>("btnDeleteModel2");
btnLoad1.onClick.AddListener(OnClickBtnLoad1);
btnLoad2.onClick.AddListener(OnClickBtnLoad2);
btnDelete1.onClick.AddListener(OnClickBtnDelete1);
btnDelete2.onClick.AddListener(OnClickBtnDelete2);
}
private void OnClickBtnDelete2()
{
// EventManager.Instance.FireNow(this, new DeleteModelEventArgs(AssetConst.Assets_Res_Prefab_Player_测试模型2_prefab));
}
private void OnClickBtnDelete1()
{
// EventManager.Instance.FireNow(this, new DeleteModelEventArgs(AssetConst.Assets_Res_Prefab_Player_测试模型1_prefab));
}
private void OnClickBtnLoad2()
{
// EventManager.Instance.FireNow(this, new LoadModelEventArgs(AssetConst.Assets_Res_Prefab_Player_测试模型2_prefab));
}
private void OnClickBtnLoad1()
{
// EventManager.Instance.FireNow(this, new LoadModelEventArgs(AssetConst.Assets_Res_Prefab_Player_测试模型1_prefab));
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4fa4c046b5e04e3ea549314b78e59bf4
timeCreated: 1719218121

View File

@ -0,0 +1,31 @@
using TMPro;
using UnityEngine.UI;
namespace ZC
{
[UIType(UIType.LoadingUI)]
public class LoadingUI : UIBase
{
private TMP_Text txtSlider;
private Slider _slider;
public override void Init()
{
base.Init();
this.txtSlider = self.transform.FindChildDeep<TMP_Text>("txtSlider");
this._slider = self.transform.FindChildDeep<Slider>("slider");
}
public override void Open()
{
base.Open();
this._slider.value = 0;
}
public void UpdateSlider(float f)
{
this._slider.value = f;
this.txtSlider.text = $"进度:{f * 100}%";
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1d9c1bdf5bce4e559542978292cf76c5
timeCreated: 1719215734

View File

@ -0,0 +1,74 @@
using System;
using UnityEngine;
namespace ZC
{
[AttributeUsage(AttributeTargets.Class)]
class UITypeAttribute: Attribute
{
public UIType UIType { get; set; }
public UITypeAttribute(UIType uiType)
{
UIType = uiType;
}
}
public abstract class UIBase : IUI
{
private bool _isPause;
private bool _isActive;
private GameObject _self;
private CanvasGroup _group;
public bool isPause => _isPause;
public bool isActive => _isActive;
public GameObject self => _self;
public void SetGameObject(GameObject gameObject, bool isPause = true, bool isActive = false)
{
this._self = gameObject;
}
public virtual void Init()
{
_isPause = false;
_isActive = true;
}
public virtual void Open()
{
_self.SetActive(true);
}
public void Update(float dateTime)
{
if (_isPause) return;
}
public virtual void Close()
{
_self.SetActive(false);
}
public virtual void Pause()
{
_isPause = true;
_group.interactable = false;
}
public virtual void Resume()
{
_isPause = false;
_group.interactable = true;
}
public virtual void Dispose()
{
_isActive = false;
_isPause = true;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f259060dd3e0409fb3834af8cb9c5078
timeCreated: 1712027046

View File

@ -0,0 +1,24 @@
using TMPro;
using UnityEngine;
namespace ZC
{
class UIInfo : MonoBehaviour
{
[SerializeField] public IUI ui;
public TMP_FontAsset font;
public void SetStart(IUI ui, TMP_FontAsset font)
{
this.ui = ui;
this.font = font;
return;
var findChildDeeps = this.transform.FindChildDeeps<TMP_Text>();
foreach (var tmpText in findChildDeeps)
{
tmpText.font = font;
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: aa6c9b462319459ca494d7979967fc65
timeCreated: 1712128595

View File

@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace ZC
{
internal class UIManager : ManagerBase<UIManager>, IUIManager
{
private Stack<UIBase> _uis = new Stack<UIBase>();
private Dictionary<UIType, UIBase> _uiDic = new Dictionary<UIType, UIBase>();
private Dictionary<UIType, Type> _types = new Dictionary<UIType, Type>();
public TMP_FontAsset font { get; set; }
public override void OnInit()
{
base.OnInit();
var types = new List<(Type, UITypeAttribute)>();
AssemblyManager.GetTypesInhertType<UITypeAttribute>(typeof(UIBase), types);
foreach (var (type, uiTypeAttribute) in types)
{
_types.Add(uiTypeAttribute.UIType, type);
}
}
public override void OnUpdate(float time)
{
base.OnUpdate(time);
foreach (var ui in _uis)
{
ui.Update(time);
}
}
public override void OnPause()
{
base.OnPause();
foreach (var ui in _uis)
{
ui.Pause();
}
}
public override void OnResume()
{
base.OnResume();
foreach (var ui in _uis)
{
ui.Resume();
}
}
public IUI CreateUI(UIType uiType, string path, UILayer uiLayer)
{
var gameObject = ResourcesLocalComponent.Instance.LoadUIGameObjectSync(path, uiLayer);
if (!this._types.TryGetValue(uiType, out var type))
{
throw new InvalidOperationException();
}
if (Activator.CreateInstance(type, false) is not UIBase ui)
throw new NullReferenceException();
ui.SetGameObject(gameObject);
ui.Init();
ui.Close();
_uiDic.Add(uiType, ui);
//#if UNITY_EDITOR
// view
var addComponent = gameObject.GetComponent<UIInfo>();
addComponent.SetStart(ui, this.font);
//#endif
return ui;
}
public IUI ShowUI(UIType uiType)
{
if (_uiDic.TryGetValue(uiType, out var ui))
{
_uis.Push(ui);
ui.Open();
return ui;
}
return null;
}
public T GetUI<T>(UIType uiType) where T : UIBase
{
if (_uiDic.TryGetValue(uiType, out var ui))
{
if (ui is not T t)
throw new InvalidCastException();
return t;
}
return default;
}
public bool HideUI(UIType uiType)
{
if (_uiDic.TryGetValue(uiType, out var ui))
{
ui.Close();
return true;
}
return false;
}
public IUI CloseLast()
{
if (_uis.Count > 0)
{
var ui = _uis.Pop();
ui.Close();
return ui;
}
return null;
}
public void CloseAll()
{
while (_uis.Count > 0)
{
var ui = _uis.Pop();
ui.Close();
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 43d5ea11aafb4f9d8b7b4a05bd42a6d8
timeCreated: 1712025435

View File

@ -0,0 +1,16 @@
namespace ZC
{
public enum UIType
{
LoadingUI,
GameUI
}
public enum UILayer
{
Min,
Low,
Mid,
High,
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b11ce1cbd194451dba81be6d30716635
timeCreated: 1712137075

View File

@ -6,22 +6,34 @@ namespace ZC
public class ZCGame
{
private GameObject _self;
private Transform _objectPool;
private bool isDisposed;
private bool isPause;
private float gameTime;
public static ZCGame _zcGame;
private ObjectManager _objectManager;
private UIManager _uiManager;
private ProcedureManager _procedureManager;
public static GameObject Self => _zcGame._self;
public static Transform ObjectPool => _zcGame._objectPool;
public static float GameTime => _zcGame.gameTime;
public static IObjectManager ObjectManager => _zcGame._objectManager;
public static IUIManager UIManager => _zcGame._uiManager;
public static IProcedureManager ProcedureManager => _zcGame._procedureManager;
public ZCGame(GameObject self)
{
_zcGame = this;
this._self = self;
this._objectPool = self.transform.Find("ObjectPool");
this.isDisposed = false;
this.isPause = false;
AssemblyManager.Initialize();
this.UpdateGame().Forget();
}
@ -58,19 +70,21 @@ namespace ZC
void Init()
{
UniTask.Void(async () =>
{
await ResourcesLocalComponent.Instance.LoadSceneAsync(AssetsConst.Assets_DemoGame_GameRes_Scene_Game_unity);
var loadAssetAsync = await ResourcesLocalComponent.Instance.LoadAssetAndInsAsync(AssetsConst.Assets_DemoGame_GameRes_Entity_Cube_prefab);
Debug.Log("加载的是:" + loadAssetAsync);
});
}
this._objectManager = new ObjectManager();
this._objectManager.OnInit();
TestZhongLi testZhongLi = new TestZhongLi();
this._uiManager = new UIManager();
this._procedureManager = new ProcedureManager();
this._objectManager.Add(this._uiManager);
this._objectManager.Add(this._procedureManager);
this._procedureManager.ChangeProcedure(ProcedureType.LoadingGameSceneProcedure);
}
void Update(float time)
{
testZhongLi.Update();
this._objectManager.OnUpdate(time);
}
void LateUpdate(float time)
@ -79,6 +93,7 @@ namespace ZC
void Dispose()
{
this._objectManager.OnDispose();
Debug.Log("关闭应用了");
}
}