using QFramework; using System; using System.Collections; using System.Collections.Generic; using Turing.Core.TuringInput; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; public class ObjectToggle : MonoBehaviour { public bool isOn = false; //EventHandler OnValueChanged public UnityEvent OnValueChanged = new UnityEvent(); private float mouseDownTime; private bool isMouseDown; bool isLock = false; private void Awake() { TypeEventSystem.Global.Register(OnLockEvent).UnRegisterWhenDisabled(this); #if Turing UIRoot.Instance.transform.Find("ZStylus").GetComponent().OnButtonPressed.AddListener(OnButtonPressed); UIRoot.Instance.transform.Find("ZStylus").GetComponent().OnButtonReleased.AddListener(OnButtonReleased); UIRoot.Instance.transform.Find("ZStylus").GetComponent().OnObjectEntered.AddListener(OnEnter); UIRoot.Instance.transform.Find("ZStylus").GetComponent().OnObjectExited.AddListener(OnExit); UIRoot.Instance.transform.Find("ZMouse").GetComponent().OnButtonPressed.AddListener(OnButtonPressed); UIRoot.Instance.transform.Find("ZMouse").GetComponent().OnButtonReleased.AddListener(OnButtonReleased); UIRoot.Instance.transform.Find("ZMouse").GetComponent().OnObjectEntered.AddListener(OnEnter); UIRoot.Instance.transform.Find("ZMouse").GetComponent().OnObjectExited.AddListener(OnExit); #endif } #if Turing bool isEnter = false; private void OnExit(TuringPointer arg0, GameObject arg1) { if (arg1 == gameObject) { isEnter = false; } } private void OnEnter(TuringPointer arg0, GameObject arg1) { if (arg1 == gameObject) { isEnter = true; } } private void OnButtonReleased(TuringPointer arg0, int arg1) { if (arg1 == 0 && isEnter) { OnMouseUp(); } } private void OnButtonPressed(TuringPointer arg0, int arg1) { if (arg1 == 0 && isEnter) { OnMouseDown(); } } #endif private void OnLockEvent(OnLock islock) { this.isLock = islock.isLock; } private void OnMouseDown() { if (isLock == true) { return; } if (EventSystem.current.IsPointerOverGameObject() == true) { return; } // 记录鼠标按下的时间 mouseDownTime = Time.time; isMouseDown = true; } private void OnMouseUp() { if (isMouseDown) { // 计算鼠标按下和抬起的时间间隔 float clickDuration = Time.time - mouseDownTime; // 检查时间间隔是否大于 0.2 秒 if (clickDuration < 0.2f) { if (isOn == false) { isOn = !isOn; OnValueChanged?.Invoke(isOn); } } isMouseDown = false; } } public void Set(bool isOn) { this.isOn = isOn; OnValueChanged?.Invoke(isOn); } }