Esempio n. 1
0
        /// <summary>
        /// Deserialize a previously serialized game object.
        /// </summary>

        static void DeserializeComponents(this GameObject go, DataNode root)
        {
            DataNode scriptNode = root.GetChild("Components");

            if (scriptNode == null)
            {
                return;
            }

            for (int i = 0; i < scriptNode.children.size; ++i)
            {
                DataNode    node = scriptNode.children[i];
                System.Type type = UnityTools.GetType(node.name);

                if (type != null && type.IsSubclassOf(typeof(Component)))
                {
                    Component comp = go.GetComponent(type);
                    if (comp == null)
                    {
                        comp = go.AddComponent(type);
                    }
                    comp.Deserialize(node);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Convert a serialized string reference to an actual object reference.
        /// </summary>

        static public UnityEngine.Object StringToReference(this GameObject go, string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }
            var split = path.Split(new char[] { '|' }, 3);

            if (split.Length == 3)
            {
                var myType = UnityTools.GetType(split[1]);

                if (myType != null)
                {
                    if (myType == typeof(Shader))
                    {
                        return(Shader.Find(split[2]));
                    }
                    else if (split[0] == "asset")
                    {
                        return(LoadResource(split[2], myType));
                    }
                    else if (split[0] == "ref")                     // No longer used, but kept for backwards compatibility
                    {
                        var t         = go.transform;
                        var splitPath = split[2].Split('/');

                        for (int i = 0; i < splitPath.Length; ++i)
                        {
                            string s = splitPath[i];

                            if (s == "..")
                            {
                                t = t.parent;
                                if (t == null)
                                {
                                    break;
                                }
                            }
                            else if (!string.IsNullOrEmpty(s))
                            {
                                t = t.Find(s);
                                if (t == null)
                                {
                                    break;
                                }
                            }
                        }

                        if (t != null)
                        {
                            if (myType == typeof(GameObject))
                            {
                                return(t.gameObject);
                            }
                            return(t.GetComponent(myType));
                        }
                        else
                        {
                            Debug.LogWarning("Hierarchy path not found: " + split[2], go);
                        }
                    }
                }
            }
            else if (split.Length == 2 && split[0] == "ref")
            {
                var t      = go.transform;
                var myType = UnityTools.GetType(split[1]);

                if (t != null && myType != null)
                {
                    if (myType == typeof(GameObject))
                    {
                        return(t.gameObject);
                    }
                    return(t.GetComponent(myType));
                }
            }
            return(null);
        }