using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; public class ModelButtonController : MonoBehaviour { // 按钮类型 //public ButtonType buttonType; // 存储按钮点击事件 public UnityEvent onClickEvent; private MyKeyBend keyBend; void Start() { //onClickEvent = new UnityEvent(); if(GetComponent()!=null) keyBend=GetComponent(); } // 鼠标点击时触发 void OnMouseDown() { if (IsClickUI()) return; // 执行绑定的事件 onClickEvent?.Invoke(); if (keyBend != null) keyBend.OnKeyPress(); } private bool IsClickUI() { PointerEventData eventData = new PointerEventData(EventSystem.current); eventData.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y); List raycastResults = new List(); EventSystem.current.RaycastAll(eventData, raycastResults); //string re = ""; //foreach (var r in raycastResults) //{ // re += r.gameObject.name; //} //Debug.Log(raycastResults.Count+ re); return raycastResults.Count > 0; } // 设置按钮点击事件 public void SetOnClickEvent(UnityAction action) { // 清除原来的事件,并绑定新的事件 //if (onClickEvent != null) onClickEvent.RemoveAllListeners(); onClickEvent.AddListener(action); } public void ClearAllEvent() { onClickEvent.RemoveAllListeners(); } }