LocalPropSetInspector.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Linq;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace DunGen.Editor
  5. {
  6. [CustomEditor(typeof(LocalPropSet))]
  7. [CanEditMultipleObjects]
  8. public class LocalPropSetInspector : UnityEditor.Editor
  9. {
  10. #region Labels
  11. private static class Label
  12. {
  13. public static readonly GUIContent CountMode = new GUIContent("Count Mode", "Determines how to choose the number of objects to spawn");
  14. public static readonly GUIContent PropCount = new GUIContent("Count", "Min & max number of props to spawn from this group");
  15. public static readonly GUIContent CountDepthCurve = new GUIContent("Count Depth Curve", "Depth curve, see help box above for details");
  16. public static readonly GUIContent Props = new GUIContent("Prop Weights");
  17. }
  18. #endregion
  19. private SerializedProperty countMode;
  20. private SerializedProperty propCount;
  21. private SerializedProperty countDepthCurve;
  22. private SerializedProperty props;
  23. private void OnEnable()
  24. {
  25. countMode = serializedObject.FindProperty("CountMode");
  26. propCount = serializedObject.FindProperty("PropCount");
  27. countDepthCurve = serializedObject.FindProperty("CountDepthCurve");
  28. props = serializedObject.FindProperty("Props");
  29. }
  30. public override void OnInspectorGUI()
  31. {
  32. var propSet = target as LocalPropSet;
  33. serializedObject.Update();
  34. EditorGUILayout.PropertyField(countMode, Label.CountMode);
  35. string countModeHelpText = "";
  36. switch (propSet.CountMode)
  37. {
  38. case LocalPropSetCountMode.Random:
  39. countModeHelpText = "A number of props will be chosen at random between the min & max count";
  40. break;
  41. case LocalPropSetCountMode.DepthBased:
  42. countModeHelpText = "A number of props will be chosen based on the current depth into the dungeon (read from the curve below). A value of zero on the graph will use the min count, a value of one will use the max count";
  43. break;
  44. case LocalPropSetCountMode.DepthMultiply:
  45. countModeHelpText = "A number of props will be chosen at random between the min & max count and then multiplied by the value read from the curve below";
  46. break;
  47. default:
  48. break;
  49. }
  50. EditorGUILayout.HelpBox(countModeHelpText, MessageType.Info);
  51. EditorGUILayout.PropertyField(propCount, Label.PropCount);
  52. if (propSet.CountMode == LocalPropSetCountMode.DepthBased || propSet.CountMode == LocalPropSetCountMode.DepthMultiply)
  53. EditorGUILayout.CurveField(countDepthCurve, Color.white, new Rect(0, 0, 1, 1), Label.CountDepthCurve);
  54. EditorGUILayout.Space();
  55. EditorGUILayout.PropertyField(props, Label.Props);
  56. HandlePropDragAndDrop(GUILayoutUtility.GetLastRect());
  57. if (GUILayout.Button("Add Selected Props"))
  58. {
  59. Undo.RecordObject(propSet, "Add Selected Props");
  60. foreach (var go in Selection.gameObjects)
  61. {
  62. if (!propSet.Props.ContainsGameObject(go))
  63. propSet.Props.Weights.Add(new GameObjectChance(go));
  64. }
  65. Undo.FlushUndoRecordObjects();
  66. }
  67. serializedObject.ApplyModifiedProperties();
  68. }
  69. private void HandlePropDragAndDrop(Rect dragTargetRect)
  70. {
  71. var evt = Event.current;
  72. if (evt.type == EventType.DragUpdated || evt.type == EventType.DragPerform)
  73. {
  74. var validGameObjects = EditorUtil.GetValidGameObjects(DragAndDrop.objectReferences, true, false);
  75. if (dragTargetRect.Contains(evt.mousePosition) && validGameObjects.Any())
  76. {
  77. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  78. if (evt.type == EventType.DragPerform)
  79. {
  80. Undo.RecordObject(target, "Drag Prop(s)");
  81. DragAndDrop.AcceptDrag();
  82. var propSet = target as LocalPropSet;
  83. foreach (var dragObject in validGameObjects)
  84. propSet.Props.Weights.Add(new GameObjectChance(dragObject));
  85. Undo.FlushUndoRecordObjects();
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }