GoldenMaterialEditor.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using UnityEngine;
  2. using UnityEditor;
  3. [ExecuteInEditMode]
  4. public class GoldenMaterialEditor : MaterialEditor
  5. {
  6. public override void OnInspectorGUI()
  7. {
  8. if (!isVisible)
  9. return;
  10. Material material = target as Material;
  11. MaterialProperty[] properties = GetMaterialProperties(targets);
  12. string[] keys = material.shaderKeywords;
  13. bool effectsLayer1Enabled = keys.Contains("EFFECTS_LAYER_1_ON");
  14. bool effectsLayer2Enabled = keys.Contains("EFFECTS_LAYER_2_ON");
  15. bool effectsLayer3Enabled = keys.Contains("EFFECTS_LAYER_3_ON");
  16. bool effectsLayer4Enabled = keys.Contains("EFFECTS_LAYER_4_ON");
  17. EditorGUI.BeginChangeCheck();
  18. for (int i = 0; i < 3; i++)
  19. TexturePropertySingleLine(new GUIContent(properties[i].displayName), properties[i]);
  20. EditorGUILayout.Separator();
  21. effectsLayer1Enabled = EditorGUILayout.Toggle("Effects Layer 1", effectsLayer1Enabled);
  22. if (effectsLayer1Enabled)
  23. DrawEffectsLayer(properties, 1);
  24. effectsLayer2Enabled = EditorGUILayout.Toggle("Effects Layer 2", effectsLayer2Enabled);
  25. if (effectsLayer2Enabled)
  26. DrawEffectsLayer(properties, 2);
  27. effectsLayer3Enabled = EditorGUILayout.Toggle("Effects Layer 3", effectsLayer3Enabled);
  28. if (effectsLayer3Enabled)
  29. DrawEffectsLayer(properties, 3);
  30. effectsLayer4Enabled = EditorGUILayout.Toggle("Effects Layer 4", effectsLayer4Enabled);
  31. if (effectsLayer4Enabled)
  32. DrawEffectsLayer(properties, 4);
  33. if (EditorGUI.EndChangeCheck())
  34. {
  35. string[] newKeys = new string[] {
  36. effectsLayer1Enabled ? "EFFECTS_LAYER_1_ON" : "EFFECTS_LAYER_1_OFF",
  37. effectsLayer2Enabled ? "EFFECTS_LAYER_2_ON" : "EFFECTS_LAYER_2_OFF",
  38. effectsLayer3Enabled ? "EFFECTS_LAYER_3_ON" : "EFFECTS_LAYER_3_OFF",
  39. effectsLayer4Enabled ? "EFFECTS_LAYER_4_ON" : "EFFECTS_LAYER_4_OFF",
  40. };
  41. material.shaderKeywords = newKeys;
  42. EditorUtility.SetDirty(material);
  43. }
  44. }
  45. void DrawEffectsLayer(MaterialProperty[] properties, int layer)
  46. {
  47. GUIStyle style = EditorStyles.helpBox;
  48. style.margin = new RectOffset(20, 20, 0, 0);
  49. EditorGUILayout.BeginVertical(style);
  50. {
  51. TexturePropertySingleLine(new GUIContent("Effect Texture"), properties.GetByName(EffectName(layer, "Tex")));
  52. TexturePropertySingleLine(new GUIContent("Motion Texture"), properties.GetByName(EffectName(layer, "Motion")));
  53. ColorProperty(properties.GetByName(EffectName(layer, "Color")), "Tint Color");
  54. FloatProperty(properties.GetByName(EffectName(layer, "MotionSpeed")), "Motion Speed");
  55. FloatProperty(properties.GetByName(EffectName(layer, "Rotation")), "Rotation Speed");
  56. Vector4 translation = properties.GetByName(EffectName(layer, "Translation")).vectorValue;
  57. EditorGUILayout.BeginHorizontal();
  58. {
  59. EditorGUILayout.LabelField("Positon");
  60. translation.x = EditorGUILayout.FloatField(translation.x);
  61. translation.y = EditorGUILayout.FloatField(translation.y);
  62. }
  63. EditorGUILayout.EndHorizontal();
  64. properties.GetByName(EffectName(layer, "Translation")).vectorValue = translation;
  65. Vector4 pivotScale = properties.GetByName(EffectName(layer, "PivotScale")).vectorValue;
  66. EditorGUILayout.BeginHorizontal();
  67. {
  68. EditorGUILayout.LabelField("Pivot");
  69. pivotScale.x = EditorGUILayout.FloatField(pivotScale.x);
  70. pivotScale.y = EditorGUILayout.FloatField(pivotScale.y);
  71. }
  72. EditorGUILayout.EndHorizontal();
  73. EditorGUILayout.BeginHorizontal();
  74. {
  75. EditorGUILayout.LabelField("Scale");
  76. pivotScale.z = EditorGUILayout.FloatField(pivotScale.z);
  77. pivotScale.w = EditorGUILayout.FloatField(pivotScale.w);
  78. }
  79. EditorGUILayout.EndHorizontal();
  80. properties.GetByName(EffectName(layer, "PivotScale")).vectorValue = pivotScale;
  81. BoolProperty(properties.GetByName(EffectName(layer, "Foreground")), "Foreground");
  82. }
  83. EditorGUILayout.EndVertical();
  84. }
  85. bool BoolProperty(MaterialProperty property, string name)
  86. {
  87. bool toggle = property.floatValue == 0 ? false : true;
  88. toggle = EditorGUILayout.Toggle(name, toggle);
  89. property.floatValue = toggle ? 1 : 0;
  90. return toggle;
  91. }
  92. string EffectName(int layer, string property)
  93. {
  94. return string.Format("_EffectsLayer{0}{1}", layer.ToString(), property);
  95. }
  96. }