Skip to content

Commit 9ce84b9

Browse files
committed
Add support for preferences from MeshSimplifiers via IPreferences (resolves #7); Update UnityMeshSimplifier to latest
1 parent 924b478 commit 9ce84b9

File tree

5 files changed

+138
-4
lines changed

5 files changed

+138
-4
lines changed

Packages/UnityMeshSimplifier

Submodule UnityMeshSimplifier updated 49 files

Scripts/Editor/AutoLOD.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ static List<Type> batchers
173173
static SceneLOD s_SceneLOD;
174174
static List<Type> s_MeshSimplifiers;
175175
static List<Type> s_Batchers;
176+
static IPreferences s_SimplifierPreferences;
176177

177178
static void UpdateDependencies()
178179
{
@@ -642,7 +643,6 @@ static void PreferencesGUI()
642643
var maxTime = EditorGUILayout.IntSlider(label, maxExecutionTime, 0, 15);
643644
if (EditorGUI.EndChangeCheck())
644645
maxExecutionTime = maxTime;
645-
646646
}
647647
}
648648

@@ -661,6 +661,19 @@ static void PreferencesGUI()
661661
var selected = EditorGUILayout.Popup(label, Array.IndexOf(displayedOptions, type.Name), displayedOptions);
662662
if (EditorGUI.EndChangeCheck())
663663
meshSimplifierType = meshSimplifiers[selected];
664+
665+
if (meshSimplifierType != null && typeof(IMeshSimplifier).IsAssignableFrom(meshSimplifierType))
666+
{
667+
if (s_SimplifierPreferences == null || s_SimplifierPreferences.GetType() != meshSimplifierType)
668+
s_SimplifierPreferences = (IPreferences)Activator.CreateInstance(meshSimplifierType);
669+
670+
if (s_SimplifierPreferences != null)
671+
{
672+
EditorGUI.indentLevel++;
673+
s_SimplifierPreferences.OnPreferencesGUI();
674+
EditorGUI.indentLevel--;
675+
}
676+
}
664677
}
665678
else
666679
{

Scripts/Editor/MeshSimplifiers/QuadricMeshSimplifier.cs

+100-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#if ENABLE_UNITYMESHSIMPLIFIER
2-
using Unity.AutoLOD;
2+
using System;
3+
using UnityEditor;
34
using UnityEngine;
45
using UnityMeshSimplifier;
56
using Mesh = Unity.AutoLOD.WorkingMesh;
@@ -12,11 +13,64 @@
1213
#if ENABLE_UNITYMESHSIMPLIFIER
1314
namespace Unity.AutoLOD
1415
{
15-
public struct QuadricMeshSimplifier : IMeshSimplifier
16+
public struct QuadricMeshSimplifier : IMeshSimplifier, IPreferences
1617
{
18+
[InitializeOnLoad]
19+
class Preferences : ScriptableSingleton<Preferences>
20+
{
21+
const string k_Options = "AutoLOD.QuadricMeshSimplifier.Options";
22+
23+
public static SimplificationOptions Options;
24+
25+
// Needed for SerializedObject/SerializedProperty
26+
[SerializeField]
27+
internal SimplificationOptions m_Options;
28+
29+
// Load the options statically, so they can be used in jobs
30+
static Preferences()
31+
{
32+
EditorApplication.delayCall += () =>
33+
{
34+
var preferences = Preferences.instance;
35+
Debug.Assert(!preferences.Equals(default), "QuadricMeshSimplifier preferences should never be the default struct");
36+
};
37+
}
38+
39+
void OnEnable()
40+
{
41+
// It's okay that it doesn't save, but don't hide it otherwise the inspector GUI won't work
42+
hideFlags = HideFlags.DontSave;
43+
44+
var savedPrefs = EditorPrefs.GetString(k_Options, null);
45+
if (string.IsNullOrEmpty(savedPrefs))
46+
m_Options = SimplificationOptions.Default;
47+
else
48+
m_Options = JsonUtility.FromJson<SimplificationOptions>(savedPrefs);
49+
50+
// Update the static version
51+
Options = m_Options;
52+
}
53+
54+
public void ResetToDefaults()
55+
{
56+
m_Options = SimplificationOptions.Default;
57+
Save();
58+
}
59+
60+
public void Save()
61+
{
62+
var savedPrefs = JsonUtility.ToJson(m_Options);
63+
EditorPrefs.SetString(k_Options, savedPrefs);
64+
Options = m_Options;
65+
}
66+
}
67+
68+
SerializedObject m_SerializedObject;
69+
1770
public void Simplify(Mesh inputMesh, Mesh outputMesh, float quality)
1871
{
1972
var meshSimplifier = new MeshSimplifier();
73+
meshSimplifier.SimplificationOptions = Preferences.Options;
2074
meshSimplifier.Vertices = inputMesh.vertices;
2175
meshSimplifier.Normals = inputMesh.normals;
2276
meshSimplifier.Tangents = inputMesh.tangents;
@@ -50,6 +104,50 @@ public void Simplify(Mesh inputMesh, Mesh outputMesh, float quality)
50104
}
51105
}
52106

107+
public void OnPreferencesGUI()
108+
{
109+
var preferences = Preferences.instance;
110+
if (m_SerializedObject == null)
111+
m_SerializedObject = new SerializedObject(preferences);
112+
113+
m_SerializedObject.Update();
114+
var property = m_SerializedObject.FindProperty("m_Options");
115+
116+
EditorGUI.BeginChangeCheck();
117+
118+
EditorGUILayout.BeginHorizontal();
119+
EditorGUILayout.PropertyField(property);
120+
if (property.isExpanded)
121+
{
122+
GUI.enabled = !preferences.m_Options.Equals(SimplificationOptions.Default);
123+
if (GUILayout.Button("Reset", EditorStyles.miniButton, GUILayout.ExpandWidth(false)))
124+
{
125+
preferences.ResetToDefaults();
126+
EditorGUIUtility.ExitGUI();
127+
}
128+
GUI.enabled = true;
129+
}
130+
EditorGUILayout.EndHorizontal();
131+
if (property.isExpanded)
132+
{
133+
EditorGUI.indentLevel++;
134+
property.NextVisible(true);
135+
do
136+
{
137+
EditorGUILayout.BeginHorizontal();
138+
EditorGUILayout.LabelField(property.name, GUILayout.Width(200f));
139+
EditorGUILayout.PropertyField(property, GUIContent.none, true, GUILayout.ExpandWidth(false));
140+
EditorGUILayout.EndHorizontal();
141+
} while (property.NextVisible(false));
142+
EditorGUI.indentLevel--;
143+
}
144+
145+
if (EditorGUI.EndChangeCheck())
146+
{
147+
m_SerializedObject.ApplyModifiedProperties();
148+
preferences.Save();
149+
}
150+
}
53151
}
54152
}
55153
#endif

Scripts/Interfaces/IPreferences.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Unity.AutoLOD
2+
{
3+
public interface IPreferences
4+
{
5+
/// <summary>
6+
/// Callback from AutoLOD preferences GUI to show GUI options related to a simplifier / batcher.
7+
/// Settings should be saved here as well and also utilized by the simplifier / batcher.
8+
/// </summary>
9+
void OnPreferencesGUI();
10+
}
11+
}

Scripts/Interfaces/IPreferences.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)