【问题标题】:Onclickevent on a textObject Unity文本对象 Unity 上的 Onclick 事件
【发布时间】:2019-08-04 02:39:49
【问题描述】:

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

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

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

谢谢

【问题讨论】:

  • derHugo 下面给出了一个很好的答案,我只会补充一点,您阅读的文档是针对古代统一版本的

标签: c# unity3d


【解决方案1】:

您可以使用IPointerClickHandler 接口和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();
    }
}

确保场景中存在EventSystem 以允许点击检测。对于非 UI 游戏对象的点击检测,确保 PhysicsRaycaster 附加到 Camera


您也可以使用EventTrigger 组件。


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


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

[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
}

【讨论】:

  • 这两个答案很有用,谢谢!现在唯一的问题是我不想更改文本,但我想在单击文本时切换场景。可以这样做吗?
  • 只为你(和其他人发现这个;))我还添加了其他 IPointer 接口的实现,以便获得视觉反馈
  • 你,我的朋友,是 StackOverflow 的真正英雄!关于自己编写的事件处理程序的文档非常糟糕,这对我帮助很大。
猜你喜欢
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-05
相关资源
最近更新 更多