Пример #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);
            }
        }
Пример #2
0
 /// <summary>
 /// Retarget node owner.
 /// </summary>
 /// <param name="fromOwner"></param>
 /// <param name="toOwner"></param>
 /// <param name="nodes"></param>
 public static void RetargetNodeOwner(uNodeRoot fromOwner, uNodeRoot toOwner, IList <MonoBehaviour> nodes, Action <object> valueAction = null)
 {
     Object[] from = new Object[] { fromOwner, fromOwner.transform, fromOwner.gameObject };
     Object[] to   = new Object[] { toOwner, toOwner.transform, toOwner.gameObject };
     foreach (var behavior in nodes)
     {
         if (behavior is Node)
         {
             Node node = behavior as Node;
             node.owner = toOwner;
         }
         else if (behavior is RootObject)
         {
             RootObject root = behavior as RootObject;
             root.owner = toOwner;
         }
         AnalizeObject(behavior, (obj) => {
             if (valueAction != null)
             {
                 valueAction(obj);
             }
             if (obj is MemberData)
             {
                 MemberData data = obj as MemberData;
                 data.RefactorUnityObject(from, to);
                 //return true;
             }
             return(false);
         }, (instance, field, type, value) => {
             Object o = value as Object;
             if (o)
             {
                 if (o == fromOwner)
                 {
                     field.SetValueOptimized(instance, toOwner);
                 }
                 else if (o == from[1])
                 {
                     field.SetValueOptimized(instance, to[1]);
                 }
                 else if (o == from[2])
                 {
                     field.SetValueOptimized(instance, to[2]);
                 }
             }
         });
     }
 }
Пример #3
0
        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;
            }
        }
Пример #4
0
        static RuntimeGraphData GetCachedGraph(uNodeRoot targetGraph)
        {
            RuntimeGraphData graphData;

            if (!runtimeGraphMap.TryGetValue(targetGraph, out graphData))
            {
                uNodeRoot graph = Object.Instantiate(targetGraph);         //Begin instantiate original graph
                graph.gameObject.name = targetGraph.gameObject.name;       //Ensure the cached graph name is same with the original graph
#if !UNODE_DEBUG
                graph.gameObject.hideFlags = HideFlags.HideInHierarchy;    //Ensure the cached graph doesn't show in the hierarchy
#endif
                Object.DontDestroyOnLoad(graph.gameObject);                //Ensure this cached graph is cached across scenes
                graphData = new RuntimeGraphData()
                {
                    graph          = graph,
                    originalGraph  = targetGraph,
                    retargetAction = AnalizerUtility.GetRetargetNodeOwnerAction(graph, graph.RootObject.GetComponentsInChildren <MonoBehaviour>(true)),
                };
                runtimeGraphMap[targetGraph] = graphData;
            }
            return(graphData);
        }
