OutlineTargetPropertyDrawer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace EPOOutline
  6. {
  7. [CustomPropertyDrawer(typeof(OutlineTarget))]
  8. public class OutlineTargetPropertyDrawer : PropertyDrawer
  9. {
  10. private static float lastWidth = 0.0f;
  11. private static GUIContent errorContent = new GUIContent("'Optimize mesh data' option is enabled in build settings.\n In order the feature to work it should be disabled.\n It might seems to work in editor but will not work in build if the setting is enabled.");
  12. private void Shift(ref Rect rect, bool right)
  13. {
  14. if (right)
  15. rect.x += EditorGUIUtility.singleLineHeight;
  16. rect.width -= EditorGUIUtility.singleLineHeight * (right ? 1.0f : 0.5f);
  17. }
  18. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  19. {
  20. lastWidth = position.width;
  21. var initialPosition = position;
  22. var labelWidth = EditorGUIUtility.labelWidth;
  23. position.y += EditorGUIUtility.singleLineHeight * 0.2f;
  24. position.height = EditorGUIUtility.singleLineHeight;
  25. var rendererPosition = position;
  26. rendererPosition.width = position.width / 2;
  27. Shift(ref rendererPosition, false);
  28. var renderer = property.FindPropertyRelative("renderer");
  29. EditorGUI.PropertyField(rendererPosition, renderer, GUIContent.none);
  30. var menu = new GenericMenu();
  31. var textureNameProperty = property.FindPropertyRelative("cutoutTextureName");
  32. var cutoutIsInUse = !string.IsNullOrEmpty(textureNameProperty.stringValue);
  33. menu.AddItem(new GUIContent("none"), string.IsNullOrEmpty(textureNameProperty.stringValue), () =>
  34. {
  35. textureNameProperty.stringValue = string.Empty;
  36. textureNameProperty.serializedObject.ApplyModifiedProperties();
  37. });
  38. var rendererReference = renderer.objectReferenceValue as Renderer;
  39. var referenceName = "none";
  40. var usingCutout = cutoutIsInUse && rendererReference != null;
  41. if (rendererReference != null)
  42. {
  43. var material = rendererReference.sharedMaterial;
  44. if (material != null)
  45. {
  46. var propertiesCount = ShaderUtil.GetPropertyCount(material.shader);
  47. for (var index = 0; index < propertiesCount; index++)
  48. {
  49. var propertyType = ShaderUtil.GetPropertyType(material.shader, index);
  50. if (propertyType != ShaderUtil.ShaderPropertyType.TexEnv)
  51. continue;
  52. var propertyName = ShaderUtil.GetPropertyName(material.shader, index);
  53. var equals = propertyName == textureNameProperty.stringValue;
  54. if (equals)
  55. referenceName = ShaderUtil.GetPropertyDescription(material.shader, index) + " '" + propertyName + "'";
  56. menu.AddItem(new GUIContent(ShaderUtil.GetPropertyDescription(material.shader, index) + " '" + propertyName + "'"), equals && usingCutout, () =>
  57. {
  58. textureNameProperty.stringValue = propertyName;
  59. textureNameProperty.serializedObject.ApplyModifiedProperties();
  60. });
  61. }
  62. }
  63. }
  64. var cutoutPosition = position;
  65. cutoutPosition.x = rendererPosition.x + rendererPosition.width;
  66. cutoutPosition.width -= rendererPosition.width;
  67. Shift(ref cutoutPosition, true);
  68. var sourceLable = usingCutout ? referenceName : "none";
  69. var cutoutSourceLabel = "Cutout source: " + sourceLable;
  70. if (EditorGUI.DropdownButton(cutoutPosition, new GUIContent(cutoutSourceLabel), FocusType.Passive))
  71. menu.ShowAsContext();
  72. var drawingPosition = position;
  73. EditorGUIUtility.labelWidth = 80;
  74. drawingPosition.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  75. var boundsModePosition = initialPosition;
  76. boundsModePosition.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing * 3.0f;
  77. boundsModePosition.width = initialPosition.width / 2.0f;
  78. boundsModePosition.height = EditorGUIUtility.singleLineHeight;
  79. EditorGUI.PropertyField(boundsModePosition, property.FindPropertyRelative("BoundsMode"));
  80. boundsModePosition.x += boundsModePosition.width;
  81. EditorGUIUtility.labelWidth = 80;
  82. Shift(ref boundsModePosition, true);
  83. EditorGUI.PropertyField(boundsModePosition, property.FindPropertyRelative("cutoutTextureIndex"), new GUIContent("Texture index"));
  84. if (property.FindPropertyRelative("BoundsMode").intValue == (int)BoundsMode.Manual)
  85. {
  86. drawingPosition.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  87. EditorGUI.PropertyField(drawingPosition, property.FindPropertyRelative("Bounds"), GUIContent.none);
  88. drawingPosition.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  89. }
  90. EditorGUIUtility.labelWidth = labelWidth;
  91. if (usingCutout || rendererReference is SpriteRenderer)
  92. {
  93. drawingPosition.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  94. EditorGUI.PropertyField(drawingPosition, property.FindPropertyRelative("CutoutThreshold"));
  95. drawingPosition.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  96. EditorGUI.PropertyField(drawingPosition, property.FindPropertyRelative("CutoutMask"));
  97. }
  98. else
  99. {
  100. var isDilateRenderingMode = property.FindPropertyRelative("DilateRenderingMode").intValue == (int)DilateRenderMode.EdgeShift;
  101. var appropriateToUseEdgeDilate = renderer.objectReferenceValue != null && !(renderer.objectReferenceValue as Renderer).gameObject.isStatic;
  102. if (appropriateToUseEdgeDilate)
  103. drawingPosition.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  104. var modeDrawingPosition = drawingPosition;
  105. modeDrawingPosition.width /= 2;
  106. if (appropriateToUseEdgeDilate)
  107. {
  108. Shift(ref modeDrawingPosition, false);
  109. EditorGUI.LabelField(modeDrawingPosition, property.FindPropertyRelative("DilateRenderingMode").displayName);
  110. modeDrawingPosition.x += modeDrawingPosition.width;
  111. Shift(ref modeDrawingPosition, true);
  112. var initialColor = GUI.color;
  113. if (isDilateRenderingMode && PlayerSettings.stripUnusedMeshComponents)
  114. GUI.color = Color.red;
  115. EditorGUI.PropertyField(modeDrawingPosition, property.FindPropertyRelative("DilateRenderingMode"), GUIContent.none);
  116. GUI.color = initialColor;
  117. }
  118. if (isDilateRenderingMode && appropriateToUseEdgeDilate)
  119. {
  120. drawingPosition.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  121. if (PlayerSettings.stripUnusedMeshComponents)
  122. {
  123. var helpBoxPosition = drawingPosition;
  124. var buttonStart = EditorStyles.helpBox.CalcHeight(errorContent, lastWidth - 60) + EditorGUIUtility.standardVerticalSpacing;
  125. helpBoxPosition.height = buttonStart + EditorGUIUtility.singleLineHeight * 2.0f;
  126. EditorGUI.HelpBox(helpBoxPosition, errorContent.text, MessageType.Error);
  127. var buttonPosition = drawingPosition;
  128. buttonPosition.y = helpBoxPosition.y + buttonStart + EditorGUIUtility.singleLineHeight * 0.5f;
  129. buttonPosition.width -= EditorGUIUtility.singleLineHeight * 2.0f;
  130. buttonPosition.x += EditorGUIUtility.singleLineHeight;
  131. if (GUI.Button(buttonPosition, "Disable 'Optimize mesh data' option"))
  132. PlayerSettings.stripUnusedMeshComponents = false;
  133. drawingPosition.y += helpBoxPosition.height + EditorGUIUtility.singleLineHeight;
  134. }
  135. var shiftDrawingPosition = drawingPosition;
  136. shiftDrawingPosition.width /= 2;
  137. var parentRenderStyle = property.serializedObject.FindProperty("renderStyle");
  138. if (parentRenderStyle.intValue == (int)RenderStyle.Single)
  139. {
  140. EditorGUI.LabelField(shiftDrawingPosition, "Edge shift");
  141. shiftDrawingPosition.x += shiftDrawingPosition.width;
  142. Shift(ref shiftDrawingPosition, true);
  143. EditorGUI.PropertyField(shiftDrawingPosition, property.FindPropertyRelative("edgeDilateAmount"), GUIContent.none);
  144. }
  145. else
  146. {
  147. EditorGUIUtility.labelWidth = 80;
  148. Shift(ref shiftDrawingPosition, false);
  149. EditorGUI.PropertyField(shiftDrawingPosition, property.FindPropertyRelative("frontEdgeDilateAmount"), new GUIContent("Front dilate"));
  150. shiftDrawingPosition.x += shiftDrawingPosition.width;
  151. Shift(ref shiftDrawingPosition, true);
  152. EditorGUI.PropertyField(shiftDrawingPosition, property.FindPropertyRelative("backEdgeDilateAmount"), new GUIContent("Back dilate"));
  153. EditorGUIUtility.labelWidth = labelWidth;
  154. }
  155. }
  156. }
  157. drawingPosition.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  158. var linePosition = drawingPosition;
  159. linePosition.width /= 2;
  160. Shift(ref linePosition, false);
  161. var cullPosition = linePosition;
  162. cullPosition.width /= 2;
  163. EditorGUI.LabelField(cullPosition, new GUIContent("Cull mode"));
  164. cullPosition.x += cullPosition.width;
  165. EditorGUI.PropertyField(cullPosition, property.FindPropertyRelative("CullMode"), GUIContent.none);
  166. linePosition.x += linePosition.width;
  167. Shift(ref linePosition, true);
  168. var submeshIndex = property.FindPropertyRelative("SubmeshIndex");
  169. EditorGUIUtility.labelWidth = 90;
  170. EditorGUI.PropertyField(linePosition, submeshIndex);
  171. if (submeshIndex.intValue < 0)
  172. submeshIndex.intValue = 0;
  173. EditorGUIUtility.labelWidth = labelWidth;
  174. property.serializedObject.ApplyModifiedProperties();
  175. }
  176. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  177. {
  178. var renderer = property.FindPropertyRelative("renderer");
  179. var rendererReference = renderer.objectReferenceValue as Renderer;
  180. var textureNameProperty = property.FindPropertyRelative("cutoutTextureName");
  181. var usingCutout = !string.IsNullOrEmpty(textureNameProperty.stringValue);
  182. var appropriateToUseEdgeDilate = renderer.objectReferenceValue != null && !(renderer.objectReferenceValue as Renderer).gameObject.isStatic;
  183. var linesCount = renderer.objectReferenceValue == null ||
  184. renderer.objectReferenceValue != null && (renderer.objectReferenceValue as Renderer).gameObject.isStatic
  185. ? 3.0f : 4.0f;
  186. if (usingCutout || rendererReference is SpriteRenderer)
  187. linesCount += 1.0f;
  188. if (property.FindPropertyRelative("DilateRenderingMode").intValue == (int)DilateRenderMode.EdgeShift && appropriateToUseEdgeDilate)
  189. linesCount += 2.0f;
  190. if (property.FindPropertyRelative("BoundsMode").intValue == (int)BoundsMode.Manual)
  191. linesCount += 2.0f;
  192. float shift = 0.0f;
  193. var isDilateRenderingMode = property.FindPropertyRelative("DilateRenderingMode").intValue == (int)DilateRenderMode.EdgeShift;
  194. if (isDilateRenderingMode && PlayerSettings.stripUnusedMeshComponents)
  195. shift = EditorStyles.helpBox.CalcHeight(errorContent, lastWidth - 60) + EditorGUIUtility.singleLineHeight * 3.0f;
  196. return (EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight) * (linesCount + 0.5f) + shift;
  197. }
  198. }
  199. }