/// <summary> /// Ensure the singleton is initialized, return false if singleton has been initialized. /// </summary> /// <param name="callAwake"></param> /// <returns></returns> public bool EnsureInitialized(bool callAwake = true) { if (runtimeInstance != null || runtimeBehaviour != null) { return(false); } #if UNITY_EDITOR if (!Application.isPlaying) { throw new Exception("Singleton graph can only be run in playmode"); } #endif var type = GeneratedTypeName.ToType(false); if (type != null) { runtimeBehaviour = uNodeSingleton.GetNativeGraph(this); if (callAwake) { runtimeBehaviour.OnAwake(); //Call awake } } else { runtimeInstance = uNodeSingleton.GetRuntimeGraph(this); if (callAwake) { runtimeInstance.Initialize(); //Initialize singleton and call Awake function } } return(true); }
/// <summary> /// Initialize the graph, this also will call Awake function /// </summary> public void Initialize() { if (hasInitialize) { return; } if (Application.isPlaying) { hasInitialize = true; var type = GeneratedTypeName.ToType(false); if (type != null) { runtimeBehaviour = gameObject.AddComponent(type) as RuntimeBehaviour; for (int i = 0; i < Variables.Count; i++) { SetVariable(Variables[i].Name, variable[i].value); } var references = graphData.unityObjects; for (int i = 0; i < references.Count; i++) { SetVariable(references[i].name, references[i].value); } runtimeBehaviour.OnAwake(); return; } if (RootObject) { var nodes = RootObject.GetComponentsInChildren <NodeComponent>(); foreach (NodeComponent node in nodes) { if (node != null) { if (node is BaseEventNode) { (node as BaseEventNode).Initialize(); } node.RegisterPort(); } } } InitializeFunction(); var graph = this as IGraphWithUnityEvent; try { if (graph.onAwake != null) { graph.onAwake(); } } catch (System.Exception ex) { if (ex is uNodeException) { throw; } throw uNodeDebug.LogException(ex, this); } } }