GameObjectChanceDrawer.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.Linq;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace DunGen.Editor.Drawers
  5. {
  6. [CustomPropertyDrawer(typeof(GameObjectChance))]
  7. sealed class GameObjectChanceDrawer : PropertyDrawer
  8. {
  9. #region Labels
  10. private static class Label
  11. {
  12. public static readonly GUIContent WeightsFoldout = new GUIContent("Weights");
  13. public static readonly GUIContent MainPathWeight = new GUIContent("Main Path");
  14. public static readonly GUIContent BranchPathWeight = new GUIContent("Branch Path");
  15. public static readonly GUIContent DepthWeightScale = new GUIContent("Depth Scale");
  16. }
  17. #endregion
  18. public static GameObjectFilter? FilterOverride = GameObjectFilter.All;
  19. public const float Padding = 5;
  20. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  21. {
  22. var mainPathWeightProperty = property.FindPropertyRelative("MainPathWeight");
  23. float height = EditorGUIUtility.singleLineHeight;
  24. height *= (mainPathWeightProperty.isExpanded) ? 5 : 2;
  25. height += Padding * 2;
  26. return height;
  27. }
  28. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  29. {
  30. var filter = FilterOverride ?? GameObjectFilter.All;
  31. var attribute = fieldInfo.GetCustomAttributes(typeof(AcceptGameObjectTypesAttribute), true)
  32. .Cast<AcceptGameObjectTypesAttribute>()
  33. .FirstOrDefault();
  34. if (attribute != null)
  35. filter = attribute.Filter;
  36. bool allowSceneObjects = (filter & GameObjectFilter.Scene) == GameObjectFilter.Scene;
  37. bool allowAssets = (filter & GameObjectFilter.Asset) == GameObjectFilter.Asset;
  38. // Find properties
  39. var valueProperty = property.FindPropertyRelative("Value");
  40. var mainPathWeightProperty = property.FindPropertyRelative("MainPathWeight");
  41. var branchPathWeightProperty = property.FindPropertyRelative("BranchPathWeight");
  42. var depthWeightScaleProperty = property.FindPropertyRelative("DepthWeightScale");
  43. EditorGUI.BeginProperty(position, label, property);
  44. position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
  45. GUI.Box(position, GUIContent.none);
  46. var controlRect = new Rect(position.x + Padding, position.y + Padding, position.width - (Padding * 2), EditorGUIUtility.singleLineHeight);
  47. EditorUtil.ObjectField(controlRect, valueProperty, GUIContent.none, typeof(GameObject), allowSceneObjects, allowAssets);
  48. MoveRectToNextLine(ref controlRect);
  49. float foldoutOffset = EditorStyles.foldout.padding.left;
  50. var foldoutRect = new Rect(controlRect.x + foldoutOffset, controlRect.y, controlRect.width - foldoutOffset, controlRect.height);
  51. mainPathWeightProperty.isExpanded = EditorGUI.Foldout(foldoutRect, mainPathWeightProperty.isExpanded, "Weights", true);
  52. MoveRectToNextLine(ref controlRect);
  53. if (mainPathWeightProperty.isExpanded)
  54. {
  55. EditorGUI.PropertyField(controlRect, mainPathWeightProperty, Label.MainPathWeight);
  56. MoveRectToNextLine(ref controlRect);
  57. EditorGUI.PropertyField(controlRect, branchPathWeightProperty, Label.BranchPathWeight);
  58. MoveRectToNextLine(ref controlRect);
  59. EditorGUI.CurveField(controlRect, depthWeightScaleProperty, Color.white, new Rect(0, 0, 1, 1), Label.DepthWeightScale);
  60. }
  61. EditorGUI.EndProperty();
  62. }
  63. private void MoveRectToNextLine(ref Rect controlRect)
  64. {
  65. controlRect = new Rect(controlRect.x, controlRect.y + EditorGUIUtility.singleLineHeight, controlRect.width, controlRect.height);
  66. }
  67. }
  68. }