简体   繁体   English

Raycast 未检测到自定义 Unity UI 按钮脚本

[英]Raycast not detecting custom Unity UI button script

I have written a custom class to create buttons which can have an animator.我编写了一个自定义 class 来创建可以有动画师的按钮。 But objects of this class are not detectable by raycast but normal Unity UI buttons are detectable by raycast.但是这个 class 的对象不能被光线投射检测到,但普通的 Unity UI 按钮可以被光线投射检测到。 I'm looking for a solution which can be solved through code.我正在寻找可以通过代码解决的解决方案。

public class AnimatedButton : UIBehaviour, IPointerClickHandler
{
    [Serializable]
    private class ButtonClickedEvent : UnityEvent
    {
    }

    public bool Interactable = true;

    [SerializeField]
    private ButtonClickedEvent onClick = new ButtonClickedEvent();

    private Animator animator;

    private bool blockInput;

    protected override void Start()
    {
        base.Start();
        animator = GetComponent<Animator>();
    }

    public virtual void OnPointerClick(PointerEventData eventData)
    {
        if (!Interactable || eventData.button != PointerEventData.InputButton.Left)
            return;

        if (!blockInput)
        {
            blockInput = true;
            Press();
            // Block the input for a short while to prevent spamming.
            StartCoroutine(BlockInputTemporarily());
        }
    }

    public void Press()
    {
        if (!IsActive())
            return;

        animator.SetTrigger("Pressed");
        StartCoroutine(InvokeOnClickAction());
    }

    private IEnumerator InvokeOnClickAction()
    {
        yield return new WaitForSeconds(0.1f);
        onClick.Invoke();
    }

    private IEnumerator BlockInputTemporarily()
    {
        yield return new WaitForSeconds(0.5f);
        blockInput = false;
    }
}

below code is used to find the gameobject by firing a raycast下面的代码用于通过发射光线投射来找到游戏对象

private bool checkButtonClick()
{
    bool flag = false;
    PointerEventData pointer = new PointerEventData(EventSystem.current);
    List<RaycastResult> raycastResult = new List<RaycastResult>();
    
    pointer.position = Input.mousePosition;
    EventSystem.current.RaycastAll(pointer, raycastResult);
    foreach (RaycastResult result in raycastResult)
    {
        if (result.gameObject.GetComponent<Button>() != null || result.gameObject.GetComponent<AnimatedButton>() != null)
        {
            Debug.Log("Button Name : " + result.gameObject.name);
        }
    }
    raycastResult.Clear();
    return flag;
}

Only objects of type "Button" are printed with this log and objects of type "AnimatedButton" are not detected.仅使用此日志打印“Button”类型的对象,并且未检测到“AnimatedButton”类型的对象。 What could be the issue here and how to solve it?这里可能是什么问题以及如何解决?

You're close.你很近。 But it's not UIBehaviour , it's actually UnityEngine.UI.Graphic , derived from UIBehaviour , that is searched for by the GraphicRaycaster .但它不是UIBehaviour ,它实际上是UnityEngine.UI.Graphic ,派生自UIBehaviour ,由GraphicRaycaster搜索。 So you should be able to derive from Graphic , and your new button type will be found in the raycast results.因此,您应该能够从Graphic派生,并且您的新按钮类型将在光线投射结果中找到。 Here's the Unity docs relating to the Graphic class.这是与Graphic class 相关的 Unity 文档 Quoting that page:引用该页面:

Base class for all visual UI Component.所有可视 UI 组件的基础 class。

When creating visual UI components you should inherit from this class.创建可视化 UI 组件时,您应该继承此 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM