Anthony Heremans
Anthony Heremans

Reputation: 70

Onclickevent on a textObject Unity

So, i'm trying to add an OnclickEvent on a Text Element in Unity. (UnityEngine.UI.Text) But the Text object doesn't have a onclick event handler. I'm pretty sure this is possible, or Unity should be the first language where it's not possible to click a Text Object. I already found

You can't, at least not directly. The GUIText drawn with OnGUI doesn't detect input, you would have to use a GUI button. But you should not be using OnGUI at all. The new Unity UI system was released a few months ago, it's vastly superior. You should update to Unity 4.6.3 and start using that instead. Not Possible to add onclick event

But i just can't image it's not possible to click on text. I really don't want to use a button for Layout reasons.

Thanks

Upvotes: 2

Views: 10767

Answers (1)

derHugo
derHugo

Reputation: 90620

You can simply create your own click handler using the IPointerClickHandler interface and a UnityEvent:

public class TextButton : MonoBehaviour, IPointerClickHandler
{
    // add callbacks in the inspector like for buttons
    public UnityEvent onClick;

    public void OnPointerClick(PointerEventData pointerEventData)
    {
        //Output to console the clicked GameObject's name and the following message. You can replace this with your own actions for when clicking the GameObject.
        Debug.Log(name + " Game Object Clicked!", this);

        // invoke your event
        onClick.Invoke();
    }
}

Ensure an EventSystem exists in the Scene to allow click detection. For click detection on non-UI GameObjects, ensure a PhysicsRaycaster is attached to the Camera.

enter image description here


Alternatively you could use the EventTrigger component.

enter image description here


both will basically do more or less the same thing:

enter image description here


The huge advantage of the first one is you could easily enhance it, for example with visual feedback like changing the color of a button:

[RequireComponent(typeof(Text))]
public class TextButton : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
{
    #region Inspector

    public Color NormalColor = Color.black;
    public Color HoverColor = Color.black;
    public Color PressColor = Color.black;
    public Color DisabledColor = Color.gray;

    // add callbacks in the inspector like for buttons
    public UnityEvent onClick;

    #endregion Inspector

    private bool _isInteractive = true;
    public bool interactive
    {
        get
        { 
            return _isInteractive; 
        }
        set
        {
            _isInteractive = value;
            UpdateColor();
        }
    }

    private bool _isPressed;
    private bool _isHover;

    private Text _textComponent;
    private Text TextComponent
    {
        get
        {
            if(!_textComponent) _textComponent = GetComponent<Text>() ?? gameObject.AddComponent<Text>();

        }
    }

    private void Updatecolor()
    {
        if (!interactive)
        {
            TextComponent.color = DisabledColor;
            return;
        }

        if (isPressed)
        {
            TextComponent.color = PressColor;
            return;
        }

        if (isHover)
        {
            TextComponent.color = HoverColor;
            return;
        }

        TextComponent.color = NormalColor;
    }

    #region IPointer Callbacks

    public void OnPointerClick(PointerEventData pointerEventData)
    {
        //Output to console the clicked GameObject's name and the following message. You can replace this with your own actions for when clicking the GameObject.
        Debug.Log(name + " Game Object Clicked!", this);

        // invoke your event
        onClick.Invoke();
    }

    public void OnPointerDown(PointerEventData eventData)
    {
       if(!_isHover)return;
        _isPressed = true;
        Updatecolor();
    }

    public void OnPointerUp(PointerEventData eventData)
    {
      if(!_isHover)return;
        _isPressed = false;
        Updatecolor();
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        _isHover = true;
        Updatecolor();
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        _isHover = false;
        _isPressed = false;
        Updatecolor();
    }

    #endregion IPointer Callbacks
}

enter image description here

Upvotes: 17

Related Questions