public IEnumerator PersistentSceneTest()
        {
            GameObject[] sceneGOs = SceneManager.GetActiveScene().GetRootGameObjects().ToArray();
            for (int i = 0; i < sceneGOs.Length; ++i)
            {
                sceneGOs[i].AddComponent <RTSL2Ignore>();
            }

            GameObject go = new GameObject();

            go.name = "root";
            go.transform.position   = Vector3.one;
            go.transform.rotation   = Quaternion.Euler(45, 45, 45);
            go.transform.localScale = Vector3.one * 2;

            GameObject child = new GameObject();

            child.name               = "child";
            child.hideFlags          = HideFlags.NotEditable;
            child.transform.position = Vector3.one * 2;
            child.transform.SetParent(go.transform, true);

            GameObject deps = new GameObject();

            deps.AddComponent <RTSL2Deps>();


            PersistentRuntimeScene scene = new PersistentRuntimeScene();

            scene.ReadFrom(SceneManager.GetActiveScene());

            ISerializer            serializer = RTSL2Deps.Get.Serializer;
            PersistentRuntimeScene clone      = serializer.DeepClone(scene);

            clone.WriteTo(SceneManager.GetActiveScene());
            yield return(null);

            GameObject[] restoredGOs = SceneManager.GetActiveScene().GetRootGameObjects().ToArray().Where(g => g.name.Contains("root")).ToArray();
            Assert.AreEqual(1, restoredGOs.Length);

            go = restoredGOs[0];
            Assert.AreEqual("root", go.name);
            Assert.AreEqual(Vector3.one, go.transform.position);
            Assert.AreEqual(Quaternion.Euler(45, 45, 45), go.transform.rotation);
            Assert.AreEqual(Vector3.one * 2, go.transform.localScale);

            Assert.AreEqual(1, go.transform.childCount);
            child = go.transform.GetChild(0).gameObject;
            Assert.AreEqual("child", child.name);
            Assert.AreEqual(HideFlags.NotEditable, child.hideFlags);
            Assert.AreEqual(Vector3.one * 2, child.transform.position);
        }
        private static void CreateAssetLibraryFromScene(Scene scene, int index, AssetLibraryAsset asset, AssetFolderInfo folder, HashSet <UnityObject> hs)
        {
            TypeMap typeMap = new TypeMap();
            AssetDB assetDB = new AssetDB();

            IOC.Register <ITypeMap>(typeMap);
            IOC.Register <IAssetDB>(assetDB);

            PersistentRuntimeScene rtScene = new PersistentRuntimeScene();

            GetDepsFromContext ctx = new GetDepsFromContext();

            rtScene.GetDepsFrom(scene, ctx);

            IOC.Unregister <ITypeMap>(typeMap);
            IOC.Unregister <IAssetDB>(assetDB);

            int identity = asset.AssetLibrary.Identity;

            foreach (UnityObject obj in ctx.Dependencies)
            {
                if (hs.Contains(obj))
                {
                    continue;
                }

                if (obj is GameObject)
                {
                    GameObject go = (GameObject)obj;
                    if (go.IsPrefab())
                    {
                        AssetInfo assetInfo = new AssetInfo(go.name, 0, identity);
                        assetInfo.Object = go;
                        identity++;

                        List <PrefabPartInfo> prefabParts = new List <PrefabPartInfo>();
                        AssetLibraryAssetsGUI.CreatePefabParts(go, ref identity, prefabParts);
                        if (prefabParts.Count >= AssetLibraryInfo.MAX_ASSETS - AssetLibraryInfo.INITIAL_ID)
                        {
                            EditorUtility.DisplayDialog("Unable Create AssetLibrary", string.Format("Max 'Indentity' value reached. 'Identity' ==  {0}", AssetLibraryInfo.MAX_ASSETS), "OK");
                            return;
                        }

                        if (identity >= AssetLibraryInfo.MAX_ASSETS)
                        {
                            SaveAssetLibrary(asset, scene, index);
                            index++;

                            asset  = ScriptableObject.CreateInstance <AssetLibraryAsset>();
                            folder = asset.AssetLibrary.Folders.Where(f => f.depth == 0).First();

                            identity = asset.AssetLibrary.Identity;
                        }

                        assetInfo.PrefabParts       = prefabParts;
                        asset.AssetLibrary.Identity = identity;
                        folder.Assets.Add(assetInfo);
                    }
                }
                else
                {
                    AssetInfo assetInfo = new AssetInfo(obj.name, 0, identity);
                    assetInfo.Object = obj;
                    identity++;

                    if (identity >= AssetLibraryInfo.MAX_ASSETS)
                    {
                        SaveAssetLibrary(asset, scene, index);
                        index++;

                        asset    = ScriptableObject.CreateInstance <AssetLibraryAsset>();
                        folder   = asset.AssetLibrary.Folders.Where(f => f.depth == 0).First();
                        identity = asset.AssetLibrary.Identity;
                    }

                    asset.AssetLibrary.Identity = identity;
                    folder.Assets.Add(assetInfo);
                }
            }

            SaveAssetLibrary(asset, scene, index);
            index++;

            Selection.activeObject = asset;
            EditorGUIUtility.PingObject(asset);
        }
