Exemple #1
0
        /// <summary>
        /// Instantiate a new game object given its previously serialized DataNode.
        /// You can serialize game objects by using GameObject.Serialize(), but be aware that serializing only
        /// works fully in the Unity Editor. Prefabs can't be located automatically outside of the Unity Editor.
        /// </summary>

        static public GameObject Instantiate(this DataNode data)
        {
            GameObject child = null;

            byte[] assetBytes = data.GetChild <byte[]>("assetBundle");

            if (assetBytes != null)
            {
                AssetBundle ab = UnityTools.LoadAssetBundle(assetBytes);

                if (ab != null)
                {
                    var go = ab.mainAsset as GameObject;

                    if (go != null)
                    {
                        child      = GameObject.Instantiate(go) as GameObject;
                        child.name = data.name;
                    }
                }
            }
            else
            {
                string path = data.GetChild <string>("prefab");

                if (!string.IsNullOrEmpty(path))
                {
                    GameObject prefab = UnityTools.LoadPrefab(path);

                    if (prefab != null)
                    {
                        child      = GameObject.Instantiate(prefab) as GameObject;
                        child.name = data.name;
                        child.SetActive(true);
                    }
                    else
                    {
                        child = new GameObject(data.name);
                    }
                }
                else
                {
                    child = new GameObject(data.name);
                }

                child.Deserialize(data, true);
            }
            return(child);
        }