Skip to content

Instantly share code, notes, and snippets.

@liamcary
Created February 8, 2024 07:16
Show Gist options
  • Select an option

  • Save liamcary/f08e68f5090e54226ac6de945d28d777 to your computer and use it in GitHub Desktop.

Select an option

Save liamcary/f08e68f5090e54226ac6de945d28d777 to your computer and use it in GitHub Desktop.
Custom LODGroup Inspector with fields for entering distances instead of screen size percentages.
using UnityEngine;
namespace UnityEditor
{
/// NOTE: This needs to be in an editor folder with an Assembly Definition Reference referencing
/// the UnityEditor.UI assembly. This is because it uses internal extension methods which are inaccessible
/// outside of UnityEditor.UI.
[CustomEditor(typeof(LODGroup))]
internal class LODGroupCustomEditor : LODGroupEditor
{
public override void OnInspectorGUI()
{
DrawDistanceProperties();
base.OnInspectorGUI();
}
void DrawDistanceProperties()
{
var lodGroup = (LODGroup) target;
var lods = lodGroup.GetLODs();
if (targets.Length == 0 || lods.Length == 0) {
return;
}
if (SceneView.lastActiveSceneView == null || SceneView.lastActiveSceneView.camera == null) {
return;
}
var camera = SceneView.lastActiveSceneView.camera;
float[] lodDistances = new float[lods.Length];
EditorGUILayout.BeginVertical();
EditorGUILayout.LabelField("Custom Distance Tool: ");
for (int i = 0; i < lods.Length; ++i) {
EditorGUILayout.BeginHorizontal();
float percent = lods[i].screenRelativeTransitionHeight;
float distance = LODGroupExtensions.RelativeHeightToDistance(camera, percent, lodGroup.size);
string to = i == lods.Length - 1 ? "cull" : $"LOD{i}";
string label = $"LOD{i} -> {to}";
float newDistance = EditorGUILayout.FloatField(label, distance);
if (distance != newDistance) {
Undo.RecordObject(target, "Set LOD Distance");
lods[i].screenRelativeTransitionHeight = LODGroupExtensions.DistanceToRelativeHeight(camera, newDistance, lodGroup.size);
lodGroup.SetLODs(lods);
}
EditorGUILayout.LabelField(percent.ToString("P4"));
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
}
}
}
@liamcary
Copy link
Author

liamcary commented Feb 8, 2024

Reposting as a comment in case you didn't see the comment at the top of the class:

NOTE: This needs to be in an editor folder with an Assembly Definition Reference referencing the UnityEditor.UI assembly. This is because it uses internal extension methods which are inaccessible outside of UnityEditor.UI.

@PolyTimotheos
Copy link

Thanks for sharing ! I actually needed something like this very badly about 1 1/2 weeks ago ^^.
I ended up taking way too long building something similar and i had no idea that these extension functions exist, so i had to make them myself with some googling and trial and error. Though since i built it as a Component that lives next to the LODGroup, it can assure the distances no matter what aspect ratio and fov (except for some edge case ofc).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment