Beispiel #1
0
        public static SerializedGraph SerializeGraph(uNode.uNodeRoot root)
        {
            SerializedGraph  graph      = new SerializedGraph();
            List <Transform> transforms = new List <Transform>();
            {
                if (root.RootObject != null)
                {
                    var children = root.RootObject.GetComponentsInChildren <Transform>(true);
                    transforms.AddRange(children);
                }
                graph.graphType = new MemberData(root.GetType(), MemberData.TargetType.Type);
            }
            var graphRoot = Serialize(root.gameObject, new Component[] { root }, transforms);

            if (root as uNode.uNodeBase && (root as uNode.uNodeBase).NestedClass)             //Handle nested types.
            {
                transforms = new List <Transform>();
                List <Component> roots = new List <Component>();
                var comps = (root as uNode.uNodeBase).NestedClass.GetComponents <Component>();
                foreach (var c in comps)
                {
                    if (c is uNode.uNodeRoot || c is uNode.uNodeData || !(c is MonoBehaviour))
                    {
                        roots.Add(c);
                    }
                }
                foreach (var r in roots)
                {
                    if (r is uNode.uNodeRoot)
                    {
                        var nodeRoot = r as uNode.uNodeRoot;
                        if (nodeRoot.RootObject != null)
                        {
                            var children = nodeRoot.RootObject.GetComponentsInChildren <Transform>(true);
                            transforms.AddRange(children);
                        }
                    }
                }
                var nestedTypes = Serialize((root as uNode.uNodeBase).NestedClass.gameObject, roots, transforms);
                graph.nestedTypes = nestedTypes;
            }
            {
                graph.serializedGraph = graphRoot;
                graph.variables       = root.Variables.ToArray();
            }
            return(graph);
        }
Beispiel #2
0
        public static uNode.uNodeRoot DeserializeGraph(SerializedGraph graph, GameObject gameObject, IList <uNode.VariableData> variables = null, bool includeNestedGraph = true)
        {
            var originalComp = gameObject.GetComponents <uNode.uNodeRoot>();

            Deserialize(graph.serializedGraph, gameObject);
            var components = gameObject.GetComponents <uNode.uNodeRoot>();

            uNode.uNodeRoot addedRoot = null;
            foreach (var comp in components)
            {
                if (!originalComp.Contains(comp))
                {
                    addedRoot = comp;
                    break;
                }
            }
            if (includeNestedGraph && graph.nestedTypes != null && graph.nestedTypes.serializedObjects.Count > 0)
            {
                GameObject nestedGraph = new GameObject("NestedGraph");
                nestedGraph.transform.SetParent(gameObject.transform);
                Deserialize(graph.nestedTypes, nestedGraph);
                if (addedRoot as uNode.uNodeBase)
                {
                    (addedRoot as uNode.uNodeBase).NestedClass = nestedGraph.GetComponent <uNode.uNodeData>();
                }
            }
            if (variables != null && addedRoot != null)
            {
                //Initialize variable values.
                foreach (var v in addedRoot.Variables)
                {
                    foreach (var lv in variables)
                    {
                        if (v.Name == lv.Name && v.Type == lv.Type)
                        {
                            v.value = lv.value;
                            break;
                        }
                    }
                }
            }
            return(addedRoot);
        }