A game about forced loneliness, made by TACStudios
at master 68 lines 1.9 kB view raw
1using System; 2using UnityEngine.Serialization; 3 4namespace UnityEngine.Rendering 5{ 6 [ExecuteInEditMode] 7 internal class DisallowGPUDrivenRendering : MonoBehaviour 8 { 9 private bool m_AppliedRecursively; 10 11 [FormerlySerializedAs("applyToChildrenRecursively")] 12 public bool m_applyToChildrenRecursively; 13 14 public bool applyToChildrenRecursively 15 { 16 get => m_applyToChildrenRecursively; 17 set 18 { 19 m_applyToChildrenRecursively = value; 20 OnDisable(); 21 OnEnable(); 22 } 23 } 24 25 private void OnEnable() 26 { 27 m_AppliedRecursively = applyToChildrenRecursively; 28 29 if (applyToChildrenRecursively) 30 AllowGPUDrivenRenderingRecursively(transform, false); 31 else 32 AllowGPUDrivenRendering(transform, false); 33 } 34 35 private void OnDisable() 36 { 37 if (m_AppliedRecursively) 38 AllowGPUDrivenRenderingRecursively(transform, true); 39 else 40 AllowGPUDrivenRendering(transform, true); 41 } 42 43 private static void AllowGPUDrivenRendering(Transform transform, bool allow) 44 { 45 var renderer = transform.GetComponent<MeshRenderer>(); 46 47 if (renderer) 48 renderer.allowGPUDrivenRendering = allow; 49 } 50 51 private static void AllowGPUDrivenRenderingRecursively(Transform transform, bool allow) 52 { 53 AllowGPUDrivenRendering(transform, allow); 54 55 foreach (Transform child in transform) 56 { 57 if (!child.GetComponent<DisallowGPUDrivenRendering>()) 58 AllowGPUDrivenRenderingRecursively(child, allow); 59 } 60 } 61 62 private void OnValidate() 63 { 64 OnDisable(); 65 OnEnable(); 66 } 67 } 68}