67 lines
2.2 KiB
C#
67 lines
2.2 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
|
|
namespace BadDog.Rendering.AreaLight
|
|
{
|
|
/// <summary>
|
|
/// Restricts values to power-of-2 resolutions: 256, 512, 1024, 2048, 4096, 8192
|
|
/// </summary>
|
|
[CustomPropertyDrawer(typeof(ShadowAtlasResolutionAttribute))]
|
|
public class ShadowAtlasResolutionDrawer : PropertyDrawer
|
|
{
|
|
private static readonly int[] k_AllowedResolutions = { 256, 512, 1024, 2048, 4096, 8192 };
|
|
private static readonly string[] k_ResolutionLabels = { "256", "512", "1024", "2048", "4096", "8192" };
|
|
|
|
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
|
{
|
|
if (property.propertyType != SerializedPropertyType.Integer)
|
|
{
|
|
EditorGUI.PropertyField(position, property, label);
|
|
return;
|
|
}
|
|
|
|
int currentValue = property.intValue;
|
|
int selectedIndex = GetClosestResolutionIndex(currentValue);
|
|
|
|
EditorGUI.BeginProperty(position, label, property);
|
|
|
|
Rect popupRect = EditorGUI.PrefixLabel(position, label);
|
|
int newIndex = EditorGUI.Popup(popupRect, selectedIndex, k_ResolutionLabels);
|
|
|
|
if (newIndex != selectedIndex)
|
|
{
|
|
property.intValue = k_AllowedResolutions[newIndex];
|
|
}
|
|
else
|
|
{
|
|
int clampedValue = k_AllowedResolutions[selectedIndex];
|
|
if (currentValue != clampedValue)
|
|
{
|
|
property.intValue = clampedValue;
|
|
}
|
|
}
|
|
|
|
EditorGUI.EndProperty();
|
|
}
|
|
|
|
private int GetClosestResolutionIndex(int value)
|
|
{
|
|
int closestIndex = 0;
|
|
int minDiff = Mathf.Abs(value - k_AllowedResolutions[0]);
|
|
|
|
for (int i = 1; i < k_AllowedResolutions.Length; i++)
|
|
{
|
|
int diff = Mathf.Abs(value - k_AllowedResolutions[i]);
|
|
if (diff < minDiff)
|
|
{
|
|
minDiff = diff;
|
|
closestIndex = i;
|
|
}
|
|
}
|
|
|
|
return closestIndex;
|
|
}
|
|
}
|
|
}
|
|
|