SceneGraphEditor.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using XNode;
  7. namespace XNodeEditor {
  8. [CustomEditor(typeof(SceneGraph), true)]
  9. public class SceneGraphEditor : Editor {
  10. private SceneGraph sceneGraph;
  11. private bool removeSafely;
  12. private Type graphType;
  13. public override void OnInspectorGUI() {
  14. if (sceneGraph.graph == null) {
  15. if (GUILayout.Button("New graph", GUILayout.Height(40))) {
  16. if (graphType == null) {
  17. Type[] graphTypes = NodeEditorReflection.GetDerivedTypes(typeof(NodeGraph));
  18. GenericMenu menu = new GenericMenu();
  19. for (int i = 0; i < graphTypes.Length; i++) {
  20. Type graphType = graphTypes[i];
  21. menu.AddItem(new GUIContent(graphType.Name), false, () => CreateGraph(graphType));
  22. }
  23. menu.ShowAsContext();
  24. } else {
  25. CreateGraph(graphType);
  26. }
  27. }
  28. } else {
  29. if (GUILayout.Button("Open graph", GUILayout.Height(40))) {
  30. NodeEditorWindow.Open(sceneGraph.graph);
  31. }
  32. if (removeSafely) {
  33. GUILayout.BeginHorizontal();
  34. GUILayout.Label("Really remove graph?");
  35. GUI.color = new Color(1, 0.8f, 0.8f);
  36. if (GUILayout.Button("Remove")) {
  37. removeSafely = false;
  38. Undo.RecordObject(sceneGraph, "Removed graph");
  39. sceneGraph.graph = null;
  40. }
  41. GUI.color = Color.white;
  42. if (GUILayout.Button("Cancel")) {
  43. removeSafely = false;
  44. }
  45. GUILayout.EndHorizontal();
  46. } else {
  47. GUI.color = new Color(1, 0.8f, 0.8f);
  48. if (GUILayout.Button("Remove graph")) {
  49. removeSafely = true;
  50. }
  51. GUI.color = Color.white;
  52. }
  53. }
  54. }
  55. private void OnEnable() {
  56. sceneGraph = target as SceneGraph;
  57. Type sceneGraphType = sceneGraph.GetType();
  58. if (sceneGraphType == typeof(SceneGraph)) {
  59. graphType = null;
  60. } else {
  61. Type baseType = sceneGraphType.BaseType;
  62. if (baseType.IsGenericType) {
  63. graphType = sceneGraphType = baseType.GetGenericArguments() [0];
  64. }
  65. }
  66. }
  67. public void CreateGraph(Type type) {
  68. Undo.RecordObject(sceneGraph, "Create graph");
  69. sceneGraph.graph = ScriptableObject.CreateInstance(type) as NodeGraph;
  70. sceneGraph.graph.name = sceneGraph.name + "-graph";
  71. }
  72. }
  73. }