| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using JCUnityLib;
- using LightGlue.Unity.Game;
- using LightGlue.Unity.UI;
- using Unity.VisualScripting;
- using UnityEngine;
- //using LightGlue.Unity.Runtime;
- public class LightGlueEntryView : JCUnityLib.ViewBase
- {
- [Header("Roma 系统预制体")]
- [Tooltip("RomaManager 预制,由 GameManager 在 Awake 时生成(跨场景持久化)。若未配置则不会创建。")]
- [SerializeField]
- private GameObject romaSystemPrefab;
- [Header("LightGlueUI 系统预制体(可选)")]
- [Tooltip("LightGlueUI 预制,由 LightGlueUI 在 Awake 时生成(跨场景持久化)。若未配置则不会创建。")]
- [SerializeField]
- private GameObject lightGlueUIPrefab;
- [Header("LightGlueUI 类型")]
- [Tooltip("Demo:显示 Demo/BLE 相关 UI;Plugin:隐藏 Demo UI,显示插件额外 UI。")]
- [SerializeField]
- private LightGlueUIManager.UIType lightGlueUIType = LightGlueUIManager.UIType.Demo;
- [Header("BleUI(可选,仅 Demo 使用)")]
- [Tooltip("是否在生成 LightGlueUI 后,自动在 BLEContainer 下挂载 BleUI 预制。")]
- [SerializeField]
- private bool enableBleUi = false;
- [Tooltip("BleUI 预制体(建议放在 Assets/Demo/Prefabs/BleUI.prefab)。未配置则不会创建。")]
- [SerializeField]
- private GameObject bleUiPrefab;
- [Tooltip("LightGlueUI 里用于挂载 BleUI 的容器节点名。")]
- [SerializeField]
- private string bleContainerName = "BLEContainer";
- public static LightGlueEntryView _ins;
- public static void Create()
- {
- if (_ins != null) return;
- // 这里按你项目资源路径调整
- GameObject o = Object.Instantiate(Resources.Load<GameObject>("LightGlueEntryView"));
- Object.DontDestroyOnLoad(o);
- _ins = o.GetComponent<LightGlueEntryView>();
- // 挂到你们 ViewMgr 层级(按你实际节点修改)
- //if (ViewMgr.Instance != null)
- //{
- // var parent = ViewMgr.Instance.transform.Find("1");
- // if (parent != null) o.transform.SetParent(parent, false);
- //}
- _ins.OnCreated();
- }
- private void setViewMgrParent(GameObject o,GameObject parentObj = null) {
- // 挂到你们 ViewMgr 层级(按你实际节点修改)
- if (ViewMgr.Instance != null)
- {
- var parent = parentObj ? parentObj.transform : ViewMgr.Instance.transform.Find("1");
- if (parent != null) o.transform.SetParent(parent, false);
- }
- }
- // 初始化运行时
- private void OnCreated()
- {
- // 确保全局 RomaManager 只创建一次(跨场景)
- if (LightGlue.Unity.Roma.RomaManager.Instance == null && romaSystemPrefab != null)
- {
- GameObject romaSystemObj = Instantiate(romaSystemPrefab);
- //setViewMgrParent(romaSystemObj,this.gameObject);
- Debug.Log("[GameManager] RomaSystem prefab instantiated.");
- }
- // LightGlueUI:只创建一次(避免切场景重复叠 UI)
- if (LightGlueUIManager.Instance == null && lightGlueUIPrefab != null)
- {
- GameObject lightGlueUIObj = Instantiate(lightGlueUIPrefab);
- setViewMgrParent(lightGlueUIObj);
- Debug.Log("[GameManager] LightGlueUI prefab instantiated.");
- }
- // 可选:在 LightGlueUI 下挂载 BleUI(Demo 专用,插件用户可不配置)
- ApplyLightGlueUIType();
- TryAttachBleUi();
- }
- private void OnDestroy()
- {
- // 按需:如果关闭页面要停算法,就调用 Stop
- // LightGlueRuntimeFacade.Runtime?.Stop();
- }
- private void Awake()
- {
-
- }
- private void TryAttachBleUi()
- {
- if (lightGlueUIType != LightGlueUIManager.UIType.Demo) return;
- if (!enableBleUi) return;
- if (bleUiPrefab == null) return;
- var uiMgr = LightGlueUIManager.Instance != null ? LightGlueUIManager.Instance : FindObjectOfType<LightGlueUIManager>();
- if (uiMgr == null || uiMgr.uiRoot == null) return;
- Transform container = uiMgr.bleContainer != null ? uiMgr.bleContainer : FindChildByName(uiMgr.uiRoot.transform, bleContainerName);
- if (container == null) return;
- // 防重复:避免直接引用 BLEUI 类型(插件导出时可能不包含 BLEUI.cs)
- // 这里按 BleUI 预制名判断是否已挂载过实例。
- string bleUiName = bleUiPrefab.name;
- if (HasChildNamed(container, bleUiName)) return;
- var inst = Instantiate(bleUiPrefab, container);
- inst.name = bleUiPrefab.name;
- Debug.Log("[GameManager] BleUI prefab attached under BLEContainer.");
- }
- private void ApplyLightGlueUIType()
- {
- var uiMgr = LightGlueUIManager.Instance != null ? LightGlueUIManager.Instance : FindObjectOfType<LightGlueUIManager>();
- if (uiMgr == null) return;
- uiMgr.SetUIType(lightGlueUIType);
- }
- private static Transform FindChildByName(Transform root, string childName)
- {
- if (root == null || string.IsNullOrWhiteSpace(childName)) return null;
- if (root.name == childName) return root;
- for (int i = 0; i < root.childCount; i++)
- {
- Transform t = root.GetChild(i);
- var found = FindChildByName(t, childName);
- if (found != null) return found;
- }
- return null;
- }
- private static bool HasChildNamed(Transform root, string name)
- {
- if (root == null || string.IsNullOrWhiteSpace(name)) return false;
- for (int i = 0; i < root.childCount; i++)
- {
- var t = root.GetChild(i);
- if (t == null) continue;
- if (t.name == name || t.name == $"{name}(Clone)") return true;
- }
- return false;
- }
- }
|