Pulled forward new UI Selectable extension to replace the old UI button script, offers a more extensible solution
--HG-- branch : develop_5.2pull/413/head
parent
4e08512648
commit
062d717930
|
@ -362,7 +362,8 @@ namespace UnityEditor.UI
|
||||||
image.type = Image.Type.Sliced;
|
image.type = Image.Type.Sliced;
|
||||||
image.color = s_DefaultSelectableColor;
|
image.color = s_DefaultSelectableColor;
|
||||||
|
|
||||||
UIButton bt = uiButtonRoot.AddComponent<UIButton>();
|
Button bt = uiButtonRoot.AddComponent<Button>();
|
||||||
|
uiButtonRoot.AddComponent<UISelectableExtension>();
|
||||||
SetDefaultColorTransitionValues(bt);
|
SetDefaultColorTransitionValues(bt);
|
||||||
|
|
||||||
Text text = childText.AddComponent<Text>();
|
Text text = childText.AddComponent<Text>();
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
/// Credit AriathTheWise
|
/// Credit AriathTheWise
|
||||||
/// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1796783
|
/// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1796783
|
||||||
/// Extended to include a HELD state that continually fires while the button is held down.
|
/// Extended to include a HELD state that continually fires while the button is held down.
|
||||||
|
/// Refactored so it can be added to any button and expose the events in the editor.
|
||||||
|
|
||||||
using UnityEngine.Events;
|
using UnityEngine.Events;
|
||||||
using UnityEngine.EventSystems;
|
using UnityEngine.EventSystems;
|
||||||
|
@ -10,8 +11,9 @@ namespace UnityEngine.UI.Extensions
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// UIButton
|
/// UIButton
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[AddComponentMenu("UI/Extensions/UI Button")]
|
[AddComponentMenu("UI/Extensions/UI Selectable Extension")]
|
||||||
public class UIButton : Button, IPointerDownHandler, IPointerUpHandler
|
[RequireComponent(typeof(Selectable))]
|
||||||
|
public class UISelectableExtension : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
||||||
{
|
{
|
||||||
#region Sub-Classes
|
#region Sub-Classes
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
|
@ -19,8 +21,6 @@ namespace UnityEngine.UI.Extensions
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Events
|
#region Events
|
||||||
[Tooltip("Event that fires when a button is clicked")]
|
|
||||||
public UIButtonEvent OnButtonClick;
|
|
||||||
[Tooltip("Event that fires when a button is initially pressed down")]
|
[Tooltip("Event that fires when a button is initially pressed down")]
|
||||||
public UIButtonEvent OnButtonPress;
|
public UIButtonEvent OnButtonPress;
|
||||||
[Tooltip("Event that fires when a button is released")]
|
[Tooltip("Event that fires when a button is released")]
|
||||||
|
@ -32,20 +32,10 @@ namespace UnityEngine.UI.Extensions
|
||||||
private bool _pressed;
|
private bool _pressed;
|
||||||
private PointerEventData _heldEventData;
|
private PointerEventData _heldEventData;
|
||||||
|
|
||||||
public override void OnPointerClick(PointerEventData eventData)
|
|
||||||
{
|
|
||||||
base.OnSubmit(eventData);
|
|
||||||
|
|
||||||
if (OnButtonClick != null)
|
|
||||||
{
|
|
||||||
OnButtonClick.Invoke(eventData.button);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
|
void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
|
||||||
{
|
{
|
||||||
DoStateTransition(SelectionState.Pressed, false);
|
//Can't set the state as it's too locked down.
|
||||||
|
//DoStateTransition(SelectionState.Pressed, false);
|
||||||
|
|
||||||
if (OnButtonPress != null)
|
if (OnButtonPress != null)
|
||||||
{
|
{
|
||||||
|
@ -58,7 +48,7 @@ namespace UnityEngine.UI.Extensions
|
||||||
|
|
||||||
void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
|
void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
|
||||||
{
|
{
|
||||||
DoStateTransition(SelectionState.Normal, false);
|
//DoStateTransition(SelectionState.Normal, false);
|
||||||
|
|
||||||
if (OnButtonRelease != null)
|
if (OnButtonRelease != null)
|
||||||
{
|
{
|
||||||
|
@ -76,8 +66,47 @@ namespace UnityEngine.UI.Extensions
|
||||||
if (OnButtonHeld != null)
|
if (OnButtonHeld != null)
|
||||||
{
|
{
|
||||||
OnButtonHeld.Invoke(_heldEventData.button);
|
OnButtonHeld.Invoke(_heldEventData.button);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test method to verify a control has been clicked
|
||||||
|
/// </summary>
|
||||||
|
public void TestClicked()
|
||||||
|
{
|
||||||
|
#if DEBUG || UNITY_EDITOR
|
||||||
|
Debug.Log("Control Clicked");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test method to verify a controll is pressed
|
||||||
|
/// </summary>
|
||||||
|
public void TestPressed()
|
||||||
|
{
|
||||||
|
#if DEBUG || UNITY_EDITOR
|
||||||
|
Debug.Log("Control Pressed");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// est method to verify if a control is released
|
||||||
|
/// </summary>
|
||||||
|
public void TestReleased()
|
||||||
|
{
|
||||||
|
#if DEBUG || UNITY_EDITOR
|
||||||
|
Debug.Log("Control Released");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// est method to verify if a control is being held
|
||||||
|
/// </summary>
|
||||||
|
public void TestHold()
|
||||||
|
{
|
||||||
|
#if DEBUG || UNITY_EDITOR
|
||||||
|
Debug.Log("Control Held");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,8 +1,12 @@
|
||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: f2e459a0f758bc947ace4872e13f1da0
|
guid: 2bdfd6b4084989d41ba560bd1b72cbda
|
||||||
|
timeCreated: 1441922219
|
||||||
|
licenseType: Pro
|
||||||
MonoImporter:
|
MonoImporter:
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
||||||
defaultReferences: []
|
defaultReferences: []
|
||||||
executionOrder: 0
|
executionOrder: 0
|
||||||
icon: {instanceID: 0}
|
icon: {instanceID: 0}
|
||||||
userData:
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
Reference in New Issue