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


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

    通过脚本修改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 materials
        private 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;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    在这里插入图片描述

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

  • 相关阅读:
    光模块故障的主要原因及解决办法
    分库分表订单全局ID
    LSTM -长短期记忆网络(RNN循环神经网络)
    《机器学习》李宏毅(21P5-9)
    基于 Delphi 的前后端分离:之二
    ES5+ 和 ES6
    网工内推 | 国企、上市公司售前,CISP/CISSP认证,最高18K*14薪
    6、数据结构
    《C语言程序设计 第4版》笔记和代码 第十一章 指针和数组
    Go语言常用命令详解(三)
  • 原文地址:https://blog.csdn.net/thinbug/article/details/133345518