Pulled forward new UI Selectable extension to replace the old UI button script, offers a more extensible solution

--HG--
branch : develop_5.2
release
Simon (darkside) Jackson 2015-09-10 22:58:09 +01:00
parent 4e08512648
commit 062d717930
3 changed files with 54 additions and 20 deletions

View File

@ -362,7 +362,8 @@ namespace UnityEditor.UI
image.type = Image.Type.Sliced;
image.color = s_DefaultSelectableColor;
UIButton bt = uiButtonRoot.AddComponent<UIButton>();
Button bt = uiButtonRoot.AddComponent<Button>();
uiButtonRoot.AddComponent<UISelectableExtension>();
SetDefaultColorTransitionValues(bt);
Text text = childText.AddComponent<Text>();

View File

@ -1,6 +1,7 @@
/// Credit AriathTheWise
/// 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.
/// Refactored so it can be added to any button and expose the events in the editor.
using UnityEngine.Events;
using UnityEngine.EventSystems;
@ -10,8 +11,9 @@ namespace UnityEngine.UI.Extensions
/// <summary>
/// UIButton
/// </summary>
[AddComponentMenu("UI/Extensions/UI Button")]
public class UIButton : Button, IPointerDownHandler, IPointerUpHandler
[AddComponentMenu("UI/Extensions/UI Selectable Extension")]
[RequireComponent(typeof(Selectable))]
public class UISelectableExtension : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
#region Sub-Classes
[System.Serializable]
@ -19,8 +21,6 @@ namespace UnityEngine.UI.Extensions
#endregion
#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")]
public UIButtonEvent OnButtonPress;
[Tooltip("Event that fires when a button is released")]
@ -32,20 +32,10 @@ namespace UnityEngine.UI.Extensions
private bool _pressed;
private PointerEventData _heldEventData;
public override void OnPointerClick(PointerEventData eventData)
{
base.OnSubmit(eventData);
if (OnButtonClick != null)
{
OnButtonClick.Invoke(eventData.button);
}
}
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)
{
@ -58,7 +48,7 @@ namespace UnityEngine.UI.Extensions
void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
{
DoStateTransition(SelectionState.Normal, false);
//DoStateTransition(SelectionState.Normal, false);
if (OnButtonRelease != null)
{
@ -77,7 +67,46 @@ namespace UnityEngine.UI.Extensions
{
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
}
}
}

View File

@ -1,8 +1,12 @@
fileFormatVersion: 2
guid: f2e459a0f758bc947ace4872e13f1da0
guid: 2bdfd6b4084989d41ba560bd1b72cbda
timeCreated: 1441922219
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: