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

41 lines
1.1 KiB
C#
Raw Normal View History

2020-12-24 15:57:25 +00:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace QSB.Utility
2020-12-24 15:57:25 +00:00
{
public class ZOverride : MonoBehaviour
{
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();
protected virtual void Start()
2020-12-24 16:05:54 +00:00
{
uiElementsToApplyTo = gameObject.GetComponentsInChildren<Graphic>();
foreach (var graphic in uiElementsToApplyTo)
2020-12-24 16:05:54 +00:00
{
var material = graphic.materialForRendering;
if (material == null)
{
continue;
}
2021-06-18 21:38:32 +00:00
Material materialCopy;
if (!materialMappings.ContainsKey(material))
{
materialCopy = new Material(material);
materialMappings.Add(material, materialCopy);
}
else
{
materialCopy = materialMappings[material];
}
materialCopy.SetInt(shaderTestMode, (int)desiredUIComparison);
graphic.material = materialCopy;
}
2020-12-24 16:05:54 +00:00
}
}
}