当前位置: 首页 > news >正文

Unity把UGUI再World模式下显示到相机最前方

Unity把UGUI再World模式下显示到相机最前方

通过脚本修改Shader

再VR里有时候要把3D的UI显示到相机最前方,加个UI相机会坏事,可以通过修改unity_GUIZTestMode来解决。

测试用例

测试用例如下:
在这里插入图片描述
场景包含一个红色的盒子,一个UI里含有这些元素
在这里插入图片描述

在这里插入图片描述
我们在UI根挂上运行脚本WorldSpaceOverlayUI.cs

脚本如下:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;[ExecuteInEditMode] //Disable if you don't care about previewing outside of play mode
public class WorldSpaceOverlayUI : MonoBehaviour
{private const string shaderTestMode = "unity_GUIZTestMode"; //The magic property we need to set[SerializeField] UnityEngine.Rendering.CompareFunction desiredUIComparison = UnityEngine.Rendering.CompareFunction.Always; //If you want to try out other effects[Tooltip("Set to blank to automatically populate from the child UI elements")][SerializeField] Graphic[] uiGraphicsToApplyTo;[Tooltip("Set to blank to automatically populate from the child UI elements")][SerializeField] TextMeshProUGUI[] uiTextsToApplyTo;//Allows us to reuse materialsprivate Dictionary<Material, Material> materialMappings = new Dictionary<Material, Material>();protected virtual void Start(){if (uiGraphicsToApplyTo.Length == 0){uiGraphicsToApplyTo = gameObject.GetComponentsInChildren<Graphic>();}if (uiTextsToApplyTo.Length == 0){uiTextsToApplyTo = gameObject.GetComponentsInChildren<TextMeshProUGUI>();}foreach (var graphic in uiGraphicsToApplyTo){Material material = graphic.materialForRendering;if (material == null){Debug.LogError($"{nameof(WorldSpaceOverlayUI)}: skipping target without material {graphic.name}.{graphic.GetType().Name}");continue;}if (!materialMappings.TryGetValue(material, out Material materialCopy)){materialCopy = new Material(material);materialMappings.Add(material, materialCopy);}materialCopy.SetInt(shaderTestMode, (int)desiredUIComparison);graphic.material = materialCopy;}foreach (var text in uiTextsToApplyTo){Material material = text.fontMaterial;if (material == null){Debug.LogError($"{nameof(WorldSpaceOverlayUI)}: skipping target without material {text.name}.{text.GetType().Name}");continue;}if (!materialMappings.TryGetValue(material, out Material materialCopy)){materialCopy = new Material(material);materialMappings.Add(material, materialCopy);}materialCopy.SetInt(shaderTestMode, (int)desiredUIComparison);text.fontMaterial = materialCopy;}}
}

在这里插入图片描述

引用
https://discussions.unity.com/t/world-space-canvas-on-top-of-everything/128165/14

http://www.lryc.cn/news/179788.html

相关文章:

  • nodejs+vue活鲜物流监控系统elementui
  • 数据集划分——train_test_split函数使用说明
  • Pytorch中关于forward函数的理解与用法
  • vite跨域proxy设置与开发、生产环境的接口配置,接口在生产环境下,还能使用proxy代理地址吗
  • 【嵌入式】使用MultiButton开源库驱动按键并控制多级界面切换
  • 【数据结构】树的概念理解和性质推导(保姆级详解,小白必看系列)
  • 融合之力:数字孪生、人工智能和数据分析的创新驱动
  • Spring的注解开发-Spring配置类的开发
  • Linux系统编程系列之进程间通信-信号量组
  • centos 6使用yum安装软件
  • maven无法下载时的解决方法——笔记
  • Java Spring Boot 开发框架
  • Pytorch学习记录-1-张量
  • paddle2.3-基于联邦学习实现FedAVg算法-CNN
  • nuiapp保存canvas绘图
  • Object.defineProperty()方法详解,了解vue2的数据代理
  • Linux 磁盘管理
  • 大数据与人工智能的未来已来
  • 【AI视野·今日Robot 机器人论文速览 第四十一期】Tue, 26 Sep 2023
  • [NOIP2012 提高组] 开车旅行
  • 数据库设计流程---以案例熟悉
  • Miniconda创建paddlepaddle环境
  • postgresql实现单主单从
  • 提取PDF数据:Documents for PDF ( GcPdf )
  • adb连接切换到模拟器端口
  • 为何每个开发者都在谈论Go?
  • 【Leetcode】 501. 二叉搜索树中的众数
  • 怎样给Ubuntu系统安装vmware-tools
  • DDS信号发生器波形发生器VHDL
  • Python3操作SQLite3创建表主键自增长|CRUD基本操作