Created
February 8, 2024 07:16
-
-
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.
This file contains hidden or 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
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.