Example #1
0
        public void Initialize()
        {
            if (!Application.isPlaying)
            {
                throw new System.Exception("Can't initialize when the aplication is not playing.");
            }
            if (target == null)
            {
                throw new System.Exception("Target graph can't be null");
            }
            var type = target.GeneratedTypeName.ToType(false);

            if (type != null)
            {
                runtimeAsset = ScriptableObject.CreateInstance(type) as RuntimeAsset;
                for (int i = 0; i < target.Variables.Count; i++)
                {
                    VariableData var = target.Variables[i];
                    for (int x = 0; x < variable.Count; x++)
                    {
                        if (var.Name.Equals(variable[x].Name))
                        {
                            SetVariable(var.Name, variable[x].value);
                        }
                    }
                }
            }
            else
            {
                var mainObject = new GameObject(name);
                mainObject.SetActive(false);

                uNodeRoot    graph = Instantiate(target);
                uNodeRuntime main  = mainObject.AddComponent <uNodeRuntime>();
                main.originalGraph = target;
                main.Name          = target.GraphName;
                main.Variables     = graph.Variables;
                main.RootObject    = graph.RootObject;
                main.RootObject.transform.SetParent(mainObject.transform);
                AnalizerUtility.RetargetNodeOwner(graph, main, main.RootObject.GetComponentsInChildren <MonoBehaviour>(true));
                main.Refresh();
                Destroy(graph.gameObject);
                for (int i = 0; i < main.Variables.Count; i++)
                {
                    VariableData var = main.Variables[i];
                    for (int x = 0; x < variable.Count; x++)
                    {
                        if (var.Name.Equals(variable[x].Name))
                        {
                            main.Variables[i] = new VariableData(variable[x]);
                        }
                    }
                }
                //Uncomment this to prevent resetting variable you're exiting play mode
                // this.variable = main.variable;
                this.runtimeInstance = main;
                GameObject.DontDestroyOnLoad(mainObject);
            }
        }
        public static uNodeRuntime InstantiateGraph(uNodeRoot targetGraph, GameObject destination, IList <VariableData> variables)
        {
            try {
                var          data = GetCachedGraph(targetGraph);
                uNodeRuntime main = destination.AddComponent <uNodeRuntime>();
                data.retargetAction?.Invoke(main);               //This will retarget the node owner to the new owner
                var graph = Object.Instantiate(data.graph);      //Instantiate the cached graph
                main.originalGraph = targetGraph;                //Assign the original graph
                main.Name          = targetGraph.GraphName;      //This will ensure the uid is valid
                var defaultVariable = graph.Variables;
                main.RootObject = graph.RootObject;
                main.RootObject.transform.SetParent(destination.transform);
                Object.Destroy(graph.gameObject);           //Clean up cached graph
                main.Refresh();                             //Refresh the graph so it has up to date data
#if !UNODE_DEBUG_PLUS
                main.hideFlags = HideFlags.HideInInspector; //Ensure the graph doesn't show up in the inspector
#else
                main.RootObject.hideFlags = HideFlags.None;
#endif
                //Initialize variable values
                main.Variables = defaultVariable;      //This is for set variable value to be same with the graph
                if (variables != null)                 //This is for set variable value to same with overridden variable in instanced graph
                {
                    for (int i = 0; i < main.Variables.Count; i++)
                    {
                        VariableData var = main.Variables[i];
                        for (int x = 0; x < variables.Count; x++)
                        {
                            if (var.Name.Equals(variables[x].Name))
                            {
                                main.Variables[i] = variables[x];
                            }
                        }
                    }
                }
                return(main);
            }
            catch (Exception ex) {
                Debug.LogError($"Error on trying to initialize graph: {targetGraph.GraphName} to the {destination}.\nError: {ex.ToString()}", targetGraph);
                throw;
            }
        }
Example #3
0
        public void EnsureInitialized()
        {
            if (hasInitialize)
            {
                return;
            }
            if (!Application.isPlaying)
            {
                throw new System.Exception("Can't initialize event when not playing.");
            }
            else if (target == null)
            {
                throw new Exception("Target graph can't be null");
            }
            hasInitialize = true;
            if (target is uNodeComponentSingleton singleton)
            {
                bool flag = singleton.EnsureInitialized(false);
                runtimeBehaviour = singleton.runtimeBehaviour;
                if (flag && runtimeBehaviour != null)
                {
                    //Initialize the variable
                    for (int i = 0; i < target.Variables.Count; i++)
                    {
                        VariableData var = target.Variables[i];
                        for (int x = 0; x < variable.Count; x++)
                        {
                            if (var.Name.Equals(variable[x].Name))
                            {
                                var = variable[x];
                                break;
                            }
                        }
                        SetVariable(var.Name, SerializerUtility.Duplicate(var.value));
                    }
                    runtimeBehaviour.OnAwake();
                }
                runtimeInstance = singleton.runtimeInstance;
                if (flag && runtimeInstance != null)
                {
                    //Initialize the variable
                    for (int i = 0; i < target.Variables.Count; i++)
                    {
                        VariableData var = target.Variables[i];
                        for (int x = 0; x < variable.Count; x++)
                        {
                            if (var.Name.Equals(variable[x].Name))
                            {
                                var = variable[x];
                                break;
                            }
                        }
                        SetVariable(var.Name, SerializerUtility.Duplicate(var.value));
                    }
                    variable = runtimeInstance.Variables;
                    runtimeInstance.Initialize();
                }
                return;
            }
            var type = target.GeneratedTypeName.ToType(false);

            if (type != null)
            {
                //Instance native c# graph, native graph will call Awake immediately
                runtimeBehaviour           = gameObject.AddComponent(type) as RuntimeBehaviour;
                runtimeBehaviour.hideFlags = HideFlags.HideInInspector;
                //Initialize the references
                var references = target.graphData.unityObjects;
                for (int i = 0; i < references.Count; i++)
                {
                    SetVariable(references[i].name, references[i].value);
                }
                //Initialize the variable
                for (int i = 0; i < target.Variables.Count; i++)
                {
                    VariableData var = target.Variables[i];
                    for (int x = 0; x < variable.Count; x++)
                    {
                        if (var.Name.Equals(variable[x].Name))
                        {
                            var = variable[x];
                            break;
                        }
                    }
                    SetVariable(var.Name, SerializerUtility.Duplicate(var.value));
                }
                //Call awake
                runtimeBehaviour.OnAwake();
            }
            else
            {
                //Instance reflection graph
                var main = RuntimeGraphManager.InstantiateGraph(target, gameObject, variable);
                variable        = main.Variables;
                runtimeInstance = main;
                main.Initialize();                //Initialize the graph, so it will call Awake after created.
                main.manualHandlingEvent = true;
            }
        }