文本对象 Unity 上的 Onclickevent [英] Onclickevent on a textObject Unity

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

问题描述

因此,我正在尝试在 Unity 中的文本元素上添加 OnclickEvent.(UnityEngine.UI.Text)但是 Text 对象没有 onclick 事件处理程序.我很确定这是可能的,或者 Unity 应该是第一种无法单击文本对象的语言.我已经找到了

<块引用>

你不能,至少不能直接.使用 OnGUI 绘制的 GUIText 不检测输入,您必须使用 GUI 按钮.但是您根本不应该使用 OnGUI.几个月前发布了新的 Unity UI 系统,它非常出色.您应该更新到 Unity 4.6.3 并开始使用它.

<小时>

或者,您可以使用

<小时>

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

<小时>

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

[RequireComponent(typeof(Text))]公共类 TextButton : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler{#region 检查器公共颜色 NormalColor = Color.black;公共颜色 HoverColor = Color.black;公共颜色 PressColor = Color.black;public Color DisabledColor = Color.gray;//在检查器中添加回调,如按钮公共 UnityEvent onClick;#endregion 检查员私人布尔_isInteractive = true;公共布尔互动{得到{返回 _isInteractive;}放{_isInteractive = 值;更新颜色();}}私人布尔_isPressed;私人布尔_isHover;私人文本_textComponent;私有文本文本组件{得到{if(!_textComponent) _textComponent = GetComponent() ??gameObject.AddComponent();}}私有无效 Updatecolor(){如果(!交互式){TextComponent.color = DisabledColor;返回;}如果(被按下){TextComponent.color = PressColor;返回;}如果(是悬停){TextComponent.color = HoverColor;返回;}TextComponent.color = NormalColor;}#region IPointer 回调public void OnPointerClick(PointerEventData pointerEventData){//输出到控制台单击的游戏对象的名称和以下消息.单击游戏对象时,您可以将其替换为您自己的操作.Debug.Log(name + " Game Object Clicked!", this);//调用你的事件onClick.Invoke();}public void OnPointerDown(PointerEventData eventData){如果(!_isHover)返回;_isPressed = 真;更新颜色();}public void OnPointerUp(PointerEventData eventData){如果(!_isHover)返回;_isPressed = 假;更新颜色();}public void OnPointerEnter(PointerEventData eventData){_isHover = 真;更新颜色();}public void OnPointerExit(PointerEventData eventData){_isHover = 假;_isPressed = 假;更新颜色();}#endregion IPointer 回调}

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

解决方案

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.


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
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