Skip to content
Snippets Groups Projects
Commit e0c5ed25 authored by William MacDonald SE2014's avatar William MacDonald SE2014
Browse files

Merge branch 'save-and-load' into 'master'

Save and load

See merge request !1
parents 82a0d9ed 0953aa69
No related branches found
No related tags found
1 merge request!1Save and load
Showing
with 238 additions and 1 deletion
File added
using System;
using System.IO;
using System.Xml.Serialization;
using UnityEditor;
using UnityEngine;
public class SettingsLoader {
private IRenderSettingPreset loadedPreset;
public IRenderSettingPreset GetLoadedPreset() {
return loadedPreset;
}
public bool Load() {
string path = EditorUtility.OpenFilePanel("Choose file to load", "","json");
if (File.Exists(path)) {
string dataAsJson = File.ReadAllText(path);
Debug.Log("Loaded JSON: " + dataAsJson);
PresetReferenceData presetData = JsonUtility.FromJson<PresetReferenceData>(dataAsJson);
RenderSettingPreset preset = new RenderSettingPreset();
preset.SetName(presetData.GetName());
string relativePath = CalculateRelativePath(Directory.GetParent(path).Name);
foreach(VisualBlueprintReferenceData blueprint in presetData.GetVisualBlueprintData()) {
preset.AddVisualBlueprint(LoadVisualBlueprint(relativePath, blueprint));
}
loadedPreset = preset;
return true;
} else {
return false;
}
}
private string CalculateRelativePath(string path) {
Debug.Log(path);
return "Assets/RenderPresets/" + path + "/";
}
private VisualBlueprint LoadVisualBlueprint(string presetDirectory , VisualBlueprintReferenceData data) {
string name = data.GetName();
Debug.Log("Preset Directory name: " + presetDirectory);
//Interactive mode is set to false because the dialogue box is not needed
AssetDatabase.ImportPackage(presetDirectory + data.GetDependenciesPath(), false);
GameObject blueprintObject = LoadBlueprintObject(presetDirectory + data.GetBlueprintObjectPath());
return new VisualBlueprint(name, blueprintObject);
}
private GameObject LoadBlueprintObject(string path) {
return (GameObject)AssetDatabase.LoadAssetAtPath(path, typeof(GameObject));
}
}
using System;
using System.Collections.Generic;
[Serializable]
public class PresetReferenceData {
public string name;
public List<VisualBlueprintReferenceData> visualBlueprintData;
public PresetReferenceData(string name) {
this.name = name;
visualBlueprintData = new List<VisualBlueprintReferenceData>();
}
public void AddBlueprintReferenceData(VisualBlueprintReferenceData data) {
this.visualBlueprintData.Add(data);
}
public string GetName() {
return name;
}
public List<VisualBlueprintReferenceData> GetVisualBlueprintData() {
return visualBlueprintData;
}
}
using System;
[Serializable]
public class VisualBlueprintReferenceData {
public string name;
public string blueprintObjectPath;
public string dependenciesPath;
public VisualBlueprintReferenceData(string name, string blueprintObjectPath, string dependencies) {
this.name = name;
this.blueprintObjectPath = blueprintObjectPath;
this.dependenciesPath = dependencies;
}
public string GetName() {
return this.name;
}
public string GetBlueprintObjectPath() {
return this.blueprintObjectPath;
}
public string GetDependenciesPath() {
return this.dependenciesPath;
}
}
public interface ISettingsSaver
{
void Save(IRenderSettingPreset preset);
}
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using UnityEditor;
using UnityEngine;
public class SettingsSaver : ISettingsSaver {
private const string presetsRoot = "Assets/RenderPresets/";
private const string visualBlueprintFolder = "VisualBlueprints/";
private const string presetConfExtension = ".json";
public void Save(IRenderSettingPreset preset) {
CreateRequiredFolders(preset);
SavePreset(preset);
}
private void CreateRequiredFolders(IRenderSettingPreset preset) {
if (!Directory.Exists("Assets/RenderPresets/" + preset.GetName() + "/")) {
Directory.CreateDirectory("Assets/RenderPresets/" + preset.GetName() + "/");
Directory.CreateDirectory("Assets/RenderPresets/" + preset.GetName() + "/VisualBlueprints/");
}
}
private void SavePreset(IRenderSettingPreset preset) {
PresetReferenceData presetData = new PresetReferenceData(preset.GetName());
foreach(VisualBlueprint blueprint in preset.GetVisualBlueprints()) {
string savedPrefabPath = SaveVisualBlueprint(blueprint, preset);
string dependencyPath = ExportDependencies(blueprint, preset);
presetData.AddBlueprintReferenceData(new VisualBlueprintReferenceData(blueprint.GetName(), savedPrefabPath, dependencyPath));
}
string presetFile = "Assets/RenderPresets/" + preset.GetName() + "/" + "presetconfig" + presetConfExtension;
string serializedPresetData = JsonUtility.ToJson(presetData);
File.WriteAllText(presetFile, serializedPresetData);
}
private string SaveVisualBlueprint(VisualBlueprint blueprint, IRenderSettingPreset preset) {
//Path of blueprint relative to prese root
PrefabUtility.CreatePrefab("Assets/RenderPresets/" + preset.GetName() + "/VisualBlueprints/" + blueprint.GetName() + ".prefab", blueprint.GetBlueprintObject());
// PrefabUtility.ReplacePrefab(blueprint.GetBlueprintObject(), prefab, ReplacePrefabOptions.ConnectToPrefab);
return "VisualBlueprints/" + blueprint.GetName() + ".prefab";
}
private string ExportDependencies(VisualBlueprint blueprint, IRenderSettingPreset preset) {
string[] dependencies = AssetDatabase.GetDependencies("Assets/RenderPresets/" + preset.GetName() + "/VisualBlueprints/" + blueprint.GetName() + ".prefab");
AssetDatabase.ExportPackage(dependencies, "Assets/RenderPresets/" + preset.GetName() + "/VisualBlueprints/" + blueprint.GetName() + "_dep.unitypackage");
return "VisualBlueprints/" + blueprint.GetName() + "_dep.unitypackage";
}
private string GetFilePath(IRenderSettingPreset preset) {
return EditorUtility.SaveFilePanel("Choose location to save file", "", preset.GetName() + ".json","json");
}
}
......@@ -25,6 +25,7 @@ public class RenderSettingApplier {
}
} catch (UnityEngine.UnityException e) {
Debug.Log("No objects in scene with tag: " + blueprint.GetName() + " exist");
Debug.Log(e);
}
}
......
......@@ -4,7 +4,10 @@ using UnityEngine;
public class RenderSettingPreset : IRenderSettingPreset {
[SerializeField]
private string name;
[SerializeField]
private HashSet<VisualBlueprint> visualBlueprints;
public RenderSettingPreset() {
......
......@@ -4,7 +4,10 @@ using UnityEngine;
public class VisualBlueprint {
[SerializeField]
private string name;
[SerializeField]
private GameObject blueprint;
public VisualBlueprint(string name, GameObject bluePrintObject) {
......
......@@ -12,6 +12,8 @@ public class RenderSettingsWindow : EditorWindow {
private IRenderSettingPresetBuilder presetBuilder = new RenderSettingPresetBuilder();
private RenderSettingApplier settingApplier = new RenderSettingApplier();
private ISettingsSaver settingsSaver = new SettingsSaver();
private SettingsLoader settingsLoader = new SettingsLoader();
private const bool isNotCollapsed = true;
......@@ -22,6 +24,34 @@ public class RenderSettingsWindow : EditorWindow {
private bool isCreatingPreset = false;
private bool UserWantsToOverwritePreset(string name) {
return EditorUtility.DisplayDialog("Overwrite existing preset?",
"A preset called " + name + " exists. Are you sure you want to overwrite it?", "Overwrite", "Do Not Overwrite");
}
private void RemovePresetWithName(string name) {
foreach (IRenderSettingPreset p in presets) {
if (p.GetName().Equals(name)) {
presets.Remove(p);
break;
}
}
}
void LoadPreset() {
IRenderSettingPreset preset = settingsLoader.GetLoadedPreset();
if (CheckPresetNameInUse(preset.GetName())) {
if (UserWantsToOverwritePreset(preset.GetName())) {
RemovePresetWithName(preset.GetName());
presets.Add(preset);
presetCollapsedDict.Add(preset, isNotCollapsed);
}
} else {
presets.Add(preset);
presetCollapsedDict.Add(preset, isNotCollapsed);
}
}
void OnGUI() {
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
if (presets.Count == 0) {
......@@ -36,6 +66,10 @@ public class RenderSettingsWindow : EditorWindow {
DrawPresetCreator();
} else if(GUILayout.Button("Create new preset")) {
isCreatingPreset = true;
} else if (GUILayout.Button("Load preset")) {
if (settingsLoader.Load()){
LoadPreset();
}
}
EditorGUILayout.EndScrollView();
......@@ -108,6 +142,10 @@ public class RenderSettingsWindow : EditorWindow {
if (GUILayout.Button("Apply All", GUILayout.Width(100))) {
settingApplier.Apply(preset);
}
if (GUILayout.Button("Save", GUILayout.Width(100))) {
SavePreset(preset);
}
}
GUILayout.FlexibleSpace();
......@@ -146,6 +184,10 @@ public class RenderSettingsWindow : EditorWindow {
}
}
private void SavePreset(IRenderSettingPreset preset) {
settingsSaver.Save(preset);
}
private bool AddDropdownSettingsButton(FocusType type) {
GUIStyle iconButtonStyle = GUI.skin.FindStyle("IconButton") ?? EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).FindStyle("IconButton");
GUIContent content = new GUIContent(EditorGUIUtility.Load("icons/_Popup.png") as Texture2D);
......@@ -169,7 +211,7 @@ public class RenderSettingsWindow : EditorWindow {
if (AddDropdownSettingsButton(FocusType.Keyboard)) {
GenericMenu presetMenu = new GenericMenu();
focusedPreset = preset; // Need to put the preset into a field because it is not possible to pass 2 arguments to a Menu Item callback method.
presetMenu.AddItem(new GUIContent("Remove Blueprint"), false, RemoveVisualBlueprint, blueprint);
presetMenu.AddItem(new GUIContent("Remove Blueprin"), false, RemoveVisualBlueprint, blueprint);
presetMenu.ShowAsContext();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment