using System; using System.Collections; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.Serialization; namespace UnityEngine.UI { /// /// A standard button that sends an event when clicked. /// [AddComponentMenu("UI/Button", 30)] public class Button : Selectable, IPointerClickHandler, ISubmitHandler { [Serializable] /// /// Function definition for a button click event. /// public class ButtonClickedEvent : UnityEvent {} // Event delegates triggered on click. [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. /// /// /// /// (); /// btn.onClick.AddListener(TaskOnClick); /// } /// /// void TaskOnClick() /// { /// Debug.Log("You have clicked the button!"); /// } /// } /// ]]> /// /// 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(); } /// /// 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. /// /// /// /// /// 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); } } }