ViewManager2.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ViewManager2
  5. {
  6. private class ViewCacheInfo
  7. {
  8. public string fullPath;
  9. public GameObject gameObject;
  10. }
  11. private static List<ViewCacheInfo> _ViewCacheInfos = new();
  12. public static void ShowView(string path)
  13. {
  14. string fullPath = "SmartBow/Prefabs/Views/" + path;
  15. _DestroyExistViews(fullPath);
  16. GameObject o = Object.Instantiate(Resources.Load<GameObject>(fullPath));
  17. ViewCacheInfo viewCacheInfo = new ViewCacheInfo();
  18. viewCacheInfo.fullPath = fullPath;
  19. viewCacheInfo.gameObject = o;
  20. _ViewCacheInfos.Add(viewCacheInfo);
  21. }
  22. public static GameObject getGameObjectAndShowView(string path) {
  23. string fullPath = "SmartBow/Prefabs/Views/" + path;
  24. _DestroyExistViews(fullPath);
  25. GameObject o = Object.Instantiate(Resources.Load<GameObject>(fullPath));
  26. ViewCacheInfo viewCacheInfo = new ViewCacheInfo();
  27. viewCacheInfo.fullPath = fullPath;
  28. viewCacheInfo.gameObject = o;
  29. _ViewCacheInfos.Add(viewCacheInfo);
  30. return o;
  31. }
  32. public static void HideView(string path)
  33. {
  34. string fullPath = "SmartBow/Prefabs/Views/" + path;
  35. _DestroyExistViews(fullPath);
  36. }
  37. private static void _DestroyExistViews(string fullPath)
  38. {
  39. _ViewCacheInfos.FindAll(e => e.fullPath == fullPath).ForEach(e =>
  40. {
  41. if (e.gameObject) Object.Destroy(e.gameObject);
  42. });
  43. _ViewCacheInfos.RemoveAll(e => e.fullPath == fullPath);
  44. }
  45. public static void RemoveView(string path)
  46. {
  47. string fullPath = "SmartBow/Prefabs/Views/" + path;
  48. _RemoveView(fullPath);
  49. }
  50. //仅移除 _ViewCacheInfos
  51. private static void _RemoveView(string fullPath)
  52. {
  53. _ViewCacheInfos.RemoveAll(e => e.fullPath == fullPath);
  54. }
  55. public const string Path_SettingsView = "Home/SettingsView";
  56. public const string Path_SocialView = "Home/SocialView";
  57. public const string Path_PersonalView = "Home/PersonalView";
  58. public const string Path_RankingView = "Home/RankingView";
  59. public const string Path_ConnectGuidanceView = "Home/ConnectGuidanceView";
  60. public const string Path_GyrGuidanceView = "Home/GyrGuidanceView";
  61. public const string Path_MagGuidanceView = "Home/MagGuidanceView";
  62. public const string Path_InfraredView = "Home/InfraredView";
  63. public const string Path_InfraredScreenPositioningView = "Home/InfraredScreenPositioningView";
  64. public const string Path_HomeViewTip = "Home/HomeView_Tip";
  65. public const string Path_GameResultView = "GameResultView";
  66. }