UnityXR-跳转到其他场景中射线无法交互

使用单XR Rig的时候,跳转到其他场景中射线无法交互,则需要通过代码设置Canvas上的Event Camera,另外进入新的场景后需要重新初始化XR RayInteractor,这里只需要做一下隐藏和显示就可以了。具体示例如下,将该脚本挂载到UI Canvas上。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;

public class CanvasController : MonoBehaviour
{
    Canvas canvas;
    private void Awake()
    {
        canvas = GetComponent<Canvas>();
        canvas.renderMode = RenderMode.WorldSpace;
        canvas.worldCamera = Camera.main;//更新主相机
    }
    // Start is called before the first frame update
    IEnumerator Start()
    {
        XRRayInteractor[] xRRayInteractor = FindObjectsOfType<XRRayInteractor>();
        foreach (XRRayInteractor xrRay in xRRayInteractor)
        {
            xrRay.gameObject.SetActive(false);
            yield return new  WaitForEndOfFrame();
            xrRay.gameObject.SetActive(true);
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

 

作者:Miracle
来源:麦瑞克博客
链接:https://www.playcreator.cn/archives/unity/1485/
本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0许可协议,转载请注明!
THE END
分享
海报
UnityXR-跳转到其他场景中射线无法交互
使用单XR Rig的时候,跳转到其他场景中射线无法交互,则需要通过代码设置Canvas上的Event Camera,另外进入新的场景后需要重新初始化XR RayInteractor,这里只……
<<上一篇
下一篇>>
文章目录
关闭
目 录