/// <summary> /// Loads the project from the specified path. /// </summary> /// <param name="path">The path.</param> public static void Load(string path) { if (Initialized) { Close(); } Debug.Log(string.Format("Start loading project {0}", path)); Project = new Project { ProjectName = Path.GetFileNameWithoutExtension(path), ProjectDirectory = Path.GetDirectoryName(path), ModDirectory = Path.Combine(Utility.Utility.ParkitectModPath, Path.GetFileNameWithoutExtension(path)), ProjectFile = Path.GetFileName(path), ProjectFileAutoSave = Path.GetFileName(path) + ".autosave" }; AssetPack = JsonConvert.DeserializeObject <AssetPack>(File.ReadAllText(path)); AssetPack.LoadGameObjects(); AssetPack.InitAssetsInScene(); EditorPrefs.SetString("loadedProject", string.Format("{0}.autosave", Project.Value.ProjectFile)); AssetEditorWindow.ShowWindow(); Debug.Log(string.Format("Finished loading project {0}", path)); }
/// <summary> /// Saves and exports this project. /// </summary> /// <param name="exportAssetZip">If the assets folder should be exported as an archive with the pack.</param> public static bool Export(bool exportAssetZip) { EditorSceneManager.SaveScene(SceneManager.GetActiveScene()); var path = Path.Combine(Project.Value.ProjectDirectory, Project.Value.ProjectFile); if (Save() && AssetPack.CreateAssetBundle()) { File.Copy(path, Path.Combine(Project.Value.ModDirectory, Project.Value.ProjectFile), true); var assetZipPath = Path.Combine(Project.Value.ModDirectory, "assets.zip"); // Always delete the old file. It will get recreated if the user checked the checkbox. if (File.Exists(assetZipPath)) { File.Delete(assetZipPath); } if (exportAssetZip) { Debug.Log(string.Format("Archiving {0} to {1}", Project.Value.ProjectDirectory, assetZipPath)); ArchiveHelper.CreateZip(assetZipPath, Project.Value.ProjectDirectory); } var previewImagePath = Path.Combine(Project.Value.ProjectDirectory, "Resources/preview.png"); File.Copy(previewImagePath, Path.Combine(Project.Value.ModDirectory, "preview.png"), true); return(true); } Debug.LogWarning(string.Format("Failed saving project {0}", path)); return(false); }
/// <summary> /// Fills asset pack with gameobjects from the scene and/or prefabs. /// </summary> /// <param name="assetPack">The asset pack.</param> public static void LoadGameObjects(this AssetPack assetPack) { for (var i = assetPack.Assets.Count - 1; i >= 0; i--) { var asset = assetPack.Assets[i]; // instantiate the prefab if game object doesn't exist. if (asset.GameObject == null) { Debug.Log(string.Format("Can't find {0} in the scene, instantiating prefab.", asset.Name)); try // if one object fails to load, don't make it fail the rest { var go = Resources.Load <GameObject>(string.Format("AssetPack/{0}", asset.Guid)); asset.GameObject = Object.Instantiate(go); asset.GameObject.name = asset.Name; } catch (System.Exception) { Debug.LogError(string.Format("Could not find GameObject at Assets/Resources/AssetPack/{0} for asset {1}, skipped loading of asset", asset.Guid, asset.Name)); assetPack.Assets.Remove(asset); } } } }
/// <summary> /// Saves the specified asset pack. /// </summary> /// <param name="assetPack">The asset pack.</param> /// <returns></returns> public static bool CreateAssetBundle(this AssetPack assetPack) { if (assetPack.Assets.Any(a => a.GameObject == null)) { foreach (var asset in assetPack.Assets.Where(a => a.GameObject == null)) { Debug.LogError(string.Format("Could not save asset pack because GameObject of asset {0} is missing.", asset.Name)); return(false); } } // make sure the prefab directory exists Directory.CreateDirectory(Path.Combine(ProjectManager.Project.Value.ProjectDirectory, "Resources/AssetPack")); // create the prefabs and store the paths in prefabPaths var prefabPaths = new List <string>(); foreach (var asset in assetPack.Assets) { if (asset.Type == AssetType.Train) { if (asset.LeadCar != null) { prefabPaths.Add(CreatePrefab(asset.LeadCar.GameObject, asset.LeadCar.Guid)); } if (asset.Car != null) { prefabPaths.Add(CreatePrefab(asset.Car.GameObject, asset.Car.Guid)); } if (asset.RearCar != null) { prefabPaths.Add(CreatePrefab(asset.RearCar.GameObject, asset.RearCar.Guid)); } } else { asset.LeadCar = null; asset.Car = null; asset.RearCar = null; prefabPaths.Add(CreatePrefab(asset.GameObject, asset.Guid)); } } // use the prefab list to build an assetbundle AssetBundleBuild[] descriptor = { new AssetBundleBuild() { assetBundleName = "assetPack", assetNames = prefabPaths.ToArray() } }; BuildPipeline.BuildAssetBundles(ProjectManager.Project.Value.ModDirectory, descriptor, BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.StrictMode, BuildTarget.StandaloneWindows); return(true); }
/// <summary> /// Closes the loaded project without saving. /// </summary> public static void Close() { AssetPack.RemoveAssetsFromScene(); AssetPack = null; Project = null; EditorPrefs.SetString("loadedProject", null); }
/// <summary> /// Initializes a new project with the specified name. /// </summary> /// <param name="name">The name. <remarks>May only container letters, numbers and spaces</remarks></param> /// <exception cref="ProjectAlreadyExistsException">When the directory already exists.</exception> public static void Init(string name) { if (!Regex.IsMatch(name, "^[0-9A-Za-z ]+$")) { throw new InvalidProjectNameException("Project name may not contain any special characters."); } var projectDirectory = Application.dataPath; var modDirectory = Path.Combine(Utility.Utility.ParkitectModPath, name); var projectFile = string.Format("{0}.assetProject", name); var projectFileAutoSave = string.Format("{0}.autosave", projectFile); var projectFilePath = Path.Combine(projectDirectory, projectFile); if (File.Exists(projectFilePath)) { throw new ProjectAlreadyExistsException(string.Format("There already is a project at {0}", projectFilePath)); } if (Directory.Exists(modDirectory)) { throw new ProjectAlreadyExistsException(string.Format("Your Parkitect installation already has a mod called {0} at {1}", name, modDirectory)); } Project = new Project { ProjectName = name, ProjectDirectory = projectDirectory, ModDirectory = modDirectory, ProjectFile = projectFile, ProjectFileAutoSave = projectFileAutoSave }; AssetPack = new AssetPack { Name = name, Description = "An asset pack" }; EditorPrefs.SetString("loadedProject", Path.Combine(Project.Value.ProjectDirectory, Project.Value.ProjectFileAutoSave)); }