Skip to content

Commit

Permalink
fix: #22
Browse files Browse the repository at this point in the history
Store changes into editor preferences.
  • Loading branch information
IF-ACT committed Nov 2, 2020
1 parent ba56109 commit f8b0537
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static class BulletStormEditorUtil
/// <returns>Information to draw gizmos.</returns>
public static void DrawShapePreview(Shape shape, [NotNull] Camera camera, LabelContent content)
{
if (shape is null || !Caches.Instance.shapePreviewMesh || !Caches.Instance.shapePreviewMaterial) return;
if (shape is null || !Preferences._.shapePreviewMesh || !Preferences._.shapePreviewMaterial) return;

var prs = new List<Tuple<Vector3, Quaternion, float>>();

Expand All @@ -39,8 +39,8 @@ public static void DrawShapePreview(Shape shape, [NotNull] Camera camera, LabelC
var block = new MaterialPropertyBlock();
block.SetColor(ColorIndex, shape[i].DefaultColor ? Color.white : shape[i].color);

Graphics.DrawMesh(Caches.Instance.shapePreviewMesh, Matrix4x4.TRS(position, lookRotation, size),
Caches.Instance.shapePreviewMaterial, 0, camera, 0, block);
Graphics.DrawMesh(Preferences._.shapePreviewMesh, Matrix4x4.TRS(position, lookRotation, size),
Preferences._.shapePreviewMaterial, 0, camera, 0, block);

prs.Add(new Tuple<Vector3, Quaternion, float>(position, lookRotation, speed));
}
Expand Down
16 changes: 0 additions & 16 deletions Packages/bullet-storm-unity/Scripts/Editor/Caches.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Packages/bullet-storm-unity/Scripts/Editor/Caches.cs.meta

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CANStudio.BulletStorm.Emission;
using CANStudio.BulletStorm.Util;
using CANStudio.BulletStorm.XNodes;
using CANStudio.BulletStorm.XNodes.ShapeNodes;
using UnityEditor;
Expand Down Expand Up @@ -37,10 +38,14 @@ public override void OnPreviewSettings()
labelContent =
(BulletStormEditorUtil.LabelContent) EditorGUILayout.EnumPopup("Label content", labelContent);

Caches.Instance.shapePreviewMesh =
EditorGUILayout.ObjectField(Caches.Instance.shapePreviewMesh, typeof(Mesh), false) as Mesh;
Caches.Instance.shapePreviewMaterial =
EditorGUILayout.ObjectField(Caches.Instance.shapePreviewMaterial, typeof(Material), false) as Material;
EditorGUI.BeginChangeCheck();

Preferences._.shapePreviewMesh =
EditorGUILayout.ObjectField(Preferences._.shapePreviewMesh, typeof(Mesh), false) as Mesh;
Preferences._.shapePreviewMaterial =
EditorGUILayout.ObjectField(Preferences._.shapePreviewMaterial, typeof(Material), false) as Material;

if (EditorGUI.EndChangeCheck()) Preferences.ApplyChanges();
}

protected void OnEnable()
Expand Down
57 changes: 57 additions & 0 deletions Packages/bullet-storm-unity/Scripts/Editor/Preferences.cs
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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f8b0537

Please sign in to comment.