Пример #1
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;
                }
            }
        }
Пример #2
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;
                }
            }
        }