在VR开发中,经常需要把UI渲染在相机的最前方,有2种方式可以实现
1.使用单独的相机渲染UI层,但有时由于VRSDK的限制,该方法无法使用,则需要第二种方法
2.自己编写shader,改变其渲染队列,使其在其他物体渲染之后,在渲染UI,代码如下:
C#代码:
using UnityEngine;
using UnityEngine.UI;
public class UIOverlay : MonoBehaviour
{
void Start()
{
Material material = new Material(Shader.Find("UI/Overlay"));
material.renderQueue = 3010;
MaskableGraphic[] maskableGraphics = transform.GetComponentsInChildren<MaskableGraphic>();
if(maskableGraphics.Length > 0)
{
for (int i =0;i < maskableGraphics.Length;i++)
{
if(maskableGraphics[i].material.shader == Shader.Find("UI/Default") || maskableGraphics[i].material == null)
{
maskableGraphics[i].material = material;