From 3d5c5a216054ca4da4546cedfaa3a0239be0dc5f Mon Sep 17 00:00:00 2001 From: Jeff Campbell Date: Sat, 7 Mar 2020 21:37:16 +0100 Subject: [PATCH] Added fix for rename bug with v2 AssetDatabase * Added fix for NodeGraph rename bug with v2 AssetDatabase where renaming a graph asset can sometimes result in it being swapped as the main asset with one of the node sub assets. --- .../Editor/GraphRenameFixAssetProcessor.cs | 40 +++++++++++++++++++ .../GraphRenameFixAssetProcessor.cs.meta | 11 +++++ 2 files changed, 51 insertions(+) create mode 100644 Scripts/Editor/GraphRenameFixAssetProcessor.cs create mode 100644 Scripts/Editor/GraphRenameFixAssetProcessor.cs.meta diff --git a/Scripts/Editor/GraphRenameFixAssetProcessor.cs b/Scripts/Editor/GraphRenameFixAssetProcessor.cs new file mode 100644 index 0000000..4cfe3d6 --- /dev/null +++ b/Scripts/Editor/GraphRenameFixAssetProcessor.cs @@ -0,0 +1,40 @@ +using UnityEditor; +using XNode; + +namespace XNodeEditor +{ + /// + /// This asset processor resolves an issue with the new v2 AssetDatabase system present on 2019.3 and later. When + /// renaming a asset, it appears that sometimes the v2 AssetDatabase will swap which asset + /// is the main asset (present at top level) between the and one of its + /// sub-assets. As a workaround until Unity fixes this, this asset processor checks all renamed assets and if it + /// finds a case where a has been made the main asset it will swap it back to being a sub-asset + /// and rename the node to the default name for that node type. + /// + internal sealed class GraphRenameFixAssetProcessor : AssetPostprocessor + { + private static void OnPostprocessAllAssets( + string[] importedAssets, + string[] deletedAssets, + string[] movedAssets, + string[] movedFromAssetPaths) + { + for (int i = 0; i < movedAssets.Length; i++) + { + Node nodeAsset = AssetDatabase.LoadMainAssetAtPath(movedAssets[i]) as Node; + + // If the renamed asset is a node graph, but the v2 AssetDatabase has swapped a sub-asset node to be its + // main asset, reset the node graph to be the main asset and rename the node asset back to its default + // name. + if (nodeAsset != null && AssetDatabase.IsMainAsset(nodeAsset)) + { + AssetDatabase.SetMainObject(nodeAsset.graph, movedAssets[i]); + AssetDatabase.ImportAsset(movedAssets[i]); + + nodeAsset.name = NodeEditorUtilities.NodeDefaultName(nodeAsset.GetType()); + EditorUtility.SetDirty(nodeAsset); + } + } + } + } +} diff --git a/Scripts/Editor/GraphRenameFixAssetProcessor.cs.meta b/Scripts/Editor/GraphRenameFixAssetProcessor.cs.meta new file mode 100644 index 0000000..77e87ee --- /dev/null +++ b/Scripts/Editor/GraphRenameFixAssetProcessor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 65da1ff1c50a9984a9c95fd18799e8dd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: