Advertisement
DugganSC

Untitled

Sep 24th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6.  
  7. public class Ghostable : MonoBehaviour
  8. {
  9. [SerializeField] Dictionary<Material, Material> _regToGhostMaterialMap = new Dictionary<Material, Material>();
  10. [SerializeField] Dictionary<Material, Material> _ghostToRegMaterialMap = new Dictionary<Material, Material>();
  11.  
  12. static Shader shaderURP_Lit;
  13. static Shader shaderURP_Unlit;
  14.  
  15. private void Start()
  16. {
  17. if (shaderURP_Lit == null) {
  18. shaderURP_Lit = Shader.Find("Universal Render Pipeline/Lit");
  19. }
  20.  
  21. if (shaderURP_Unlit == null)
  22. {
  23. shaderURP_Unlit = Shader.Find("Universal Render Pipeline/Unlit");
  24. }
  25.  
  26. Debug.Log("Start");
  27. // Get references to materials
  28. GetMaterialReferences(this.gameObject);
  29. foreach (Material key in _regToGhostMaterialMap.Keys)
  30. {
  31. Debug.Log($"Key: {key.name} Value: {_regToGhostMaterialMap[key].name}");
  32. }
  33. }
  34.  
  35. private void GetMaterialReferences(GameObject gameObj)
  36. {
  37. Debug.Log($"Getting Materials for {gameObj.name}");
  38. Renderer[] renderers = gameObj.GetComponents<Renderer>();
  39.  
  40. foreach (Renderer renderer in renderers)
  41. {
  42. foreach (Material material in renderer.materials)
  43. {
  44. if (!_regToGhostMaterialMap.ContainsKey(material))
  45. {
  46. if (material.shader.Equals(shaderURP_Lit))
  47. {
  48. Material unlitMaterial = new Material(material);
  49. unlitMaterial.shader = shaderURP_Unlit;
  50. unlitMaterial.name += " Unlit";
  51. _regToGhostMaterialMap[material] = unlitMaterial;
  52. _ghostToRegMaterialMap[unlitMaterial] = material;
  53. }
  54. }
  55. }
  56. }
  57.  
  58. for (int i = 0; i < gameObj.transform.childCount; i++)
  59. {
  60. GetMaterialReferences(gameObj.transform.GetChild(i).gameObject);
  61. }
  62. }
  63.  
  64. public void GhostMode()
  65. {
  66. // If it's a light, turn off.
  67. if (TryGetComponent<Light>(out Light light))
  68. {
  69. light.enabled = false;
  70. }
  71.  
  72. // If it's a rendering object, switch to Unlit shader
  73. ConvertMaterials(gameObject);
  74.  
  75. // If it has emissions, turn them off.
  76. }
  77.  
  78. private void ConvertMaterials(GameObject gameObj)
  79. {
  80. Debug.Log($"Changing materials for {gameObj.name}?");
  81. Renderer[] renderers = gameObj.GetComponents<Renderer>();
  82.  
  83. foreach (Renderer renderer in renderers)
  84. {
  85. for (int i = 0; i < renderer.materials.Length; i++)
  86. {
  87. Debug.Log($"Replacing {renderer.materials[i]}");
  88. renderer.materials[i] = _regToGhostMaterialMap[renderer.materials[i]];
  89. }
  90. }
  91.  
  92. for (int i = 0; i < gameObj.transform.childCount; i++)
  93. {
  94. ConvertMaterials(gameObj.transform.GetChild(i).gameObject);
  95. }
  96. }
  97.  
  98. private void Update()
  99. {
  100. if (Input.GetKeyDown(KeyCode.G))
  101. {
  102. GhostMode();
  103. }
  104. }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement