C# Raycast未检测到自定义Unity UI按钮脚本
我已经编写了一个自定义类来创建可以有动画的按钮。但是此类对象不能被raycast检测到,但是普通的Unity UI按钮可以被raycast检测到。我正在寻找一个可以通过代码解决的解决方案C# Raycast未检测到自定义Unity UI按钮脚本,c#,unity3d,raycasting,C#,Unity3d,Raycasting,我已经编写了一个自定义类来创建可以有动画的按钮。但是此类对象不能被raycast检测到,但是普通的Unity UI按钮可以被raycast检测到。我正在寻找一个可以通过代码解决的解决方案 public class AnimatedButton : UIBehaviour, IPointerClickHandler { [Serializable] private class ButtonClickedEvent : UnityEvent { } publ
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;
}
}
public类AnimatedButton:UIBehavior,IPoClickHandler
{
[可序列化]
私有类按钮ClickedEvent:UnityEvent
{
}
公共bool可交互=真;
[序列化字段]
private ButtonClickedEvent onClick=新建ButtonClickedEvent();
私人动画师;
私有布尔块输入;
受保护的覆盖无效开始()
{
base.Start();
animator=GetComponent();
}
公共虚拟void OnPointerClick(PointerEventData事件数据)
{
如果(!interactiable | | eventData.button!=PointerEventData.InputButton.Left)
返回;
如果(!blockInput)
{
blockInput=true;
按();
//暂时阻止输入以防止垃圾邮件。
start例程(blockInputTemporaly());
}
}
新闻界
{
如果(!IsActive())
返回;
设置触发器(“按下”);
Start例程(调用OnClickAction());
}
私有IEnumerator调用OnClickAction()
{
收益率返回新WaitForSeconds(0.1f);
onClick.Invoke();
}
私有IEnumerator BlockInputTemporaly()
{
收益率返回新的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;
}
private bool checkbutton单击()
{
布尔标志=假;
PointerEventData指针=新的PointerEventData(EventSystem.current);
List raycastResult=新列表();
pointer.position=Input.mousePosition;
EventSystem.current.RaycastAll(指针,raycastResult);
foreach(RaycastResult结果为RaycastResult)
{
if(result.gameObject.GetComponent()!=null | | result.gameObject.GetComponent()!=null)
{
Log(“按钮名:+result.gameObject.Name”);
}
}
raycastResult.Clear();
返回标志;
}
此日志仅打印“Button”类型的对象,未检测到“AnimatedButton”类型的对象。这里可能存在什么问题以及如何解决它?你很接近了。但它不是
uibehavior
,而是GraphicRaycaster
搜索的UnityEngine.UI.Graphic
,它源自uibehavior
。因此,您应该能够从图形
派生,并且您的新按钮类型将在光线投射结果中找到。下面是与图形类相关的统一性。引用该页:
所有可视UI组件的基类
当创建可视化UI组件时,您应该从该类继承
谢谢,你说得对。但当我尝试从图形继承时,我面临的问题是画布渲染器未连接到对象时会出现异常。如果我尝试在运行时添加画布渲染器,在某些情况下,我会得到一个类似于白色边框的按钮。那么还有其他办法吗?@Zenhal我不太清楚为什么你会在一些物品周围有白色边框。这就是你的下一期吗?对不起,我不太清楚当你问“还有别的办法吗”时你在问什么。我们还可以看看为什么要从这样一个低级类创建一个新项AniamtedButton。您是否可以扩展按钮
,或者甚至扩展UnityEngine.UI.selective
?