Advertisement
Guest User

Untitled

a guest
Sep 14th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using UnityEngine;
  6.  
  7. [ExecuteInEditMode]
  8.  
  9. public class SetMaterial : MonoBehaviour
  10. {
  11. public bool Recursive = true;
  12. public GameObject[] RecursiveWhitelist;
  13.  
  14. [SerializeField]
  15. private bool _visible = true;
  16.  
  17. public bool Visible
  18. {
  19. get { return _visible; }
  20. set
  21. {
  22. if (_visible != value)
  23. {
  24. ApplyVisibility(value);
  25. }
  26. }
  27. }
  28.  
  29. [Space]
  30. [SerializeField]
  31. public bool OverrideMaterial = false;
  32. [SerializeField]
  33. private Material _material = null;
  34. public Material Material
  35. {
  36. get { return _material; }
  37. set
  38. {
  39. if (value != _material)
  40. {
  41. OverrideMaterial = true;
  42. ApplyMaterial(value);
  43. }
  44. }
  45. }
  46.  
  47. [Space]
  48. [SerializeField]
  49. public bool OverrideColor = false;
  50. [SerializeField]
  51. private Color _color = Color.white;
  52. public Color Color
  53. {
  54. get { return _color; }
  55. set
  56. {
  57. if (value != _color)
  58. {
  59. OverrideColor = true;
  60. if (OverrideAlpha)
  61. {
  62. value = new Color(value.r, value.g, value.b, _alpha);
  63. }
  64. ApplyColor(value);
  65. }
  66. }
  67. }
  68.  
  69. [Space]
  70. [SerializeField]
  71. public bool OverrideAlpha = false;
  72. [SerializeField]
  73. private float _alpha = 1f;
  74. public float Alpha {
  75. get { return _alpha; }
  76. set
  77. {
  78. float diff = value - _alpha;
  79. if (diff < -0.001f || diff > 0.001f)
  80. {
  81. OverrideAlpha = true;
  82. if (OverrideColor)
  83. {
  84. _alpha = value;
  85. _color = new Color(_color.r, _color.g, _color.b, _alpha);
  86. ApplyColor(_color);
  87. }
  88. else
  89. {
  90. ApplyAlpha(value);
  91. }
  92. }
  93. }
  94. }
  95.  
  96. protected ICollection<MeshRenderer> GetRenderers()
  97. {
  98. if (Recursive)
  99. {
  100. if (RecursiveWhitelist != null && RecursiveWhitelist.Length > 0)
  101. {
  102. List<MeshRenderer> renderers = new List<MeshRenderer>();;
  103. foreach (GameObject gameObject in RecursiveWhitelist)
  104. {
  105. renderers.AddRange(gameObject.GetComponentsInChildren<MeshRenderer>());
  106. }
  107.  
  108. return renderers;
  109. }
  110.  
  111. return GetComponentsInChildren<MeshRenderer>();
  112. }
  113. else
  114. return new[] { GetComponent<MeshRenderer>() };
  115. }
  116.  
  117.  
  118. public void ApplyVisibility(bool visible)
  119. {
  120. _visible = visible;
  121. foreach (MeshRenderer mesh in GetRenderers())
  122. mesh.enabled = _visible;
  123. }
  124.  
  125. public void ApplyMaterial(Material mat)
  126. {
  127. _material = mat;
  128. foreach (MeshRenderer mesh in GetRenderers())
  129. if(mesh.material != mat)
  130. mesh.material = mat;
  131.  
  132. if (OverrideColor)
  133. {
  134. ApplyColor(_color);
  135. }
  136. else if (OverrideAlpha)
  137. {
  138. ApplyAlpha(_alpha);
  139. }
  140. }
  141.  
  142. public void ApplyColor(Color color)
  143. {
  144. _color = color;
  145. foreach (MeshRenderer mesh in GetRenderers())
  146. mesh.material.color = color;
  147. }
  148.  
  149. public void ApplyAlpha(float alpha)
  150. {
  151. _alpha = alpha;
  152. foreach (MeshRenderer mesh in GetRenderers())
  153. mesh.material.color = new Color(
  154. mesh.material.color.r,
  155. mesh.material.color.g,
  156. mesh.material.color.b,
  157. alpha);
  158. }
  159.  
  160.  
  161. private void ApplyAll()
  162. {
  163. if (OverrideMaterial)
  164. {
  165. ApplyMaterial(_material);
  166. }
  167.  
  168. if (OverrideColor)
  169. {
  170. Color c = _color;
  171. if (OverrideAlpha)
  172. {
  173. c = new Color(_color.r, _color.g, _color.b, Alpha);
  174. }
  175. ApplyColor(c);
  176. }else if (OverrideAlpha)
  177. {
  178. ApplyAlpha(_alpha);
  179. }
  180. }
  181.  
  182.  
  183. void Start ()
  184. {
  185. ApplyAll();
  186. }
  187.  
  188. void Update () {
  189. if (!Application.isPlaying)
  190. {
  191. ApplyAll();
  192. }
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement