using GCSeries.Core.Input; using QFramework; using QFramework.Example; using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using XMLTool; public class DeviceController : MonoSingleton { public Dictionary deviceDict = new Dictionary(); public class DeviceData { public DeviceItem item; public Device device; } public override void OnSingletonInit() { base.OnSingletonInit(); TypeEventSystem.Global.Register(OnStart).UnRegisterWhenGameObjectDestroyed(gameObject); TypeEventSystem.Global.Register(OnQuit).UnRegisterWhenGameObjectDestroyed(gameObject); #if VR UIRoot.Instance.transform.Find("ZStylus").GetComponent()?.OnObjectEntered.AddListener(OnObjEnter); UIRoot.Instance.transform.Find("ZMouse").GetComponent()?.OnObjectEntered.AddListener(OnObjEnter); #endif } private void OnObjEnter(ZPointer arg0, GameObject arg1) { var deviceItem = arg1.GetComponent(); var uitip = UIKit.GetPanel(); if (deviceItem != null && deviceItem.tipItem != null) { if (uitip != null) { UIKit.OpenPanelAsync(UILevel.PopUI).ToAction().Start(this, () => { }); } if (uitip != null) { uitip.Open(new UIDeviceTipData() { txt = deviceItem.tipItem.label }); uitip.Show(); return; } } else { uitip?.Hide(); } } private void OnQuit(OnModuleQuit quit) { foreach (var item in deviceDict) { item.Value.item?.Close(); item.Value.device = null; } deviceDict.Clear(); } private void OnStart(OnModuleStart start) { foreach (var device in Global.Instance.curModule.Devices) { GameObject obj = null; DeviceItem item = null; if (string.IsNullOrEmpty(device.Path) == false) { obj = Utility.FindObj(device.Path); item = obj?.GetOrAddComponent(); item?.Init(device); } deviceDict.Add(device.Name, new DeviceData() { device = device, item = item }); } } public Device GetDevice(string name) { if (deviceDict.ContainsKey(name)) { return deviceDict[name]?.device; } return null; } public GameObject GetDeviceObj(string name) { return GetDeviceItem(name)?.gameObject; } private void Update() { #if !VR var uitip = UIKit.GetPanel(); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { var deviceItem = hit.transform.GetComponent(); if (deviceItem != null) { if (Input.GetMouseButtonUp(1)) { if (deviceItem.device.rightMenu != null) { var data = new UIRightMenuData(); data.btns = deviceItem.device.rightMenu.items; data.OnBtnClick = str => { // 调试输出:确认str的值和类型 if (str.Equals("拾取", StringComparison.Ordinal)) { PlayerController.Instance.PickDevice(deviceItem.device.Name); } }; UIKit.OpenPanelAsync(canvasLevel: UILevel.PopUI, uiData: data).ToAction().StartGlobal(); } } else if (uitip != null && deviceItem.tipItem != null && EventSystem.current.IsPointerOverGameObject() == false) { uitip.Open(new UIDeviceTipData() { txt = deviceItem.tipItem.label }); uitip.Show(); return; } } } uitip?.Hide(); #endif } public DeviceItem GetDeviceItem(string name) { if (deviceDict.ContainsKey(name)) { return deviceDict[name]?.item; } return null; } }