GraphMaskDrawer.cs 1.2 KB

123456789101112131415161718192021222324252627282930
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace Pathfinding {
  4. [CustomPropertyDrawer(typeof(GraphMask))]
  5. public class GraphMaskDrawer : PropertyDrawer {
  6. string[] graphLabels = new string[32];
  7. public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
  8. // Make sure the AstarPath object is initialized and the graphs are loaded, this is required to be able to show graph names in the mask popup
  9. AstarPath.FindAstarPath();
  10. for (int i = 0; i < graphLabels.Length; i++) {
  11. if (AstarPath.active == null || AstarPath.active.data.graphs == null || i >= AstarPath.active.data.graphs.Length || AstarPath.active.data.graphs[i] == null) graphLabels[i] = "Graph " + i + (i == 31 ? "+" : "");
  12. else {
  13. graphLabels[i] = AstarPath.active.data.graphs[i].name + " (graph " + i + ")";
  14. }
  15. }
  16. EditorGUI.BeginChangeCheck();
  17. EditorGUI.showMixedValue = property.hasMultipleDifferentValues;
  18. var valueProp = property.FindPropertyRelative("value");
  19. int newVal = EditorGUI.MaskField(position, label, valueProp.intValue, graphLabels);
  20. if (EditorGUI.EndChangeCheck()) {
  21. valueProp.intValue = newVal;
  22. }
  23. EditorGUI.showMixedValue = false;
  24. }
  25. }
  26. }