UIKeys.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections.Generic;
  2. using XGame.Framework.UI;
  3. namespace FL.FGUI
  4. {
  5. public static partial class UIKeys
  6. {
  7. private static Dictionary<string, UIKey> _nameToKeys;
  8. private static Dictionary<UIKey, UILayer> _uikeyToLayerMap;
  9. public static UIKey NameToKey(string uiName)
  10. {
  11. if (_nameToKeys == null)
  12. {
  13. _nameToKeys = new Dictionary<string, UIKey>();
  14. }
  15. if (_nameToKeys.TryGetValue(uiName, out UIKey key))
  16. {
  17. return key;
  18. }
  19. var property = typeof(UIKeys).GetProperty(uiName, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.IgnoreCase);
  20. if (property != null)
  21. {
  22. key = property.GetValue(null) as UIKey;
  23. _nameToKeys.Add(uiName, key);
  24. return key;
  25. }
  26. XGame.Log.Error($"找不到UIKey. uiName:{uiName}");
  27. return default;
  28. }
  29. public static UILayer GetLayer(UIKey uikey)
  30. {
  31. if (_uikeyToLayerMap == null)
  32. {
  33. _uikeyToLayerMap = new Dictionary<UIKey, UILayer>();
  34. }
  35. if (_uikeyToLayerMap.TryGetValue(uikey, out var layer))
  36. {
  37. return layer;
  38. }
  39. layer = UILayer.Normal;
  40. var property = uikey.UIViewType.GetProperty("Layer", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.IgnoreCase);
  41. if (property != null)
  42. {
  43. layer = (UILayer)property.GetValue(null);
  44. }
  45. _uikeyToLayerMap.Add(uikey, layer);
  46. return layer;
  47. }
  48. public static void Dispose()
  49. {
  50. _nameToKeys?.Clear();
  51. _nameToKeys = null;
  52. _uikeyToLayerMap?.Clear();
  53. _uikeyToLayerMap = null;
  54. }
  55. }
  56. }