SerializedPassPropertyDrawer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace EPOOutline
  8. {
  9. public static class SerializedPropertyExtentions
  10. {
  11. private static MethodInfo getFieldInfoAndStaticTypeFromProperty;
  12. public static Attribute[] GetFieldAttributes(this SerializedProperty prop)
  13. {
  14. if (getFieldInfoAndStaticTypeFromProperty == null)
  15. {
  16. foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
  17. {
  18. foreach (var t in assembly.GetTypes())
  19. {
  20. if (t.Name != "ScriptAttributeUtility")
  21. continue;
  22. getFieldInfoAndStaticTypeFromProperty = t.GetMethod("GetFieldAttributes", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
  23. foreach (var info in getFieldInfoAndStaticTypeFromProperty.GetParameters())
  24. Debug.Log(info.ParameterType + " " + info.Name);
  25. break;
  26. }
  27. if (getFieldInfoAndStaticTypeFromProperty != null)
  28. break;
  29. }
  30. }
  31. var p = new object[] { prop, null };
  32. var fieldInfo = getFieldInfoAndStaticTypeFromProperty.Invoke(null, p) as Attribute[];
  33. return fieldInfo;
  34. }
  35. public static T GetCustomAttributeFromProperty<T>(this SerializedProperty prop) where T : System.Attribute
  36. {
  37. return Array.Find(GetFieldAttributes(prop), x => x is T) as T;
  38. }
  39. }
  40. [CustomPropertyDrawer(typeof(SerializedPass))]
  41. public class SerializedPassPropertyDrawer : PropertyDrawer
  42. {
  43. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  44. {
  45. var drawingPosition = position;
  46. drawingPosition.height = EditorGUIUtility.singleLineHeight;
  47. var shaderProperty = property.FindPropertyRelative("shader");
  48. var currentShaderReference = shaderProperty.objectReferenceValue as Shader;
  49. var attribute = (SerializedPassInfoAttribute)null;// property.GetCustomAttributeFromProperty<SerializedPassInfoAttribute>();
  50. var prefix = attribute == null ? "Hidden/EPO/Fill/" : attribute.ShadersFolder;
  51. var fillLabel = currentShaderReference == null ? "none" : currentShaderReference.name.Substring(prefix.Length);
  52. if (shaderProperty.hasMultipleDifferentValues)
  53. fillLabel = "-";
  54. if (EditorGUI.DropdownButton(position, new GUIContent(label.text + " : " + fillLabel), FocusType.Passive))
  55. {
  56. var menu = new GenericMenu();
  57. menu.AddItem(new GUIContent("none"), currentShaderReference == null && !shaderProperty.hasMultipleDifferentValues, () =>
  58. {
  59. shaderProperty.objectReferenceValue = null;
  60. shaderProperty.serializedObject.ApplyModifiedProperties();
  61. });
  62. var shaders = AssetDatabase.FindAssets("t:Shader");
  63. foreach (var shader in shaders)
  64. {
  65. var loadedShader = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(shader), typeof(Shader)) as Shader;
  66. if (!loadedShader.name.StartsWith(prefix))
  67. continue;
  68. menu.AddItem(new GUIContent(loadedShader.name.Substring(prefix.Length)), loadedShader == shaderProperty.objectReferenceValue && !shaderProperty.hasMultipleDifferentValues, () =>
  69. {
  70. shaderProperty.objectReferenceValue = loadedShader;
  71. shaderProperty.serializedObject.ApplyModifiedProperties();
  72. });
  73. }
  74. menu.ShowAsContext();
  75. }
  76. if (shaderProperty.hasMultipleDifferentValues)
  77. return;
  78. if (currentShaderReference != null)
  79. {
  80. position.x += EditorGUIUtility.singleLineHeight;
  81. position.width -= EditorGUIUtility.singleLineHeight;
  82. var properties = new Dictionary<string, SerializedProperty>();
  83. var serializedProperties = property.FindPropertyRelative("serializedProperties");
  84. for (var index = 0; index < serializedProperties.arraySize; index++)
  85. {
  86. var subProperty = serializedProperties.GetArrayElementAtIndex(index);
  87. var propertyName = subProperty.FindPropertyRelative("PropertyName");
  88. var propertyValue = subProperty.FindPropertyRelative("Property");
  89. if (propertyName == null || propertyValue == null)
  90. break;
  91. properties.Add(propertyName.stringValue, propertyValue);
  92. }
  93. var fillParametersPosition = position;
  94. fillParametersPosition.height = EditorGUIUtility.singleLineHeight;
  95. for (var index = 0; index < ShaderUtil.GetPropertyCount(currentShaderReference); index++)
  96. {
  97. var propertyName = ShaderUtil.GetPropertyName(currentShaderReference, index);
  98. if (!propertyName.StartsWith("_Public"))
  99. continue;
  100. fillParametersPosition.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  101. SerializedProperty currentProperty;
  102. if (!properties.TryGetValue(propertyName, out currentProperty))
  103. {
  104. serializedProperties.InsertArrayElementAtIndex(serializedProperties.arraySize);
  105. currentProperty = serializedProperties.GetArrayElementAtIndex(serializedProperties.arraySize - 1);
  106. currentProperty.FindPropertyRelative("PropertyName").stringValue = propertyName;
  107. currentProperty = currentProperty.FindPropertyRelative("Property");
  108. var tempMaterial = new Material(currentShaderReference);
  109. switch (ShaderUtil.GetPropertyType(currentShaderReference, index))
  110. {
  111. case ShaderUtil.ShaderPropertyType.Color:
  112. currentProperty.FindPropertyRelative("ColorValue").colorValue = tempMaterial.GetColor(propertyName);
  113. break;
  114. case ShaderUtil.ShaderPropertyType.Vector:
  115. currentProperty.FindPropertyRelative("VectorValue").vector4Value = tempMaterial.GetVector(propertyName);
  116. break;
  117. case ShaderUtil.ShaderPropertyType.Float:
  118. currentProperty.FindPropertyRelative("FloatValue").floatValue = tempMaterial.GetFloat(propertyName);
  119. break;
  120. case ShaderUtil.ShaderPropertyType.Range:
  121. currentProperty.FindPropertyRelative("FloatValue").floatValue = tempMaterial.GetFloat(propertyName);
  122. break;
  123. case ShaderUtil.ShaderPropertyType.TexEnv:
  124. currentProperty.FindPropertyRelative("TextureValue").objectReferenceValue = tempMaterial.GetTexture(propertyName);
  125. break;
  126. }
  127. GameObject.DestroyImmediate(tempMaterial);
  128. properties.Add(propertyName, currentProperty);
  129. }
  130. if (currentProperty == null)
  131. continue;
  132. var content = new GUIContent(ShaderUtil.GetPropertyDescription(currentShaderReference, index));
  133. switch (ShaderUtil.GetPropertyType(currentShaderReference, index))
  134. {
  135. case ShaderUtil.ShaderPropertyType.Color:
  136. var colorProperty = currentProperty.FindPropertyRelative("ColorValue");
  137. colorProperty.colorValue = EditorGUI.ColorField(fillParametersPosition, content, colorProperty.colorValue, true, true, true);
  138. break;
  139. case ShaderUtil.ShaderPropertyType.Vector:
  140. var vectorProperty = currentProperty.FindPropertyRelative("VectorValue");
  141. vectorProperty.vector4Value = EditorGUI.Vector4Field(fillParametersPosition, content, vectorProperty.vector4Value);
  142. break;
  143. case ShaderUtil.ShaderPropertyType.Float:
  144. EditorGUI.PropertyField(fillParametersPosition, currentProperty.FindPropertyRelative("FloatValue"), content);
  145. break;
  146. case ShaderUtil.ShaderPropertyType.Range:
  147. var floatProperty = currentProperty.FindPropertyRelative("FloatValue");
  148. floatProperty.floatValue = EditorGUI.Slider(fillParametersPosition, content, floatProperty.floatValue,
  149. ShaderUtil.GetRangeLimits(currentShaderReference, index, 1),
  150. ShaderUtil.GetRangeLimits(currentShaderReference, index, 2));
  151. break;
  152. case ShaderUtil.ShaderPropertyType.TexEnv:
  153. EditorGUI.PropertyField(fillParametersPosition, currentProperty.FindPropertyRelative("TextureValue"), content);
  154. break;
  155. }
  156. currentProperty.FindPropertyRelative("PropertyType").intValue = (int)ShaderUtil.GetPropertyType(currentShaderReference, index);
  157. }
  158. }
  159. }
  160. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  161. {
  162. if (property.FindPropertyRelative("shader").hasMultipleDifferentValues)
  163. return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  164. var shaderProperty = property.FindPropertyRelative("shader");
  165. var currentShaderReference = shaderProperty.objectReferenceValue as Shader;
  166. var additionalCount = 0;
  167. if (currentShaderReference != null)
  168. {
  169. for (var index = 0; index < ShaderUtil.GetPropertyCount(currentShaderReference); index++)
  170. {
  171. var propertyName = ShaderUtil.GetPropertyName(currentShaderReference, index);
  172. if (!propertyName.StartsWith("_Public"))
  173. continue;
  174. additionalCount++;
  175. }
  176. }
  177. return (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing) * (additionalCount + 1);
  178. }
  179. }
  180. }