LightGlueEntryView.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using JCUnityLib;
  2. using LightGlue.Unity.Game;
  3. using LightGlue.Unity.UI;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6. //using LightGlue.Unity.Runtime;
  7. public class LightGlueEntryView : JCUnityLib.ViewBase
  8. {
  9. [Header("Roma 系统预制体")]
  10. [Tooltip("RomaManager 预制,由 GameManager 在 Awake 时生成(跨场景持久化)。若未配置则不会创建。")]
  11. [SerializeField]
  12. private GameObject romaSystemPrefab;
  13. [Header("LightGlueUI 系统预制体(可选)")]
  14. [Tooltip("LightGlueUI 预制,由 LightGlueUI 在 Awake 时生成(跨场景持久化)。若未配置则不会创建。")]
  15. [SerializeField]
  16. private GameObject lightGlueUIPrefab;
  17. [Header("LightGlueUI 类型")]
  18. [Tooltip("Demo:显示 Demo/BLE 相关 UI;Plugin:隐藏 Demo UI,显示插件额外 UI。")]
  19. [SerializeField]
  20. private LightGlueUIManager.UIType lightGlueUIType = LightGlueUIManager.UIType.Demo;
  21. [Header("BleUI(可选,仅 Demo 使用)")]
  22. [Tooltip("是否在生成 LightGlueUI 后,自动在 BLEContainer 下挂载 BleUI 预制。")]
  23. [SerializeField]
  24. private bool enableBleUi = false;
  25. [Tooltip("BleUI 预制体(建议放在 Assets/Demo/Prefabs/BleUI.prefab)。未配置则不会创建。")]
  26. [SerializeField]
  27. private GameObject bleUiPrefab;
  28. [Tooltip("LightGlueUI 里用于挂载 BleUI 的容器节点名。")]
  29. [SerializeField]
  30. private string bleContainerName = "BLEContainer";
  31. public static LightGlueEntryView _ins;
  32. public static void Create()
  33. {
  34. if (_ins != null) return;
  35. // 这里按你项目资源路径调整
  36. GameObject o = Object.Instantiate(Resources.Load<GameObject>("LightGlueEntryView"));
  37. Object.DontDestroyOnLoad(o);
  38. _ins = o.GetComponent<LightGlueEntryView>();
  39. // 挂到你们 ViewMgr 层级(按你实际节点修改)
  40. //if (ViewMgr.Instance != null)
  41. //{
  42. // var parent = ViewMgr.Instance.transform.Find("1");
  43. // if (parent != null) o.transform.SetParent(parent, false);
  44. //}
  45. _ins.OnCreated();
  46. }
  47. private void setViewMgrParent(GameObject o,GameObject parentObj = null) {
  48. // 挂到你们 ViewMgr 层级(按你实际节点修改)
  49. if (ViewMgr.Instance != null)
  50. {
  51. var parent = parentObj ? parentObj.transform : ViewMgr.Instance.transform.Find("1");
  52. if (parent != null) o.transform.SetParent(parent, false);
  53. }
  54. }
  55. // 初始化运行时
  56. private void OnCreated()
  57. {
  58. // 确保全局 RomaManager 只创建一次(跨场景)
  59. if (LightGlue.Unity.Roma.RomaManager.Instance == null && romaSystemPrefab != null)
  60. {
  61. GameObject romaSystemObj = Instantiate(romaSystemPrefab);
  62. //setViewMgrParent(romaSystemObj,this.gameObject);
  63. Debug.Log("[GameManager] RomaSystem prefab instantiated.");
  64. }
  65. // LightGlueUI:只创建一次(避免切场景重复叠 UI)
  66. if (LightGlueUIManager.Instance == null && lightGlueUIPrefab != null)
  67. {
  68. GameObject lightGlueUIObj = Instantiate(lightGlueUIPrefab);
  69. setViewMgrParent(lightGlueUIObj);
  70. Debug.Log("[GameManager] LightGlueUI prefab instantiated.");
  71. }
  72. // 可选:在 LightGlueUI 下挂载 BleUI(Demo 专用,插件用户可不配置)
  73. ApplyLightGlueUIType();
  74. TryAttachBleUi();
  75. }
  76. private void OnDestroy()
  77. {
  78. // 按需:如果关闭页面要停算法,就调用 Stop
  79. // LightGlueRuntimeFacade.Runtime?.Stop();
  80. }
  81. private void Awake()
  82. {
  83. }
  84. private void TryAttachBleUi()
  85. {
  86. if (lightGlueUIType != LightGlueUIManager.UIType.Demo) return;
  87. if (!enableBleUi) return;
  88. if (bleUiPrefab == null) return;
  89. var uiMgr = LightGlueUIManager.Instance != null ? LightGlueUIManager.Instance : FindObjectOfType<LightGlueUIManager>();
  90. if (uiMgr == null || uiMgr.uiRoot == null) return;
  91. Transform container = uiMgr.bleContainer != null ? uiMgr.bleContainer : FindChildByName(uiMgr.uiRoot.transform, bleContainerName);
  92. if (container == null) return;
  93. // 防重复:避免直接引用 BLEUI 类型(插件导出时可能不包含 BLEUI.cs)
  94. // 这里按 BleUI 预制名判断是否已挂载过实例。
  95. string bleUiName = bleUiPrefab.name;
  96. if (HasChildNamed(container, bleUiName)) return;
  97. var inst = Instantiate(bleUiPrefab, container);
  98. inst.name = bleUiPrefab.name;
  99. Debug.Log("[GameManager] BleUI prefab attached under BLEContainer.");
  100. }
  101. private void ApplyLightGlueUIType()
  102. {
  103. var uiMgr = LightGlueUIManager.Instance != null ? LightGlueUIManager.Instance : FindObjectOfType<LightGlueUIManager>();
  104. if (uiMgr == null) return;
  105. uiMgr.SetUIType(lightGlueUIType);
  106. }
  107. private static Transform FindChildByName(Transform root, string childName)
  108. {
  109. if (root == null || string.IsNullOrWhiteSpace(childName)) return null;
  110. if (root.name == childName) return root;
  111. for (int i = 0; i < root.childCount; i++)
  112. {
  113. Transform t = root.GetChild(i);
  114. var found = FindChildByName(t, childName);
  115. if (found != null) return found;
  116. }
  117. return null;
  118. }
  119. private static bool HasChildNamed(Transform root, string name)
  120. {
  121. if (root == null || string.IsNullOrWhiteSpace(name)) return false;
  122. for (int i = 0; i < root.childCount; i++)
  123. {
  124. var t = root.GetChild(i);
  125. if (t == null) continue;
  126. if (t.name == name || t.name == $"{name}(Clone)") return true;
  127. }
  128. return false;
  129. }
  130. }