Пример #5
0
        /// <summary>
        /// Get retarget node Action
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="nodes"></param>
        /// <returns></returns>
        public static Action <uNodeRoot> GetRetargetNodeOwnerAction(uNodeRoot graph, IList <MonoBehaviour> nodes)
        {
            Action <uNodeRoot> action = null;
            var fromTR = graph.transform;
            var fromGO = graph.gameObject;
            var from   = new Object[] { graph, fromTR, fromGO };

            foreach (var behavior in nodes)
            {
                if (behavior is Node)
                {
                    Node node = behavior as Node;
                    action += (owner) => {
                        node.owner = owner;
                    };
                }
                else if (behavior is RootObject)
                {
                    RootObject root = behavior as RootObject;
                    action += (owner) => {
                        root.owner = owner;
                    };
                }
                AnalizeObject(behavior, (obj) => {
                    if (obj is MemberData)
                    {
                        MemberData data = obj as MemberData;
                        if (data.instanceReference != null)
                        {
                            for (int i = 0; i < data.instanceReference.Count; i++)
                            {
                                Object o  = data.instanceReference[i];
                                int index = i;
                                if (o == graph)
                                {
                                    action += (owner) => {
                                        data.instanceReference[index] = owner;
                                    };
                                }
                                else if (o == fromTR)
                                {
                                    action += (owner) => {
                                        data.instanceReference[index] = owner.transform;
                                    };
                                }
                                else if (o == fromGO)
                                {
                                    action += (owner) => {
                                        data.instanceReference[index] = owner.gameObject;
                                    };
                                }
                            }
                        }
                        if (data.targetReference != null)
                        {
                            for (int i = 0; i < data.targetReference.Count; i++)
                            {
                                Object o  = data.targetReference[i];
                                int index = i;
                                if (o == graph)
                                {
                                    action += (owner) => {
                                        data.targetReference[index] = owner;
                                    };
                                }
                                else if (o == fromTR)
                                {
                                    action += (owner) => {
                                        data.targetReference[index] = owner.transform;
                                    };
                                }
                                else if (o == fromGO)
                                {
                                    action += (owner) => {
                                        data.targetReference[index] = owner.gameObject;
                                    };
                                }
                            }
                        }
                        if (data.typeReference != null)
                        {
                            for (int i = 0; i < data.typeReference.Count; i++)
                            {
                                Object o  = data.typeReference[i];
                                int index = i;
                                if (o == graph)
                                {
                                    action += (owner) => {
                                        data.typeReference[index] = owner;
                                    };
                                }
                                else if (o == fromTR)
                                {
                                    action += (owner) => {
                                        data.typeReference[index] = owner.transform;
                                    };
                                }
                                else if (o == fromGO)
                                {
                                    action += (owner) => {
                                        data.typeReference[index] = owner.gameObject;
                                    };
                                }
                            }
                        }
                        if (data.odinTargetData.references != null)
                        {
                            for (int i = 0; i < data.odinTargetData.references.Count; i++)
                            {
                                Object o  = data.odinTargetData.references[i];
                                int index = i;
                                if (o == graph)
                                {
                                    action += (owner) => {
                                        data.odinTargetData.references[index] = owner;
                                    };
                                }
                                else if (o == fromTR)
                                {
                                    action += (owner) => {
                                        data.odinTargetData.references[index] = owner.transform;
                                    };
                                }
                                else if (o == fromGO)
                                {
                                    action += (owner) => {
                                        data.odinTargetData.references[index] = owner.gameObject;
                                    };
                                }
                            }
                        }
                        if (data.odinInstanceData.references != null)
                        {
                            for (int i = 0; i < data.odinInstanceData.references.Count; i++)
                            {
                                Object o  = data.odinInstanceData.references[i];
                                int index = i;
                                if (o == graph)
                                {
                                    action += (owner) => {
                                        data.odinInstanceData.references[index] = owner;
                                    };
                                }
                                else if (o == fromTR)
                                {
                                    action += (owner) => {
                                        data.odinInstanceData.references[index] = owner.transform;
                                    };
                                }
                                else if (o == fromGO)
                                {
                                    action += (owner) => {
                                        data.odinInstanceData.references[index] = owner.gameObject;
                                    };
                                }
                            }
                        }
                        //if(data.HasUnityReference(from)) {
                        //	var act = data.GetActionForRefactorUnityObject(from);
                        //	action += (owner) => {
                        //		act(new Object[] { owner, owner.transform, owner.gameObject });
                        //	};
                        //}
                        //return true;
                    }
                    return(false);
                }, (instance, field, type, value) => {
                    Object o = value as Object;
                    if (o)
                    {
                        if (o == graph)
                        {
                            action += (owner) => {
                                field.SetValueOptimized(instance, owner);
                            };
                        }
                        else if (o == fromTR)
                        {
                            action += (owner) => {
                                field.SetValueOptimized(instance, owner.transform);
                            };
                        }
                        else if (o == fromGO)
                        {
                            action += (owner) => {
                                field.SetValueOptimized(instance, owner.gameObject);
                            };
                        }
                    }
                });
            }
            return(action);
        }
Пример #6
0
 public RuntimeGraphType(uNodeRoot target)
 {
     this.target = target;
 }