GraphRenameFixAssetProcessor.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using UnityEditor;
  2. using XNode;
  3. namespace XNodeEditor {
  4. /// <summary>
  5. /// This asset processor resolves an issue with the new v2 AssetDatabase system present on 2019.3 and later. When
  6. /// renaming a <see cref="XNode.NodeGraph"/> asset, it appears that sometimes the v2 AssetDatabase will swap which asset
  7. /// is the main asset (present at top level) between the <see cref="XNode.NodeGraph"/> and one of its <see cref="XNode.Node"/>
  8. /// sub-assets. As a workaround until Unity fixes this, this asset processor checks all renamed assets and if it
  9. /// finds a case where a <see cref="XNode.Node"/> has been made the main asset it will swap it back to being a sub-asset
  10. /// and rename the node to the default name for that node type.
  11. /// </summary>
  12. internal sealed class GraphRenameFixAssetProcessor : AssetPostprocessor {
  13. private static void OnPostprocessAllAssets(
  14. string[] importedAssets,
  15. string[] deletedAssets,
  16. string[] movedAssets,
  17. string[] movedFromAssetPaths) {
  18. for (int i = 0; i < movedAssets.Length; i++) {
  19. Node nodeAsset = AssetDatabase.LoadMainAssetAtPath(movedAssets[i]) as Node;
  20. // If the renamed asset is a node graph, but the v2 AssetDatabase has swapped a sub-asset node to be its
  21. // main asset, reset the node graph to be the main asset and rename the node asset back to its default
  22. // name.
  23. if (nodeAsset != null && AssetDatabase.IsMainAsset(nodeAsset)) {
  24. AssetDatabase.SetMainObject(nodeAsset.graph, movedAssets[i]);
  25. AssetDatabase.ImportAsset(movedAssets[i]);
  26. nodeAsset.name = NodeEditorUtilities.NodeDefaultName(nodeAsset.GetType());
  27. EditorUtility.SetDirty(nodeAsset);
  28. }
  29. }
  30. }
  31. }
  32. }