【发布时间】:2021-03-21 13:04:09
【问题描述】:
我编写了一个自定义类来创建可以有动画师的按钮。但是这个类的对象不能被光线投射检测到,但是普通的 Unity UI 按钮可以被光线投射检测到。我正在寻找可以通过代码解决的解决方案。
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;
}
}
以下代码用于通过发射光线投射找到游戏对象
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;
}
仅使用此日志打印“按钮”类型的对象,并且未检测到“动画按钮”类型的对象。这可能是什么问题以及如何解决?
【问题讨论】:
标签: c# unity3d raycasting