You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity VR开发报错CS0103:当前上下文中不存在'OnSelectEnter'求助

错误原因

你的项目使用的XR Interaction Toolkit(XRIT)版本与参考教程使用的版本不匹配:

  • 教程使用的是XRIT 1.x版本,XRGrabInteractable类提供了受保护的OnSelectEnter(XRBaseInteractor)、OnSelectExit(XRBaseInteractor)方法
  • 你当前使用的是XRIT 2.x及以上版本,上述方法已被重构移除,且本身为非公开成员,无法在子类的公有方法中直接调用,因此抛出CS0103报错。

解决方案

方案1:适配新版XRIT(推荐)

修改代码适配你当前的XRIT版本,改动点如下:

  1. 替换选择事件的绑定、回调签名:
protected override void Awake()
{
    base.Awake();
    SetupHolds();
    // 替换为2.x版本的选择进入事件
    onSelectEntered.AddListener(SetInitialRotation);
}

private void OnDestroy()
{
    onSelectEntered.RemoveListener(SetInitialRotation);
}

// 修改回调参数为2.x版本的事件参数
private void SetInitialRotation(SelectEnterEventArgs args)
{
    XRBaseInteractor interactor = args.interactorObject as XRBaseInteractor;
    Quaternion newRotation = Quaternion.Euler(gripRotation);
    interactor.attachTransform.localRotation = newRotation;
}
  1. 替换手动触发交互的调用,不要直接调用不存在的OnSelectEnter/OnSelectExit,使用交互管理器的标准方法触发交互:
public void SetGripHand(XRBaseInteractor interactor)
{
    gripHand = interactor;
    // 标准手动触发选择逻辑
    interactionManager.SelectEnter(interactor, this);
}

public void ClearGripHand(XRBaseInteractor interactor)
{
    gripHand = null;
    // 标准手动触发取消选择逻辑
    interactionManager.SelectExit(interactor, this);
}

方案2:降级XRIT版本匹配教程

如果你希望完全沿用教程的代码逻辑,打开Unity Package Manager,找到XR Interaction Toolkit,将版本降级到1.x系列即可解决报错。

内容的提问来源于stack exchange,提问作者Örs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 16:06:05