ApplyToMeshEditor.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. //-----------------------------------------------------------------------------
  5. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. namespace RenderHeads.Media.AVProVideo.Editor
  8. {
  9. /// <summary>
  10. /// Editor for the ApplyToMesh component
  11. /// </summary>
  12. [CanEditMultipleObjects]
  13. [CustomEditor(typeof(ApplyToMesh))]
  14. public class ApplyToMeshEditor : UnityEditor.Editor
  15. {
  16. private static readonly GUIContent _guiTextTextureProperty = new GUIContent("Texture Property");
  17. private SerializedProperty _propTextureOffset;
  18. private SerializedProperty _propTextureScale;
  19. private SerializedProperty _propMediaPlayer;
  20. private SerializedProperty _propRenderer;
  21. private SerializedProperty _propMaterialIndex;
  22. private SerializedProperty _propTexturePropertyName;
  23. private SerializedProperty _propDefaultTexture;
  24. private SerializedProperty _propAutomaticStereoPacking;
  25. private SerializedProperty _propOverrideStereoPacking;
  26. private SerializedProperty _propStereoRedGreenTint;
  27. private GUIContent[] _materialTextureProperties = new GUIContent[0];
  28. void OnEnable()
  29. {
  30. _propTextureOffset = this.CheckFindProperty("_offset");
  31. _propTextureScale = this.CheckFindProperty("_scale");
  32. _propMediaPlayer = this.CheckFindProperty("_media");
  33. _propRenderer = this.CheckFindProperty("_renderer");
  34. _propMaterialIndex = this.CheckFindProperty("_materialIndex");
  35. _propTexturePropertyName = this.CheckFindProperty("_texturePropertyName");
  36. _propDefaultTexture = this.CheckFindProperty("_defaultTexture");
  37. _propAutomaticStereoPacking = this.CheckFindProperty("_automaticStereoPacking");
  38. _propOverrideStereoPacking = this.CheckFindProperty("_overrideStereoPacking");
  39. _propStereoRedGreenTint = this.CheckFindProperty("_stereoRedGreenTint");
  40. }
  41. public override void OnInspectorGUI()
  42. {
  43. serializedObject.Update();
  44. if (_propRenderer == null)
  45. {
  46. return;
  47. }
  48. EditorGUI.BeginChangeCheck();
  49. EditorGUILayout.PropertyField(_propMediaPlayer);
  50. EditorGUILayout.PropertyField(_propDefaultTexture);
  51. EditorGUILayout.PropertyField(_propRenderer);
  52. bool hasKeywords = false;
  53. int materialCount = 0;
  54. int texturePropertyIndex = 0;
  55. _materialTextureProperties = new GUIContent[0];
  56. if (_propRenderer.objectReferenceValue != null)
  57. {
  58. Renderer r = (Renderer)(_propRenderer.objectReferenceValue);
  59. materialCount = r.sharedMaterials.Length;
  60. List<Material> nonNullMaterials = new List<Material>(r.sharedMaterials);
  61. // Remove any null materials (otherwise MaterialEditor.GetMaterialProperties() errors)
  62. for (int i = 0; i < nonNullMaterials.Count; i++)
  63. {
  64. if (nonNullMaterials[i] == null)
  65. {
  66. nonNullMaterials.RemoveAt(i);
  67. i--;
  68. }
  69. }
  70. if (nonNullMaterials.Count > 0)
  71. {
  72. // Detect if there are any keywords
  73. foreach (Material mat in nonNullMaterials)
  74. {
  75. if (mat.shaderKeywords.Length > 0)
  76. {
  77. hasKeywords = true;
  78. break;
  79. }
  80. }
  81. // Get unique list of texture property names
  82. List<GUIContent> items = new List<GUIContent>(16);
  83. List<string> textureNames = new List<string>(8);
  84. foreach (Material mat in nonNullMaterials)
  85. {
  86. // NOTE: we process each material separately instead of passing them all into MaterialEditor.GetMaterialProperties() as it errors if the materials have different properties
  87. MaterialProperty[] matProps = MaterialEditor.GetMaterialProperties(new Object[] { mat });
  88. foreach (MaterialProperty matProp in matProps)
  89. {
  90. if (matProp.type == MaterialProperty.PropType.Texture)
  91. {
  92. if (!textureNames.Contains(matProp.name))
  93. {
  94. if (matProp.name == _propTexturePropertyName.stringValue)
  95. {
  96. texturePropertyIndex = items.Count;
  97. }
  98. textureNames.Add(matProp.name);
  99. items.Add(new GUIContent(matProp.name));
  100. }
  101. }
  102. }
  103. }
  104. _materialTextureProperties = items.ToArray();
  105. }
  106. }
  107. if (materialCount > 0)
  108. {
  109. GUILayout.BeginHorizontal();
  110. EditorGUILayout.PrefixLabel("All Materials");
  111. EditorGUI.BeginChangeCheck();
  112. EditorGUILayout.Toggle(_propMaterialIndex.intValue < 0);
  113. if (EditorGUI.EndChangeCheck())
  114. {
  115. if (_propMaterialIndex.intValue < 0)
  116. {
  117. _propMaterialIndex.intValue = 0;
  118. }
  119. else
  120. {
  121. _propMaterialIndex.intValue = -1;
  122. }
  123. }
  124. GUILayout.EndHorizontal();
  125. if (_propMaterialIndex.intValue >= 0)
  126. {
  127. GUILayout.BeginHorizontal();
  128. EditorGUILayout.PrefixLabel("Material Index");
  129. _propMaterialIndex.intValue = EditorGUILayout.IntSlider(_propMaterialIndex.intValue, 0, materialCount - 1);
  130. GUILayout.EndHorizontal();
  131. }
  132. }
  133. int newTexturePropertyIndex = EditorGUILayout.Popup(_guiTextTextureProperty, texturePropertyIndex, _materialTextureProperties);
  134. if (newTexturePropertyIndex >= 0 && newTexturePropertyIndex < _materialTextureProperties.Length)
  135. {
  136. _propTexturePropertyName.stringValue = _materialTextureProperties[newTexturePropertyIndex].text;
  137. }
  138. if (hasKeywords && _propTexturePropertyName.stringValue != Helper.UnityBaseTextureName)
  139. {
  140. EditorGUILayout.HelpBox("When using an uber shader you may need to enable the keywords on a material for certain texture slots to take effect. You can sometimes achieve this (eg with Standard shader) by putting a dummy texture into the texture slot.", MessageType.Info);
  141. }
  142. EditorGUILayout.PropertyField(_propTextureOffset);
  143. EditorGUILayout.PropertyField(_propTextureScale);
  144. EditorGUILayout.PropertyField(_propAutomaticStereoPacking);
  145. if (!_propAutomaticStereoPacking.boolValue)
  146. {
  147. EditorGUILayout.PropertyField(_propOverrideStereoPacking);
  148. }
  149. EditorGUILayout.PropertyField(_propStereoRedGreenTint);
  150. serializedObject.ApplyModifiedProperties();
  151. bool wasModified = EditorGUI.EndChangeCheck();
  152. if (Application.isPlaying && wasModified)
  153. {
  154. foreach (Object obj in this.targets)
  155. {
  156. ((ApplyToMesh)obj).ForceUpdate();
  157. }
  158. }
  159. }
  160. }
  161. }