Back ported fixes from 5.2 work

Also refactored and renamed the old UI Button control to a more generic Selectable extension
release
Simon (darkside) Jackson 2015-09-10 22:54:29 +01:00
parent a6b6d4793b
commit 0662788cf0
7 changed files with 117 additions and 66 deletions

View File

@ -1,10 +1,6 @@
/// Credit Chris Trueman
/// Sourced from - http://forum.unity3d.com/threads/use-reticle-like-mouse-for-worldspace-uis.295271/
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections.Generic;
namespace UnityEngine.EventSystems.Extensions
{
[RequireComponent(typeof(EventSystem))]

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,58 +0,0 @@
/// Credit AriathTheWise
/// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1796783
using UnityEngine.Events;
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions
{
/// <summary>
/// UIButton
/// </summary>
[AddComponentMenu("UI/Extensions/UI Button")]
public class UIButton : Button, IPointerDownHandler, IPointerUpHandler
{
#region Sub-Classes
[System.Serializable]
public class UIButtonEvent : UnityEvent<PointerEventData.InputButton> { }
#endregion
#region Events
public UIButtonEvent OnButtonClick;
public UIButtonEvent OnButtonPress;
public UIButtonEvent OnButtonRelease;
#endregion
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);
if (OnButtonPress != null)
{
OnButtonPress.Invoke(eventData.button);
}
}
void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
{
DoStateTransition(SelectionState.Normal, false);
if (OnButtonRelease != null)
{
OnButtonRelease.Invoke(eventData.button);
}
}
}
}

View File

@ -6,7 +6,7 @@ using System.Collections.Generic;
namespace UnityEngine.UI.Extensions
{
[AddComponentMenu("UI/Extensions/UILineRenderer")]
public class UILineRenderer : Graphic
public class UILineRenderer : MaskableGraphic
{
[SerializeField]
Texture m_Texture;

View File

@ -7,7 +7,7 @@ using System.Collections.Generic;
namespace UnityEngine.UI.Extensions
{
[AddComponentMenu("UI/Extensions/UILineTextureRenderer")]
public class UILineTextureRenderer : Graphic
public class UILineTextureRenderer : MaskableGraphic
{
[SerializeField]
Texture m_Texture;

View File

@ -0,0 +1,112 @@
/// 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;
namespace UnityEngine.UI.Extensions
{
/// <summary>
/// UIButton
/// </summary>
[AddComponentMenu("UI/Extensions/UI Selectable Extension")]
[RequireComponent(typeof(Selectable))]
public class UISelectableExtension : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
#region Sub-Classes
[System.Serializable]
public class UIButtonEvent : UnityEvent<PointerEventData.InputButton> { }
#endregion
#region Events
[Tooltip("Event that fires when a button is initially pressed down")]
public UIButtonEvent OnButtonPress;
[Tooltip("Event that fires when a button is released")]
public UIButtonEvent OnButtonRelease;
[Tooltip("Event that continually fires while a button is held down")]
public UIButtonEvent OnButtonHeld;
#endregion
private bool _pressed;
private PointerEventData _heldEventData;
void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
{
//Can't set the state as it's too locked down.
//DoStateTransition(SelectionState.Pressed, false);
if (OnButtonPress != null)
{
OnButtonPress.Invoke(eventData.button);
}
_pressed = true;
_heldEventData = eventData;
}
void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
{
//DoStateTransition(SelectionState.Normal, false);
if (OnButtonRelease != null)
{
OnButtonRelease.Invoke(eventData.button);
}
_pressed = false;
_heldEventData = null;
}
void Update()
{
if (!_pressed)
return;
if (OnButtonHeld != null)
{
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,5 +1,5 @@
fileFormatVersion: 2
guid: f2e459a0f758bc947ace4872e13f1da0
guid: 6bb501adc62c7394fae41ae7a6032eb3
MonoImporter:
serializedVersion: 2
defaultReferences: []