summaryrefslogtreecommitdiff
path: root/Other/NodeEditorExamples/Assets/xNode-examples/Scripts/Editor/GraphRenameFixAssetProcessor.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2022-06-28 09:40:37 +0800
committerchai <chaifix@163.com>2022-06-28 09:40:37 +0800
commit49b25e755b70ec412feaaf0b898d6f7e09d2bea6 (patch)
tree3c5f4260f30d1c2d7196db93153700d7ddec3157 /Other/NodeEditorExamples/Assets/xNode-examples/Scripts/Editor/GraphRenameFixAssetProcessor.cs
parentc92269331692feca2c276649f6c4ee8911f1f859 (diff)
+node example
Diffstat (limited to 'Other/NodeEditorExamples/Assets/xNode-examples/Scripts/Editor/GraphRenameFixAssetProcessor.cs')
-rw-r--r--Other/NodeEditorExamples/Assets/xNode-examples/Scripts/Editor/GraphRenameFixAssetProcessor.cs35
1 files changed, 35 insertions, 0 deletions
diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Scripts/Editor/GraphRenameFixAssetProcessor.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Scripts/Editor/GraphRenameFixAssetProcessor.cs
new file mode 100644
index 00000000..264e8b12
--- /dev/null
+++ b/Other/NodeEditorExamples/Assets/xNode-examples/Scripts/Editor/GraphRenameFixAssetProcessor.cs
@@ -0,0 +1,35 @@
+using UnityEditor;
+using XNode;
+
+namespace XNodeEditor {
+ /// <summary>
+ /// This asset processor resolves an issue with the new v2 AssetDatabase system present on 2019.3 and later. When
+ /// renaming a <see cref="XNode.NodeGraph"/> asset, it appears that sometimes the v2 AssetDatabase will swap which asset
+ /// is the main asset (present at top level) between the <see cref="XNode.NodeGraph"/> and one of its <see cref="XNode.Node"/>
+ /// sub-assets. As a workaround until Unity fixes this, this asset processor checks all renamed assets and if it
+ /// 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
+ /// and rename the node to the default name for that node type.
+ /// </summary>
+ 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);
+ }
+ }
+ }
+ }
+} \ No newline at end of file