Exemple #3
0
        private void OnLoadCompleted(AssetItem rootItem, AssetItem[] assetItems, PersistentObject[] persistentObjects, ProjectAsyncOperation <UnityObject> ao, ProjectEventHandler <UnityObject> callback)
        {
            for (int i = 0; i < assetItems.Length; ++i)
            {
                AssetItem assetItem = assetItems[i];
                if (!m_assetDB.IsMapped(assetItem.ItemID))
                {
                    if (m_assetDB.IsStaticResourceID(assetItem.ItemID))
                    {
                        int ordinal = m_assetDB.ToOrdinal(assetItem.ItemID);
                        if (!m_assetDB.IsLibraryLoaded(ordinal))
                        {
                            //AssetLibraryAsset assetLibrary = m_staticReferences.Where(r => r.Ordinal == ordinal).FirstOrDefault();
                            AssetLibraryReferenceInfo reference = m_projectInfo.References.FirstOrDefault(r => r.Ordinal == ordinal);
                            if (reference != null)
                            {
                                m_assetDB.LoadLibrary(reference.AssetLibrary, reference.Ordinal);
                            }
                        }
                    }
                    else if (m_assetDB.IsDynamicResourceID(assetItem.ItemID))
                    {
                        PersistentObject persistentObject = persistentObjects[i];
                        if (persistentObject != null)
                        {
                            if (persistentObject is PersistentRuntimeScene)
                            {
                                PersistentRuntimeScene        persistentScene = (PersistentRuntimeScene)persistentObject;
                                Dictionary <int, UnityObject> idToObj         = new Dictionary <int, UnityObject>();
                                persistentScene.CreateGameObjectWithComponents(m_typeMap, persistentScene.Descriptors[0], idToObj);
                            }
                            else if (persistentObject is PersistentRuntimePrefab)
                            {
                                PersistentRuntimePrefab       persistentPrefab   = (PersistentRuntimePrefab)persistentObject;
                                Dictionary <int, UnityObject> idToObj            = new Dictionary <int, UnityObject>();
                                List <GameObject>             createdGameObjects = new List <GameObject>();
                                persistentPrefab.CreateGameObjectWithComponents(m_typeMap, persistentPrefab.Descriptors[0], idToObj, createdGameObjects);
                                m_assetDB.RegisterDynamicResources(idToObj);
                                for (int j = 0; j < createdGameObjects.Count; ++j)
                                {
                                    GameObject createdGO = createdGameObjects[j];
                                    createdGO.transform.SetParent(createdGO.transform, false);
                                    m_dynamicResources.Add(unchecked ((int)m_assetDB.ToID(createdGO)), createdGO);
                                }
                            }
                            else
                            {
                                Type        type     = m_typeMap.ToType(assetItem.TypeGuid);
                                UnityObject instance = m_factory.CreateInstance(type);
                                m_assetDB.RegisterDynamicResource(unchecked ((int)assetItem.ItemID), instance);
                                m_dynamicResources.Add(unchecked ((int)assetItem.ItemID), instance);
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < persistentObjects.Length; ++i)
            {
                PersistentObject persistentObject = persistentObjects[i];
                if (persistentObject != null)
                {
                    if (m_assetDB.IsSceneID(assetItems[i].ItemID))
                    {
                        persistentObject.WriteTo(SceneManager.GetActiveScene());
                    }
                    else
                    {
                        UnityObject obj = m_assetDB.FromID <UnityObject>(assetItems[i].ItemID);
                        Debug.Assert(obj != null);
                        if (obj != null)
                        {
                            persistentObject.WriteTo(obj);
                        }
                    }
                }
            }
            Error       error  = new Error(Error.OK);
            UnityObject result = m_assetDB.FromID <UnityObject>(rootItem.ItemID);

            if (callback != null)
            {
                callback(error, result);
            }
            ao.Error       = error;
            ao.Result      = result;
            ao.IsCompleted = true;
        }