Unity上的Onclickevent

Unity上的Onclickevent

本文介绍了textObject Unity上的Onclickevent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试在Unity中的文本元素上添加OnclickEvent. (UnityEngine.UI.Text)但是Text对象没有onclick事件处理程序.我很确定这是可能的,否则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

但是我只是无法成像,因此无法单击文本.出于布局原因,我真的不想使用按钮.

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.

谢谢

推荐答案

您可以简单地使用 IPointerClickHandler 接口和 UnityEvent :

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();
    }
}

或者,您可以使用 EventTrigger 组件.

Alternatively you could use the EventTrigger component.

基本上两者都会做相同的事情:

both will basically do more or less the same thing:

第一个的巨大优势是您可以轻松地对其进行增强,例如,通过视觉反馈(如更改按钮的颜色):

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
}

这篇关于textObject Unity上的Onclickevent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:47