-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store changes into editor preferences.
- Loading branch information
Showing
6 changed files
with
72 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using CANStudio.BulletStorm.Util; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace CANStudio.BulletStorm.Editor | ||
{ | ||
/// <summary> | ||
/// Static class for read and write editor preferences. | ||
/// </summary> | ||
public static class Preferences | ||
{ | ||
/// <summary> | ||
/// Access preferences. | ||
/// </summary> | ||
public static SerializedPreference _ => SerializedPreference.Load(); | ||
|
||
/// <summary> | ||
/// Write all changes in preference to disk. | ||
/// </summary> | ||
public static void ApplyChanges() => _.Save(); | ||
} | ||
|
||
[Serializable] | ||
public class SerializedPreference | ||
{ | ||
public Mesh shapePreviewMesh; | ||
public Material shapePreviewMaterial; | ||
|
||
private const string PreferenceKey = "BulletStorm_Preference"; | ||
private static readonly Lazy<SerializedPreference> Cached = new Lazy<SerializedPreference>(() => | ||
{ | ||
var json = EditorPrefs.GetString(PreferenceKey); | ||
SerializedPreference preference = null; | ||
try | ||
{ | ||
preference = JsonUtility.FromJson<SerializedPreference>(json); | ||
} | ||
catch (Exception e) | ||
{ | ||
BulletStormLogger.LogWarning($"{e} occured when loading preferences:\n" + e.StackTrace); | ||
} | ||
return preference ?? new SerializedPreference(); | ||
}); | ||
|
||
private SerializedPreference(){} | ||
|
||
internal static SerializedPreference Load() => Cached.Value; | ||
|
||
internal void Save() | ||
{ | ||
var json = JsonUtility.ToJson(this); | ||
EditorPrefs.SetString(PreferenceKey, json); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Packages/bullet-storm-unity/Scripts/Editor/Preferences.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.