quantum-space-buddies/QSB/Utility/ZOverride.cs

41 lines
1.0 KiB
C#
Raw Normal View History

2020-12-24 15:57:25 +00:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2022-03-03 03:46:33 +00:00
namespace QSB.Utility;
[UsedInUnityProject]
2022-03-03 03:46:33 +00:00
public class ZOverride : MonoBehaviour
2020-12-24 15:57:25 +00:00
{
2022-03-03 03:46:33 +00:00
private const string shaderTestMode = "unity_GUIZTestMode";
private readonly UnityEngine.Rendering.CompareFunction desiredUIComparison = UnityEngine.Rendering.CompareFunction.Always;
private Graphic[] uiElementsToApplyTo;
private readonly Dictionary<Material, Material> materialMappings = new();
2022-03-03 03:46:33 +00:00
protected virtual void Start()
{
uiElementsToApplyTo = gameObject.GetComponentsInChildren<Graphic>();
foreach (var graphic in uiElementsToApplyTo)
2020-12-24 16:05:54 +00:00
{
2022-03-03 03:46:33 +00:00
var material = graphic.materialForRendering;
if (material == null)
2020-12-24 16:05:54 +00:00
{
2022-03-03 03:46:33 +00:00
continue;
}
2022-03-03 03:46:33 +00:00
Material materialCopy;
if (!materialMappings.ContainsKey(material))
{
materialCopy = new Material(material);
materialMappings.Add(material, materialCopy);
}
else
{
materialCopy = materialMappings[material];
}
2022-03-03 03:46:33 +00:00
materialCopy.SetInt(shaderTestMode, (int)desiredUIComparison);
graphic.material = materialCopy;
2020-12-24 16:05:54 +00:00
}
}
}