diff options
Diffstat (limited to 'Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/UIControls/Button.cs')
-rw-r--r-- | Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/UIControls/Button.cs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/UIControls/Button.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/UIControls/Button.cs new file mode 100644 index 0000000..d0c9ae3 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/UIControls/Button.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections; +using System.Collections.Generic; +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 // Input>Submit触发,比如手柄、键盘某个按键按下 + { + [Serializable] + public class ButtonClickedEvent : UnityEvent {} + + // Event delegates triggered on click. + [FormerlySerializedAs("onClick")] + [SerializeField] + private ButtonClickedEvent m_OnClick = new ButtonClickedEvent(); + + protected 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(); + } + + // Trigger all registered callbacks. + public virtual void OnPointerClick(PointerEventData eventData) + { + if (eventData.button != PointerEventData.InputButton.Left) + return; + + Press(); + } + + public virtual void OnSubmit(BaseEventData eventData) + { + LogHelper.Log("OnSubmit() " + gameObject.name); + + 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); + } + + } +} |