Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LOD Generator Helper Presets #63

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
158 changes: 97 additions & 61 deletions Editor/LODGeneratorHelperEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace UnityMeshSimplifier.Editor
[CustomEditor(typeof(LODGeneratorHelper))]
internal sealed class LODGeneratorHelperEditor : UnityEditor.Editor
{
private const string LodGeneratorPresetFieldName = "lodGeneratorPreset";
private const string CustomizeSettingsFieldName = "customizeSettings";
private const string FadeModeFieldName = "fadeMode";
private const string AnimateCrossFadingFieldName = "animateCrossFading";
private const string AutoCollectRenderersFieldName = "autoCollectRenderers";
Expand All @@ -54,6 +56,8 @@ internal sealed class LODGeneratorHelperEditor : UnityEditor.Editor
private const float RendererButtonWidth = 60f;
private const float RemoveRendererButtonSize = 20f;

private SerializedProperty lodGeneratorPresetProperty = null;
private SerializedProperty customizeSettingsProperty = null;
private SerializedProperty fadeModeProperty = null;
private SerializedProperty animateCrossFadingProperty = null;
private SerializedProperty autoCollectRenderersProperty = null;
Expand Down Expand Up @@ -82,6 +86,8 @@ internal sealed class LODGeneratorHelperEditor : UnityEditor.Editor

private void OnEnable()
{
lodGeneratorPresetProperty = serializedObject.FindProperty(LodGeneratorPresetFieldName);
customizeSettingsProperty = serializedObject.FindProperty(CustomizeSettingsFieldName);
fadeModeProperty = serializedObject.FindProperty(FadeModeFieldName);
animateCrossFadingProperty = serializedObject.FindProperty(AnimateCrossFadingFieldName);
autoCollectRenderersProperty = serializedObject.FindProperty(AutoCollectRenderersFieldName);
Expand Down Expand Up @@ -128,17 +134,7 @@ private void DrawGeneratedView()

private void DrawNotGeneratedView()
{
EditorGUILayout.PropertyField(fadeModeProperty);
var fadeMode = (LODFadeMode)fadeModeProperty.intValue;

bool hasCrossFade = (fadeMode == LODFadeMode.CrossFade || fadeMode == LODFadeMode.SpeedTree);
if (hasCrossFade)
{
EditorGUILayout.PropertyField(animateCrossFadingProperty);
}

EditorGUILayout.PropertyField(autoCollectRenderersProperty);
DrawSimplificationOptions();

bool newOverrideSaveAssetsPath = EditorGUILayout.Toggle(overrideSaveAssetsPathContent, overrideSaveAssetsPath);
if (newOverrideSaveAssetsPath != overrideSaveAssetsPath)
Expand All @@ -159,6 +155,36 @@ private void DrawNotGeneratedView()
}
}

EditorGUI.BeginDisabledGroup(customizeSettingsProperty.boolValue == true);
{
EditorGUILayout.ObjectField(lodGeneratorPresetProperty, typeof(LODGeneratorPreset));
}
EditorGUI.EndDisabledGroup();

EditorGUILayout.PropertyField(customizeSettingsProperty);

EditorGUI.BeginDisabledGroup(customizeSettingsProperty.boolValue == false);
{
EditorGUILayout.PropertyField(fadeModeProperty);
}
EditorGUI.EndDisabledGroup();
var fadeMode = (LODFadeMode)fadeModeProperty.intValue;

bool hasCrossFade = (fadeMode == LODFadeMode.CrossFade || fadeMode == LODFadeMode.SpeedTree);

EditorGUI.BeginDisabledGroup(customizeSettingsProperty.boolValue == false);
{
if (hasCrossFade)
{
EditorGUILayout.PropertyField(animateCrossFadingProperty);
}
}
EditorGUI.EndDisabledGroup();

EditorGUI.BeginDisabledGroup(customizeSettingsProperty.boolValue == false);
DrawSimplificationOptions();
EditorGUI.EndDisabledGroup();

if (settingsExpanded == null || settingsExpanded.Length != levelsProperty.arraySize)
{
var newSettingsExpanded = new bool[levelsProperty.arraySize];
Expand All @@ -175,10 +201,14 @@ private void DrawNotGeneratedView()
DrawLevel(levelIndex, levelProperty, hasCrossFade);
}

if (GUILayout.Button(createLevelButtonContent))
EditorGUI.BeginDisabledGroup(customizeSettingsProperty.boolValue == false);
{
CreateLevel();
if (GUILayout.Button(createLevelButtonContent))
{
CreateLevel();
}
}
EditorGUI.EndDisabledGroup();

if (GUILayout.Button(generateLODButtonContent))
{
Expand Down Expand Up @@ -209,73 +239,78 @@ private void DrawSimplificationOptions()

private void DrawLevel(int index, SerializedProperty levelProperty, bool hasCrossFade)
{
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
GUILayout.Label(string.Format("Level {0}", index + 1), EditorStyles.boldLabel);
var renderersProperty = levelProperty.FindPropertyRelative(LevelRenderersFieldName);

var previousBackgroundColor = GUI.backgroundColor;
GUI.backgroundColor = removeColor;
if (GUILayout.Button(deleteLevelButtonContent, GUILayout.Width(RemoveLevelButtonSize)))
EditorGUI.BeginDisabledGroup(customizeSettingsProperty.boolValue == false);
{
DeleteLevel(index);
}
GUI.backgroundColor = previousBackgroundColor;
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
GUILayout.Label(string.Format("Level {0}", index + 1), EditorStyles.boldLabel);

++EditorGUI.indentLevel;

var screenRelativeHeightProperty = levelProperty.FindPropertyRelative(LevelScreenRelativeHeightFieldName);
EditorGUILayout.PropertyField(screenRelativeHeightProperty);

var qualityProperty = levelProperty.FindPropertyRelative(LevelQualityFieldName);
EditorGUILayout.PropertyField(qualityProperty);
var previousBackgroundColor = GUI.backgroundColor;
GUI.backgroundColor = removeColor;
if (GUILayout.Button(deleteLevelButtonContent, GUILayout.Width(RemoveLevelButtonSize)))
{
DeleteLevel(index);
}
GUI.backgroundColor = previousBackgroundColor;
EditorGUILayout.EndHorizontal();

bool animateCrossFading = (hasCrossFade ? animateCrossFadingProperty.boolValue : false);
settingsExpanded[index] = EditorGUILayout.Foldout(settingsExpanded[index], settingsContent);
if (settingsExpanded[index])
{
++EditorGUI.indentLevel;

var combineMeshesProperty = levelProperty.FindPropertyRelative(LevelCombineMeshesFieldName);
EditorGUILayout.PropertyField(combineMeshesProperty);
var screenRelativeHeightProperty = levelProperty.FindPropertyRelative(LevelScreenRelativeHeightFieldName);
EditorGUILayout.PropertyField(screenRelativeHeightProperty);

if (combineMeshesProperty.boolValue)
{
var combineSubMeshesProperty = levelProperty.FindPropertyRelative(LevelCombineSubMeshesFieldName);
EditorGUILayout.PropertyField(combineSubMeshesProperty);
}
var qualityProperty = levelProperty.FindPropertyRelative(LevelQualityFieldName);
EditorGUILayout.PropertyField(qualityProperty);

var childProperties = levelProperty.GetChildProperties();
foreach (var childProperty in childProperties)
bool animateCrossFading = (hasCrossFade ? animateCrossFadingProperty.boolValue : false);
settingsExpanded[index] = EditorGUILayout.Foldout(settingsExpanded[index], settingsContent);
if (settingsExpanded[index])
{
if (string.Equals(childProperty.name, LevelScreenRelativeHeightFieldName) || string.Equals(childProperty.name, LevelQualityFieldName) ||
string.Equals(childProperty.name, LevelCombineMeshesFieldName) || string.Equals(childProperty.name, LevelCombineSubMeshesFieldName) ||
string.Equals(childProperty.name, LevelRenderersFieldName))
++EditorGUI.indentLevel;

var combineMeshesProperty = levelProperty.FindPropertyRelative(LevelCombineMeshesFieldName);
EditorGUILayout.PropertyField(combineMeshesProperty);

if (combineMeshesProperty.boolValue)
{
continue;
var combineSubMeshesProperty = levelProperty.FindPropertyRelative(LevelCombineSubMeshesFieldName);
EditorGUILayout.PropertyField(combineSubMeshesProperty);
}
else if ((!hasCrossFade || !animateCrossFading) && string.Equals(childProperty.name, LevelFadeTransitionWidthFieldName))

var childProperties = levelProperty.GetChildProperties();
foreach (var childProperty in childProperties)
{
continue;
if (string.Equals(childProperty.name, LevelScreenRelativeHeightFieldName) || string.Equals(childProperty.name, LevelQualityFieldName) ||
string.Equals(childProperty.name, LevelCombineMeshesFieldName) || string.Equals(childProperty.name, LevelCombineSubMeshesFieldName) ||
string.Equals(childProperty.name, LevelRenderersFieldName))
{
continue;
}
else if ((!hasCrossFade || !animateCrossFading) && string.Equals(childProperty.name, LevelFadeTransitionWidthFieldName))
{
continue;
}

EditorGUILayout.PropertyField(childProperty, true);
}

EditorGUILayout.PropertyField(childProperty, true);
--EditorGUI.indentLevel;
}

--EditorGUI.indentLevel;
}

// Remove any null renderers
var renderersProperty = levelProperty.FindPropertyRelative(LevelRenderersFieldName);
for (int rendererIndex = renderersProperty.arraySize - 1; rendererIndex >= 0; rendererIndex--)
{
var rendererProperty = renderersProperty.GetArrayElementAtIndex(rendererIndex);
var renderer = rendererProperty.objectReferenceValue as Renderer;
if (renderer == null)
// Remove any null renderers
for (int rendererIndex = renderersProperty.arraySize - 1; rendererIndex >= 0; rendererIndex--)
{
renderersProperty.DeleteArrayElementAtIndex(rendererIndex);
var rendererProperty = renderersProperty.GetArrayElementAtIndex(rendererIndex);
var renderer = rendererProperty.objectReferenceValue as Renderer;
if (renderer == null)
{
renderersProperty.DeleteArrayElementAtIndex(rendererIndex);
}
}
}
EditorGUI.EndDisabledGroup();

bool autoCollectRenderers = autoCollectRenderersProperty.boolValue;
if (!autoCollectRenderers)
Expand Down Expand Up @@ -509,6 +544,7 @@ private void GenerateLODs()
try
{
EditorUtility.DisplayProgressBar("Generating LODs", "Generating LODs...", 0f);
lodGeneratorHelper?.TryUpdateSettingsFromPreset();
var lodGroup = LODGenerator.GenerateLODs(lodGeneratorHelper);
if (lodGroup != null)
{
Expand Down Expand Up @@ -631,7 +667,7 @@ where go.transform.IsChildOf(ourTransform)

if (prefabGameObjects.Any())
{
EditorUtility.DisplayDialog("Invalid GameObjects", "Some objects are not children of the LODGenerator GameObject," +
EditorUtility.DisplayDialog("Invalid GameObjects", "Some objects are not children of the LODGenerator GameObject," +
" as well as being part of a prefab. They will not be added.", "OK");
}
#endif
Expand Down
Loading