예제 #1
0
        private static void CreateAssetLibraryForScene(Scene scene, int index, AssetLibraryAsset asset, AssetFolderInfo folder, HashSet <UnityObject> hs)
        {
            TypeMap           typeMap    = new TypeMap();
            AssetDB           assetDB    = new AssetDB();
            RuntimeShaderUtil shaderUtil = new RuntimeShaderUtil();

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

            PersistentRuntimeScene rtScene = new PersistentRuntimeScene();

            GetDepsFromContext ctx = new GetDepsFromContext();

            rtScene.GetDepsFrom(scene, ctx);

            Queue <UnityObject> depsQueue  = new Queue <UnityObject>(ctx.Dependencies.OfType <UnityObject>());
            GetDepsFromContext  getDepsCtx = new GetDepsFromContext();

            while (depsQueue.Count > 0)
            {
                UnityObject uo = depsQueue.Dequeue();
                if (!uo)
                {
                    continue;
                }

                Type persistentType = typeMap.ToPersistentType(uo.GetType());
                if (persistentType != null)
                {
                    getDepsCtx.Clear();

                    try
                    {
                        PersistentObject persistentObject = (PersistentObject)Activator.CreateInstance(persistentType);
                        //persistentObject.ReadFrom(uo);
                        persistentObject.GetDepsFrom(uo, getDepsCtx);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e);
                    }

                    foreach (UnityObject dep in getDepsCtx.Dependencies)
                    {
                        if (!ctx.Dependencies.Contains(dep))
                        {
                            ctx.Dependencies.Add(dep);
                            depsQueue.Enqueue(dep);
                        }
                    }
                }
            }

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

            CreateAssetLibrary(ctx.Dependencies.ToArray(), "Scenes/" + scene.name, "SceneAssetLibrary", index, asset, folder, hs);
        }
예제 #2
0
        public static void ConvertToAsset()
        {
            const string ext = ";*.rtscene;*.rtprefab;*.rtmat;*.rttex;*.rtmesh;";

            if (Application.isPlaying)
            {
                EditorUtility.DisplayDialog("Unable to load runtime asset", "Unable to load runtime asset in play mode", "OK");
                return;
            }

            string path = EditorUtility.OpenFilePanel("Select Runtime Asset", Application.persistentDataPath, ext);

            if (path.Length != 0)
            {
                GameObject         projGo = new GameObject();
                IAssetBundleLoader bundleLoader;
#if USE_GOOGLE_DRIVE
                if (File.Exists(Application.streamingAssetsPath + "/credentials.json"))
                {
                    bundleLoader = new GoogleDriveAssetBundleLoader();
                }
                else
#endif
                {
                    bundleLoader = new AssetBundleLoader();
                }

                IOC.Register(bundleLoader);

                ITypeMap typeMap = new TypeMap <long>();
                IOC.Register(typeMap);

                IUnityObjectFactory objFactory = new UnityObjectFactory();
                IOC.Register(objFactory);

                ISerializer serializer = new ProtobufSerializer();
                IOC.Register(serializer);

                IStorage <long> storage = new FileSystemStorage <long>();
                IOC.Register(storage);

                IRuntimeShaderUtil shaderUtil = new RuntimeShaderUtil();
                IOC.Register(shaderUtil);

                IMaterialUtil materialUtil = new StandardMaterialUtils();
                IOC.Register(materialUtil);

                IAssetDB assetDB = new AssetDB();
                IOC.Register <IIDMap>(assetDB);
                IOC.Register(assetDB);

                Project project = projGo.AddComponent <Project>();
                project.Awake_Internal();

                DirectoryInfo root     = new DirectoryInfo(Application.persistentDataPath);
                string        rootPath = root.ToString().ToLower();

                DirectoryInfo parent = Directory.GetParent(path);
                while (true)
                {
                    if (parent == null)
                    {
                        EditorUtility.DisplayDialog("Unable to load runtime asset", "Project.rtmeta was not found", "OK");
                        UnityObject.DestroyImmediate(projGo);
                        IOC.ClearAll();
                        return;
                    }

                    string projectPath = parent.FullName.ToLower();
                    if (rootPath == projectPath)
                    {
                        EditorUtility.DisplayDialog("Unable to load runtime asset", "Project.rtmeta was not found", "OK");
                        UnityObject.DestroyImmediate(projGo);
                        IOC.ClearAll();
                        return;
                    }

                    string projectFile = Path.Combine(projectPath, "Project.rtmeta");
                    if (File.Exists(projectFile))
                    {
                        storage.RootPath = Path.GetDirectoryName(projectPath).Replace('\\', '/') + "/";

                        string projectName = Path.GetFileNameWithoutExtension(projectPath);
                        project.OpenProject(projectName, (error, result) =>
                        {
                            if (error.HasError)
                            {
                                EditorUtility.DisplayDialog("Unable to load runtime asset", "Project " + projectName + " can not be loaded", "OK");
                                UnityObject.DestroyImmediate(projGo);
                                IOC.ClearAll();
                                return;
                            }

                            string relativePath = GetRelativePath(path, projectPath);
                            relativePath        = relativePath.Replace('\\', '/');
                            AssetItem assetItem = (AssetItem)project.Root.Get(relativePath);

                            project.Load(new[] { assetItem }, (loadError, loadedObjects) =>
                            {
                                if (loadError.HasError)
                                {
                                    EditorUtility.DisplayDialog("Unable to load runtime asset", loadError.ToString(), "OK");
                                }
                                else
                                {
                                    if (!project.IsScene(assetItem))
                                    {
                                        foreach (UnityObject asset in assetDB.GetDynamicResources())
                                        {
                                            asset.hideFlags = HideFlags.None;
                                        }

                                        UnityObject loadedObj = loadedObjects[0];
                                        if (loadedObj == null)
                                        {
                                            EditorUtility.DisplayDialog("Unable to load runtime asset", assetItem.Name, "OK");
                                        }
                                        else
                                        {
                                            GameObject loadedGo = loadedObj as GameObject;
                                            if (loadedGo != null)
                                            {
                                                string savePath = EditorUtility.SaveFilePanelInProject("Save " + loadedGo.name, loadedGo.name, "prefab", "Save prefab");

                                                if (!string.IsNullOrWhiteSpace(savePath))
                                                {
                                                    PersistentRuntimePrefab <long> runtimePrefab = new PersistentRuntimePrefab <long>();

                                                    GetDepsFromContext ctx = new GetDepsFromContext();
                                                    runtimePrefab.GetDepsFrom(loadedGo, ctx);

                                                    SaveDependencies(ctx, typeMap, assetDB, Path.GetDirectoryName(savePath));
                                                    PrefabUtility.SaveAsPrefabAsset(loadedGo, savePath);
                                                }
                                            }
                                            else
                                            {
                                                string savePath = EditorUtility.SaveFilePanelInProject("Save " + loadedObj.name, loadedObj.name, "asset", "Save asset");
                                                if (!string.IsNullOrWhiteSpace(savePath))
                                                {
                                                    AssetDatabase.CreateAsset(loadedObj, savePath);
                                                }
                                            }
                                        }
                                    }
                                }

                                IOC.ClearAll();
                                UnityObject.DestroyImmediate(projGo);
                            });
                        });

                        return;
                    }

                    parent = parent.Parent;
                }
            }
        }
