From 9e7e7c4c9b90360225256a7f8668edb5ca6568b8 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Tue, 7 May 2019 17:02:25 +0200 Subject: [PATCH] UPGRADE NOTICE: Removed Init. Use OnEnable instead OnEnable is a built-in event in Unity, and as such you don't need to override anything. Simply replace 'protected override void Init()' with 'private void OnEnable()'. You can use any access modifier. --- Scripts/Editor/NodeEditorReflection.cs | 19 +++++++++++++++++++ .../ScriptTemplates/xNode_NodeTemplate.cs.txt | 5 ++--- Scripts/Node.cs | 7 +------ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Scripts/Editor/NodeEditorReflection.cs b/Scripts/Editor/NodeEditorReflection.cs index 18b72fa..6f7813d 100644 --- a/Scripts/Editor/NodeEditorReflection.cs +++ b/Scripts/Editor/NodeEditorReflection.cs @@ -34,6 +34,25 @@ namespace XNodeEditor { } private Func _isDocked; + /// + /// This method is called automatically on script reload, and ensures that + /// all static ports in all nodes match their Node's class' port attributes + /// + [InitializeOnLoadMethod] + private static void UpdateAllStaticNodePorts() { + // Find all Graphs in the project + string[] guids = AssetDatabase.FindAssets("t:" + typeof(XNode.NodeGraph).Name); + for (int i = 0; i < guids.Length; i++) { + string assetpath = AssetDatabase.GUIDToAssetPath(guids[i]); + UnityEngine.Object[] objs = AssetDatabase.LoadAllAssetRepresentationsAtPath(assetpath); + // Loop through graph asset and search for nodes (nodes exist inside the graph asset as sub-assets) + for (int k = 0; k < objs.Length; k++) { + XNode.Node node = objs[k] as XNode.Node; + if (node != null) node.UpdateStaticPorts(); + } + } + } + public static Type[] GetNodeTypes() { //Get all classes deriving from Node via reflection return GetDerivedTypes(typeof(XNode.Node)); diff --git a/Scripts/Editor/Resources/ScriptTemplates/xNode_NodeTemplate.cs.txt b/Scripts/Editor/Resources/ScriptTemplates/xNode_NodeTemplate.cs.txt index de791fc..144d9e1 100644 --- a/Scripts/Editor/Resources/ScriptTemplates/xNode_NodeTemplate.cs.txt +++ b/Scripts/Editor/Resources/ScriptTemplates/xNode_NodeTemplate.cs.txt @@ -5,9 +5,8 @@ using XNode; public class #SCRIPTNAME# : Node { - // Use this for initialization - protected override void Init() { - base.Init(); + // Use OnEnable for initialization + private void OnEnable() { #NOTRIM# } diff --git a/Scripts/Node.cs b/Scripts/Node.cs index cd86b95..a16a234 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -114,11 +114,9 @@ namespace XNode { /// Used during node instantiation to fix null/misconfigured graph during OnEnable/Init. Set it before instantiating a node. Will automatically be unset during OnEnable public static NodeGraph graphHotfix; - protected void OnEnable() { + private void Awake() { if (graphHotfix != null) graph = graphHotfix; graphHotfix = null; - UpdateStaticPorts(); - Init(); } /// Update static ports to reflect class fields. This happens automatically on enable. @@ -126,9 +124,6 @@ namespace XNode { NodeDataCache.UpdatePorts(this, ports); } - /// Initialize node. Called on enable. - protected virtual void Init() { } - /// Checks all connections for invalid references, and removes them. public void VerifyConnections() { foreach (NodePort port in Ports) port.VerifyConnections();