ImageAppearanceProviderEditorHelper.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace TheraBytes.BetterUi.Editor
  8. {
  9. public class ImageAppearanceProviderEditorHelper
  10. {
  11. static readonly string DEFAULT = "Default";
  12. static readonly string CUSTOM = "Custom";
  13. SerializedObject serializedObject;
  14. IImageAppearanceProvider img;
  15. private SerializedProperty materialProperty1, materialProperty2, materialProperty3;
  16. private SerializedProperty propMatType, propEffType;
  17. string[] materials;
  18. int materialIndex, materialEffectIndex;
  19. public ImageAppearanceProviderEditorHelper(SerializedObject serializedObject, IImageAppearanceProvider img)
  20. {
  21. this.serializedObject = serializedObject;
  22. this.img = img;
  23. this.materialProperty1 = serializedObject.FindProperty("materialProperty1");
  24. this.materialProperty2 = serializedObject.FindProperty("materialProperty2");
  25. this.materialProperty3 = serializedObject.FindProperty("materialProperty3");
  26. propMatType = serializedObject.FindProperty("materialType");
  27. propEffType = serializedObject.FindProperty("materialEffect");
  28. List<string> materialOptions = new List<string>();
  29. materialOptions.Add(DEFAULT);
  30. materialOptions.AddRange(Materials.Instance.GetAllMaterialNames());
  31. materialOptions.Add(CUSTOM);
  32. materials = materialOptions.ToArray();
  33. materialIndex = materialOptions.IndexOf(img.MaterialType);
  34. if (materialIndex < 0)
  35. materialIndex = 0;
  36. var effectOptions = Materials.Instance.GetAllMaterialEffects(img.MaterialType).ToList();
  37. materialEffectIndex = effectOptions.IndexOf(img.MaterialEffect);
  38. if (materialEffectIndex < 0)
  39. materialEffectIndex = 0;
  40. }
  41. public void DrawMaterialGui(SerializedProperty materialProp)
  42. {
  43. // MATERIAL
  44. materialIndex = EditorGUILayout.Popup("Material", materialIndex, materials);
  45. string materialType = materials[materialIndex];
  46. MaterialEffect effect;
  47. if (materialType == CUSTOM || materialType == DEFAULT)
  48. {
  49. effect = MaterialEffect.Normal;
  50. }
  51. else
  52. {
  53. var options = Materials.Instance.GetAllMaterialEffects(materialType).Select(o => o.ToString()).ToArray();
  54. materialEffectIndex = EditorGUILayout.Popup("Effect", materialEffectIndex, options);
  55. if (materialEffectIndex >= options.Length)
  56. materialEffectIndex = 0;
  57. effect = (MaterialEffect)Enum.Parse(typeof(MaterialEffect), options[materialEffectIndex]);
  58. }
  59. var materialInfo = Materials.Instance.GetMaterialInfo(materialType, effect);
  60. var material = (materialInfo != null) ? materialInfo.Material : null;
  61. var propVars = serializedObject.FindProperty("materialProperties");
  62. // material type changed
  63. bool materialChanged = propMatType.stringValue != materialType;
  64. bool effectChanged = propEffType.enumValueIndex != (int)effect;
  65. if (materialChanged || effectChanged)
  66. {
  67. propMatType.stringValue = materialType;
  68. materialProp.objectReferenceValue = material;
  69. propEffType.enumValueIndex = (int)effect;
  70. int infoIdx = Materials.Instance.GetMaterialInfoIndex(materialType, effect);
  71. if (infoIdx >= 0)
  72. {
  73. SerializedObject obj = new SerializedObject(Materials.Instance);
  74. var source = obj.FindProperty("materials")
  75. .GetArrayElementAtIndex(infoIdx)
  76. .FindPropertyRelative("Properties");
  77. SerializedPropertyUtil.Copy(source, propVars);
  78. propVars = serializedObject.FindProperty("materialProperties");
  79. serializedObject.ApplyModifiedPropertiesWithoutUndo();
  80. // update material properties
  81. var floats = propVars.FindPropertyRelative("FloatProperties");
  82. if (floats != null)
  83. {
  84. for (int i = 0; i < floats.arraySize; i++)
  85. {
  86. var p = floats.GetArrayElementAtIndex(i);
  87. SerializedProperty innerProp = p.FindPropertyRelative("Value");
  88. if (innerProp == null)
  89. continue;
  90. SerializedProperty valProp;
  91. switch (i)
  92. {
  93. case 0: valProp = materialProperty1; break;
  94. case 1: valProp = materialProperty2; break;
  95. case 2: valProp = materialProperty3; break;
  96. default: throw new ArgumentOutOfRangeException();
  97. }
  98. if (materialChanged)
  99. valProp.floatValue = innerProp.floatValue;
  100. else if (effectChanged)
  101. innerProp.floatValue = valProp.floatValue;
  102. }
  103. }
  104. }
  105. serializedObject.ApplyModifiedPropertiesWithoutUndo();
  106. }
  107. if (materialType == CUSTOM)
  108. {
  109. EditorGUILayout.PropertyField(materialProp, new GUILayoutOption[0]);
  110. }
  111. else if (materialType != DEFAULT)
  112. {
  113. var floats = propVars.FindPropertyRelative("FloatProperties");
  114. if (floats != null)
  115. {
  116. for (int i = 0; i < floats.arraySize; i++)
  117. {
  118. var f = img.MaterialProperties.FloatProperties[i];
  119. var p = floats.GetArrayElementAtIndex(i);
  120. string displayName = p.FindPropertyRelative("Name").stringValue;
  121. SerializedProperty valProp;
  122. switch (i)
  123. {
  124. case 0: valProp = materialProperty1; break;
  125. case 1: valProp = materialProperty2; break;
  126. case 2: valProp = materialProperty3; break;
  127. default: throw new ArgumentOutOfRangeException();
  128. }
  129. if (f.IsRestricted)
  130. {
  131. EditorGUILayout.Slider(valProp, f.Min, f.Max, displayName);
  132. }
  133. else
  134. {
  135. EditorGUILayout.PropertyField(valProp, new GUIContent(displayName));
  136. }
  137. SerializedProperty innerProp = p.FindPropertyRelative("Value");
  138. innerProp.floatValue = valProp.floatValue;
  139. }
  140. }
  141. }
  142. if (materialType == CUSTOM && materialProp.objectReferenceValue != null)
  143. {
  144. bool isOrig = !(materialProp.objectReferenceValue.name.EndsWith("(Clone)")); // TODO: find better check
  145. EditorGUILayout.BeginHorizontal(new GUILayoutOption[0]);
  146. GUILayout.Label((isOrig) ? "Material: SHARED" : "Material: CLONED",
  147. GUILayout.Width(EditorGUIUtility.labelWidth));
  148. if (GUILayout.Button((isOrig) ? "Clone" : "Remove",
  149. EditorStyles.miniButton, new GUILayoutOption[0]))
  150. {
  151. materialProp.objectReferenceValue = (isOrig)
  152. ? Material.Instantiate(img.material)
  153. : null;
  154. img.SetMaterialDirty();
  155. }
  156. EditorGUILayout.EndHorizontal();
  157. }
  158. }
  159. public static void DrawColorGui(SerializedProperty colorMode, SerializedProperty firstColor, SerializedProperty secondColor)
  160. {
  161. // COLOR
  162. EditorGUILayout.PropertyField(colorMode);
  163. EditorGUILayout.BeginHorizontal();
  164. EditorGUILayout.BeginVertical();
  165. EditorGUILayout.PropertyField(firstColor, new GUILayoutOption[0]);
  166. var mode = (ColorMode)colorMode.intValue;
  167. if (mode != ColorMode.Color)
  168. {
  169. EditorGUILayout.PropertyField(secondColor);
  170. }
  171. EditorGUILayout.EndVertical();
  172. if (mode != ColorMode.Color)
  173. {
  174. if (GUILayout.Button("↕",
  175. GUILayout.Width(25), GUILayout.Height(2 * EditorGUIUtility.singleLineHeight)))
  176. {
  177. Color a = firstColor.colorValue;
  178. firstColor.colorValue = secondColor.colorValue;
  179. secondColor.colorValue = a;
  180. }
  181. }
  182. EditorGUILayout.EndHorizontal();
  183. EditorGUILayout.Separator();
  184. }
  185. }
  186. }