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

news/2024/10/17 23:24:38/

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.ppmy.cn/news/1132037.html

相关文章

前端JavaScript入门到精通,javascript核心进阶ES6语法、API、js高级等基础知识和实战 —— Web APIs(三)

思维导图 全选案例 大按钮控制小按钮 小按钮控制大按钮 css伪类选择器checked <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><…

【C++】vector的介绍 | 常见接口的使用

目录 vector的介绍 常见接口 构造函数 尾插push_back() vector的遍历 1.用方括号下标 遍历&#xff1a; 2.调用at()来访问&#xff1a; 3.用迭代器遍历&#xff1a; 4.范围for遍历&#xff1a; vector的空间修改 vector增删查改 覆盖assign() 查找find() 插入ins…

算法练习8——有序三元组中的最大值

LeetCode 100088 有序三元组中的最大值 I LeetCode 100086 有序三元组中的最大值 II 给你一个下标从 0 开始的整数数组 nums 。 请你从所有满足 i < j < k 的下标三元组 (i, j, k) 中&#xff0c;找出并返回下标三元组的最大值。如果所有满足条件的三元组的值都是负数&am…

在Qt中,怎么获取到在mainwindow.ui文件中添加的控件

2023年9月30日&#xff0c;周六晚上 假设我在mainwindow.ui中添加了一个名为textEdit的QTextEdit对象 在mainwindow.cpp中&#xff0c;可以通过ui对象来获取到这个控件

《Jetpack Compose从入门到实战》 第二章 了解常用UI组件

目录 常用的基础组件文字组件图片组件按钮组件选择器组件对话框组件进度条组件 常用的布局组件布局Scaffold脚手架 列表 书附代码 Google的图标库 常用的基础组件 文字组件 Composable fun TestText() {Column(modifier Modifier.verticalScroll(state rememberScrollState…

【面试经典150 | 矩阵】有效的数独

文章目录 写在前面Tag题目来源题目解读解题思路方法一&#xff1a;一次遍历数组 写在最后 写在前面 本专栏专注于分析与讲解【面试经典150】算法&#xff0c;两到三天更新一篇文章&#xff0c;欢迎催更…… 专栏内容以分析题目为主&#xff0c;并附带一些对于本题涉及到的数据结…

Java中那么多排序方法该怎么选择呢

在 Java 中&#xff0c;有几种常用的排序方法&#xff0c;比如 Arrays.sort 、Collections.sort 和集合自身的 sort 方法。本文将对这三种排序方法的用法、区别和应用场景进行总结。 Arrays.sort Arrays.sort 方法是 Java 中用于对数组进行排序的方法。它可以处理基本类型和对…

Visual Studio Code键盘快捷键大全

Visual Studio Code键盘快捷键大全 前言导航快捷键编辑快捷键多光标快捷键终端快捷键调试快捷键文件管理快捷键Git快捷键代码格式化快捷键代码折叠快捷键工作区快捷键Markdown快捷键Zen模式快捷键窗口管理快捷键重构快捷键IntelliSense快捷键测试快捷键扩展快捷键 前言 欢迎来…