| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using UnityEngine;
- namespace Model
- {
- [ObjectEvent]
- public class UIComponentEvent : ObjectEvent<UIComponent>, IAwake, ILoader
- {
- public void Load()
- {
- UIComponent component = GetValue();
- component.Load();
- }
- public void Awake()
- {
- this.GetValue().Awake();
- }
- }
-
- /// <summary>
- /// 管理所有UI
- /// </summary>
- public class UIComponent: Component
- {
- private UI Root;
- private Dictionary<UIType, IUIFactory> UiTypes;
- private readonly Dictionary<UIType, UI> uis = new Dictionary<UIType, UI>();
- public override void Dispose()
- {
- if (this.Id == 0)
- {
- return;
- }
- base.Dispose();
- foreach (UIType type in uis.Keys.ToArray())
- {
- UI ui;
- if (!uis.TryGetValue(type, out ui))
- {
- continue;
- }
- uis.Remove(type);
- ui.Dispose();
- }
- }
- public void Awake()
- {
- GameObject uiCanvas = GameObject.Find("/UICanvas");
- uiCanvas.GetComponent<Canvas>().worldCamera = GameObject.Find("/Camera").GetComponent<Camera>();
- this.Root = new UI(this.GetOwner<Scene>(), UIType.Root, null, uiCanvas);
- this.Load();
- }
- public void Load()
- {
- this.UiTypes = new Dictionary<UIType, IUIFactory>();
- Assembly[] assemblies = ObjectManager.Instance.GetAssemblies();
- foreach (Assembly assembly in assemblies)
- {
- Type[] types = assembly.GetTypes();
- foreach (Type type in types)
- {
- object[] attrs = type.GetCustomAttributes(typeof(UIFactoryAttribute), false);
- if (attrs.Length == 0)
- {
- continue;
- }
- UIFactoryAttribute attribute = attrs[0] as UIFactoryAttribute;
- if (this.UiTypes.ContainsKey(attribute.Type))
- {
- throw new GameException($"已经存在同类UI Factory: {attribute.Type}");
- }
- IUIFactory iIuiFactory = Activator.CreateInstance(type) as IUIFactory;
- if (iIuiFactory == null)
- {
- throw new GameException("UI Factory没有继承IUIFactory");
- }
- this.UiTypes.Add(attribute.Type, iIuiFactory);
- }
- }
- }
- public UI Create(UIType type)
- {
- try
- {
- UI ui = this.UiTypes[type].Create(this.GetOwner<Scene>(), type, this.Root);
- this.uis.Add(type, ui);
- return ui;
- }
- catch (Exception e)
- {
- throw new Exception($"{type} UI 错误: {e}");
- }
- }
- public void Add(UIType type, UI ui)
- {
- this.uis.Add(type, ui);
- }
- public void Remove(UIType type)
- {
- UI ui;
- if (!this.uis.TryGetValue(type, out ui))
- {
- return;
- }
- this.uis.Remove(type);
- ui.Dispose();
- }
- public void RemoveAll()
- {
- foreach (UIType type in this.uis.Keys.ToArray())
- {
- UI ui;
- if (!this.uis.TryGetValue(type, out ui))
- {
- continue;
- }
- this.uis.Remove(type);
- ui.Dispose();
- }
- }
-
- public UI Get(UIType type)
- {
- UI ui;
- this.uis.TryGetValue(type, out ui);
- return ui;
- }
- public List<UIType> GetUITypeList()
- {
- return new List<UIType>(this.uis.Keys);
- }
- }
- }
|