MapKeys.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System.Collections.Generic;
  2. using XGame.Framework.Map;
  3. namespace FL.Map
  4. {
  5. public static partial class MapKeys
  6. {
  7. private static Dictionary<string, MapKey> _nameToKeys;
  8. public static MapKey NameToKey(string mapName)
  9. {
  10. if (_nameToKeys == null)
  11. {
  12. _nameToKeys = new Dictionary<string, MapKey>();
  13. }
  14. if (_nameToKeys.TryGetValue(mapName, out MapKey key))
  15. {
  16. return key;
  17. }
  18. var property = typeof(MapKeys).GetProperty(mapName, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.IgnoreCase);
  19. if (property != null)
  20. {
  21. key = property.GetValue(null) as MapKey;
  22. _nameToKeys.Add(mapName, key);
  23. return key;
  24. }
  25. XGame.Log.Error($"找不到MapKey. mapName:{mapName}");
  26. return default;
  27. }
  28. public static void Dispose()
  29. {
  30. _nameToKeys?.Clear();
  31. _nameToKeys = null;
  32. }
  33. }
  34. }