using System; using System.Collections; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.Serialization; namespace UnityEngine.UI { // Button that's meant to work with mouse or touch-based devices. [AddComponentMenu("UI/Button", 30)] public class Button : Selectable, IPointerClickHandler, ISubmitHandler { [Serializable] ///
/// public ButtonClickedEvent onClick { get { return m_OnClick; } set { m_OnClick = value; } } //按下,同时调用点击回调 private void Press() { if (!IsActive() || !IsInteractable()) return; UISystemProfilerApi.AddMarker("Button.onClick", this); m_OnClick.Invoke(); } ////// Function definition for a button click event. /// public class ButtonClickedEvent : UnityEvent {} // 点击事件回调 [FormerlySerializedAs("onClick")] [SerializeField] private ButtonClickedEvent m_OnClick = new ButtonClickedEvent(); protected Button() {} /// /// UnityEvent that is triggered when the button is pressed. /// Note: Triggered on MouseUp after MouseDown on the same object. /// /// /// /// using UnityEngine; /// using UnityEngine.UI; /// using System.Collections; /// /// public class ClickExample : MonoBehaviour /// { /// public Button yourButton; /// /// void Start() /// { /// Button btn = yourButton.GetComponent /// btn.onClick.AddListener(TaskOnClick); /// } /// /// void TaskOnClick() /// { /// Debug.Log("You have clicked the button!"); /// } /// } ///
/// Call all registered IPointerClickHandlers. /// Register button presses using the IPointerClickHandler. You can also use it to tell what type of click happened (left, right etc.). /// Make sure your Scene has an EventSystem. /// /// Pointer Data associated with the event. Typically by the event system. /// /// /// //Attatch this script to a Button GameObject /// using UnityEngine; /// using UnityEngine.EventSystems; /// /// public class Example : MonoBehaviour, IPointerClickHandler /// { /// //Detect if a click occurs /// public void OnPointerClick(PointerEventData pointerEventData) /// { /// //Use this to tell when the user right-clicks on the Button /// if (pointerEventData.button == PointerEventData.InputButton.Right) /// { /// //Output to console the clicked GameObject's name and the following message. You can replace this with your own actions for when clicking the GameObject. /// Debug.Log(name + " Game Object Right Clicked!"); /// } /// /// //Use this to tell when the user left-clicks on the Button /// if (pointerEventData.button == PointerEventData.InputButton.Left) /// { /// Debug.Log(name + " Game Object Left Clicked!"); /// } /// } /// } /// /// //鼠标点击的回调,先判断是否是左键点击,会调用按下 public virtual void OnPointerClick(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left) return; Press(); } ///
/// Call all registered ISubmitHandler. /// /// Associated data with the event. Typically by the event system. /// /// This detects when a Button has been selected via a "submit" key you specify (default is the return key). /// /// To change the submit key, either: /// /// 1. Go to Edit->Project Settings->Input. /// /// 2. Next, expand the Axes section and go to the Submit section if it exists. /// /// 3. If Submit doesn’t exist, add 1 number to the Size field. This creates a new section at the bottom. Expand the new section and change the Name field to “Submit”. /// /// 4. Change the Positive Button field to the key you want (e.g. space). /// /// /// Or: /// /// 1. Go to your EventSystem in your Project /// /// 2. Go to the Inspector window and change the Submit Button field to one of the sections in the Input Manager (e.g. "Submit"), or create your own by naming it what you like, then following the next few steps. /// /// 3. Go to Edit->Project Settings->Input to get to the Input Manager. /// /// 4. Expand the Axes section in the Inspector window. Add 1 to the number in the Size field. This creates a new section at the bottom. /// /// 5. Expand the new section and name it the same as the name you inserted in the Submit Button field in the EventSystem. Set the Positive Button field to the key you want (e.g. space) /// //提交回调 public virtual void OnSubmit(BaseEventData eventData) { Press(); // if we get set disabled during the press // don't run the coroutine. if (!IsActive() || !IsInteractable()) return; DoStateTransition(SelectionState.Pressed, false); StartCoroutine(OnFinishSubmit()); } //完成提交回调 private IEnumerator OnFinishSubmit() { var fadeTime = colors.fadeDuration; var elapsedTime = 0f; while (elapsedTime < fadeTime) { elapsedTime += Time.unscaledDeltaTime; yield return null; } DoStateTransition(currentSelectionState, false); } } }
对OnPointerClick溯源,调用侧来自ExecuteEvents类的Execute方法
再往上
在往上,来到StandaloneInputModule类的ReleaseMouse方法和ProcessTouchPress方法
这是一个为鼠标/键盘/控制器输入而设计的BaseInputModule类
private void ReleaseMouse(PointerEventData pointerEvent, GameObject currentOverGo)
{
ExecuteEvents.Execute(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerUpHandler);
//通过冒泡查找是哪个物体接收了点击
var pointerUpHandler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo);
// PointerClick and Drop events
if (pointerEvent.pointerPress == pointerUpHandler && pointerEvent.eligibleForClick)
{
ExecuteEvents.Execute(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerClickHandler); //执行点击
}
else if (pointerEvent.pointerDrag != null && pointerEvent.dragging)
{
ExecuteEvents.ExecuteHierarchy(currentOverGo, pointerEvent, ExecuteEvents.dropHandler);
}
pointerEvent.eligibleForClick = false;
pointerEvent.pointerPress = null;
pointerEvent.rawPointerPress = null;
if (pointerEvent.pointerDrag != null && pointerEvent.dragging)
ExecuteEvents.Execute(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.endDragHandler);
pointerEvent.dragging = false;
pointerEvent.pointerDrag = null;
// redo pointer enter / exit to refresh state
// so that if we moused over something that ignored it before
// due to having pressed on something else
// it now gets it.
if (currentOverGo != pointerEvent.pointerEnter)
{
HandlePointerExitAndEnter(pointerEvent, null);
HandlePointerExitAndEnter(pointerEvent, currentOverGo);
}
m_InputPointerEvent = pointerEvent;
}
鼠标释放的时候调用
再往上
protected void ProcessMousePress(MouseButtonEventData data)
{
var pointerEvent = data.buttonData;
var currentOverGo = pointerEvent.pointerCurrentRaycast.gameObject;
// PointerDown notification
if (data.PressedThisFrame())
{
pointerEvent.eligibleForClick = true;
pointerEvent.delta = Vector2.zero;
pointerEvent.dragging = false;
pointerEvent.useDragThreshold = true;
pointerEvent.pressPosition = pointerEvent.position;
pointerEvent.pointerPressRaycast = pointerEvent.pointerCurrentRaycast;
DeselectIfSelectionChanged(currentOverGo, pointerEvent);
// search for the control that will receive the press
// if we can't find a press handler set the press
// handler to be what would receive a click.
var newPressed = ExecuteEvents.ExecuteHierarchy(currentOverGo, pointerEvent, ExecuteEvents.pointerDownHandler);
// didnt find a press handler... search for a click handler
if (newPressed == null)
newPressed = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo);
// Debug.Log("Pressed: " + newPressed);
float time = Time.unscaledTime;
if (newPressed == pointerEvent.lastPress)
{
var diffTime = time - pointerEvent.clickTime;
if (diffTime < 0.3f)
++pointerEvent.clickCount;
else
pointerEvent.clickCount = 1;
pointerEvent.clickTime = time;
}
else
{
pointerEvent.clickCount = 1;
}
pointerEvent.pointerPress = newPressed;
pointerEvent.rawPointerPress = currentOverGo;
pointerEvent.clickTime = time;
// Save the drag handler as well
pointerEvent.pointerDrag = ExecuteEvents.GetEventHandler<IDragHandler>(currentOverGo);
if (pointerEvent.pointerDrag != null)
ExecuteEvents.Execute(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.initializePotentialDrag);
m_InputPointerEvent = pointerEvent;
}
// PointerUp notification
if (data.ReleasedThisFrame())
{
ReleaseMouse(pointerEvent, currentOverGo);
}
}
此方法用来计算和处理任何鼠标按钮状态的变化
再往上
protected void ProcessMouseEvent(int id)
{
var mouseData = GetMousePointerEventData(id);
var leftButtonData = mouseData.GetButtonState(PointerEventData.InputButton.Left).eventData;
m_CurrentFocusedGameObject = leftButtonData.buttonData.pointerCurrentRaycast.gameObject;
// Process the first mouse button fully
ProcessMousePress(leftButtonData);
ProcessMove(leftButtonData.buttonData);
ProcessDrag(leftButtonData.buttonData);
// Now process right / middle clicks
ProcessMousePress(mouseData.GetButtonState(PointerEventData.InputButton.Right).eventData);
ProcessDrag(mouseData.GetButtonState(PointerEventData.InputButton.Right).eventData.buttonData);
ProcessMousePress(mouseData.GetButtonState(PointerEventData.InputButton.Middle).eventData);
ProcessDrag(mouseData.GetButtonState(PointerEventData.InputButton.Middle).eventData.buttonData);
if (!Mathf.Approximately(leftButtonData.buttonData.scrollDelta.sqrMagnitude, 0.0f))
{
var scrollHandler = ExecuteEvents.GetEventHandler<IScrollHandler>(leftButtonData.buttonData.pointerCurrentRaycast.gameObject);
ExecuteEvents.ExecuteHierarchy(scrollHandler, leftButtonData.buttonData, ExecuteEvents.scrollHandler);
}
}