ApplyToMaterialEditor.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 ApplyToMaterial component
  11. /// </summary>
  12. [CanEditMultipleObjects]
  13. [CustomEditor(typeof(ApplyToMaterial))]
  14. public class ApplyToMaterialEditor : 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 _propMaterial;
  21. private SerializedProperty _propTexturePropertyName;
  22. private SerializedProperty _propDefaultTexture;
  23. private GUIContent[] _materialTextureProperties = new GUIContent[0];
  24. void OnEnable()
  25. {
  26. _propTextureOffset = this.CheckFindProperty("_offset");
  27. _propTextureScale = this.CheckFindProperty("_scale");
  28. _propMediaPlayer = this.CheckFindProperty("_media");
  29. _propMaterial = this.CheckFindProperty("_material");
  30. _propTexturePropertyName = this.CheckFindProperty("_texturePropertyName");
  31. _propDefaultTexture = this.CheckFindProperty("_defaultTexture");
  32. }
  33. public override void OnInspectorGUI()
  34. {
  35. serializedObject.Update();
  36. if (_propMaterial == null)
  37. {
  38. return;
  39. }
  40. EditorGUI.BeginChangeCheck();
  41. EditorGUILayout.PropertyField(_propMediaPlayer);
  42. EditorGUILayout.PropertyField(_propDefaultTexture);
  43. EditorGUILayout.PropertyField(_propMaterial);
  44. bool hasKeywords = false;
  45. int texturePropertyIndex = 0;
  46. if (_propMaterial.objectReferenceValue != null)
  47. {
  48. Material mat = (Material)(_propMaterial.objectReferenceValue);
  49. if (mat.shaderKeywords.Length > 0)
  50. {
  51. hasKeywords = true;
  52. }
  53. MaterialProperty[] matProps = MaterialEditor.GetMaterialProperties(new UnityEngine.Object[] { mat });
  54. List<GUIContent> items = new List<GUIContent>(16);
  55. foreach (MaterialProperty matProp in matProps)
  56. {
  57. if (matProp.type == MaterialProperty.PropType.Texture)
  58. {
  59. if (matProp.name == _propTexturePropertyName.stringValue)
  60. {
  61. texturePropertyIndex = items.Count;
  62. }
  63. items.Add(new GUIContent(matProp.name));
  64. }
  65. }
  66. _materialTextureProperties = items.ToArray();
  67. }
  68. int newTexturePropertyIndex = EditorGUILayout.Popup(_guiTextTextureProperty, texturePropertyIndex, _materialTextureProperties);
  69. if (newTexturePropertyIndex >= 0 && newTexturePropertyIndex < _materialTextureProperties.Length)
  70. {
  71. _propTexturePropertyName.stringValue = _materialTextureProperties[newTexturePropertyIndex].text;
  72. }
  73. if (hasKeywords && _propTexturePropertyName.stringValue != Helper.UnityBaseTextureName)
  74. {
  75. 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);
  76. }
  77. EditorGUILayout.PropertyField(_propTextureOffset);
  78. EditorGUILayout.PropertyField(_propTextureScale);
  79. serializedObject.ApplyModifiedProperties();
  80. bool wasModified = EditorGUI.EndChangeCheck();
  81. if (Application.isPlaying && wasModified)
  82. {
  83. foreach (Object obj in this.targets)
  84. {
  85. ((ApplyToMaterial)obj).ForceUpdate();
  86. }
  87. }
  88. }
  89. }
  90. }