예제 #3
0
        public static void OpenScene()
        {
            if (Application.isPlaying)
            {
                EditorUtility.DisplayDialog("Unable to open scene", "Unable to open scene in play mode", "OK");
                return;
            }

            string path = EditorUtility.OpenFilePanel("Open Scene", Application.persistentDataPath, "rtscene");

            if (path.Length != 0)
            {
                GameObject         projGo = new GameObject();
                IAssetBundleLoader bundleLoader;
                if (File.Exists(Application.streamingAssetsPath + "/credentials.json"))
                {
                    bundleLoader = new GoogleDriveAssetBundleLoader();
                }
                else
                {
                    bundleLoader = new AssetBundleLoader();
                }

                IOC.Register(bundleLoader);

                ITypeMap typeMap = new TypeMap();
                IOC.Register(typeMap);

                IUnityObjectFactory objFactory = new UnityObjectFactory();
                IOC.Register(objFactory);

                ISerializer serializer = new ProtobufSerializer();
                IOC.Register(serializer);

                IStorage storage = new FileSystemStorage();
                IOC.Register(storage);

                IRuntimeShaderUtil shaderUtil = new RuntimeShaderUtil();
                IOC.Register(shaderUtil);

                IAssetDB assetDB = new AssetDB();
                IOC.Register <IIDMap>(assetDB);
                IOC.Register(assetDB);

                Project project = projGo.AddComponent <Project>();
                project.Awake_Internal();

                DirectoryInfo root     = new DirectoryInfo(Application.persistentDataPath);
                string        rootPath = root.ToString().ToLower();

                DirectoryInfo parent = Directory.GetParent(path);
                while (true)
                {
                    if (parent == null)
                    {
                        EditorUtility.DisplayDialog("Unable to open scene", "Project.rtmeta was not found", "OK");
                        UnityObject.DestroyImmediate(projGo);
                        IOC.ClearAll();
                        return;
                    }

                    string projectPath = parent.FullName.ToLower();
                    if (rootPath == projectPath)
                    {
                        EditorUtility.DisplayDialog("Unable to open scene", "Project.rtmeta was not found", "OK");
                        UnityObject.DestroyImmediate(projGo);
                        IOC.ClearAll();
                        return;
                    }

                    string projectFile = Path.Combine(projectPath, "Project.rtmeta");
                    if (File.Exists(projectFile))
                    {
                        string projectFileName = Path.GetFileNameWithoutExtension(projectPath);

                        project.OpenProject(projectFileName, (error, result) =>
                        {
                            if (error.HasError)
                            {
                                EditorUtility.DisplayDialog("Unable to open scene", "Project " + projectFileName + " can not be loaded", "OK");
                                UnityObject.DestroyImmediate(projGo);
                                IOC.ClearAll();
                                return;
                            }

                            string relativePath = GetRelativePath(path, projectPath);
                            relativePath        = relativePath.Replace('\\', '/');
                            AssetItem scene     = (AssetItem)project.Root.Get(relativePath);

                            project.Load(new[] { scene }, (loadError, loadedObjects) =>
                            {
                                IOC.ClearAll();
                                if (loadError.HasError)
                                {
                                    EditorUtility.DisplayDialog("Unable to open scene", loadError.ToString(), "OK");
                                    UnityObject.DestroyImmediate(projGo);
                                    return;
                                }
                            });
                        });

                        return;
                    }

                    parent = parent.Parent;
                }
            }
        }