NodeGraphImporter.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEditor.Experimental.AssetImporters;
  6. using UnityEngine;
  7. using XNode;
  8. namespace XNodeEditor {
  9. /// <summary> Deals with modified assets </summary>
  10. class NodeGraphImporter : AssetPostprocessor {
  11. private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) {
  12. foreach (string path in importedAssets) {
  13. // Skip processing anything without the .asset extension
  14. if (Path.GetExtension(path) != ".asset") continue;
  15. // Get the object that is requested for deletion
  16. NodeGraph graph = AssetDatabase.LoadAssetAtPath<NodeGraph>(path);
  17. if (graph == null) continue;
  18. // Get attributes
  19. Type graphType = graph.GetType();
  20. NodeGraph.RequireNodeAttribute[] attribs = Array.ConvertAll(
  21. graphType.GetCustomAttributes(typeof(NodeGraph.RequireNodeAttribute), true), x => x as NodeGraph.RequireNodeAttribute);
  22. Vector2 position = Vector2.zero;
  23. foreach (NodeGraph.RequireNodeAttribute attrib in attribs) {
  24. if (attrib.type0 != null) AddRequired(graph, attrib.type0, ref position);
  25. if (attrib.type1 != null) AddRequired(graph, attrib.type1, ref position);
  26. if (attrib.type2 != null) AddRequired(graph, attrib.type2, ref position);
  27. }
  28. }
  29. }
  30. private static void AddRequired(NodeGraph graph, Type type, ref Vector2 position) {
  31. if (!graph.nodes.Any(x => x.GetType() == type)) {
  32. XNode.Node node = graph.AddNode(type);
  33. node.position = position;
  34. position.x += 200;
  35. if (node.name == null || node.name.Trim() == "") node.name = NodeEditorUtilities.NodeDefaultName(type);
  36. if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(graph))) AssetDatabase.AddObjectToAsset(node, graph);
  37. }
  38. }
  39. }
  40. }