using UnityEngine; using UnityEditor; namespace BadDog.Rendering.AreaLight { /// /// Custom property drawer for AreaLightSettings /// [CustomPropertyDrawer(typeof(AreaLightSettings))] public class AreaLightSettingsDrawer : PropertyDrawer { private SerializedProperty m_MaxAreaLights; private SerializedProperty m_PreIntegratedFGD_GGXDisneyDiffuse; private SerializedProperty m_PreIntegratedFGD_CharlieFabricLambert; private SerializedProperty m_PreIntegratedFGD_Marschner; private string m_CachedPropertyPath; private void FindProperties(SerializedProperty property) { string currentPath = property.propertyPath; if (m_CachedPropertyPath != currentPath) { m_MaxAreaLights = property.FindPropertyRelative("maxAreaLights"); m_PreIntegratedFGD_GGXDisneyDiffuse = property.FindPropertyRelative("preIntegratedFGD_GGXDisneyDiffuse"); m_PreIntegratedFGD_CharlieFabricLambert = property.FindPropertyRelative("preIntegratedFGD_CharlieFabricLambert"); m_PreIntegratedFGD_Marschner = property.FindPropertyRelative("preIntegratedFGD_Marschner"); m_CachedPropertyPath = currentPath; } } public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { FindProperties(property); EditorGUI.BeginProperty(position, label, property); Rect foldoutRect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight); property.isExpanded = EditorGUI.Foldout(foldoutRect, property.isExpanded, label, true); if (!property.isExpanded) { EditorGUI.EndProperty(); return; } EditorGUI.indentLevel++; float yOffset = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; if (HasMissingShaders()) { property.serializedObject.Update(); AutoFindShaders(); property.serializedObject.ApplyModifiedProperties(); } DrawProperty(m_MaxAreaLights, ref yOffset, position); DrawProperty(m_PreIntegratedFGD_GGXDisneyDiffuse, ref yOffset, position); DrawProperty(m_PreIntegratedFGD_CharlieFabricLambert, ref yOffset, position); DrawProperty(m_PreIntegratedFGD_Marschner, ref yOffset, position); EditorGUI.indentLevel--; EditorGUI.EndProperty(); } private void AutoFindShaders() { const string packageShadersPath = "Packages/com.baddog.rendering.arealight/Shaders/"; TryLoadShader(m_PreIntegratedFGD_GGXDisneyDiffuse, packageShadersPath + "PreIntegratedFGD_GGXDisneyDiffuse.shader", "PreIntegratedFGD GGX Disney Diffuse"); TryLoadShader(m_PreIntegratedFGD_CharlieFabricLambert, packageShadersPath + "PreIntegratedFGD_CharlieFabricLambert.shader", "PreIntegratedFGD Charlie Fabric Lambert"); TryLoadShader(m_PreIntegratedFGD_Marschner, packageShadersPath + "PreIntegratedFGD_Marschner.shader", "PreIntegratedFGD Marschner"); } private void TryLoadShader(SerializedProperty shaderRef, string path, string displayName) { if (shaderRef != null && shaderRef.objectReferenceValue == null) { var shader = AssetDatabase.LoadAssetAtPath(path); if (shader != null) { shaderRef.objectReferenceValue = shader; } } } private bool HasMissingShaders() { return (m_PreIntegratedFGD_GGXDisneyDiffuse != null && m_PreIntegratedFGD_GGXDisneyDiffuse.objectReferenceValue == null) || (m_PreIntegratedFGD_CharlieFabricLambert != null && m_PreIntegratedFGD_CharlieFabricLambert.objectReferenceValue == null) || (m_PreIntegratedFGD_Marschner != null && m_PreIntegratedFGD_Marschner.objectReferenceValue == null); } private void DrawProperty(SerializedProperty prop, ref float yOffset, Rect position) { if (prop != null) { Rect rect = new Rect(position.x, position.y + yOffset, position.width, EditorGUI.GetPropertyHeight(prop)); EditorGUI.PropertyField(rect, prop, true); yOffset += EditorGUI.GetPropertyHeight(prop) + EditorGUIUtility.standardVerticalSpacing; } } public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { // Base height for foldout float height = EditorGUIUtility.singleLineHeight; // If not expanded, return just the foldout height if (!property.isExpanded) { return height; } // Find properties to calculate height FindProperties(property); // Add height for all properties using cached references height += GetPropertyHeight(m_MaxAreaLights); height += GetPropertyHeight(m_PreIntegratedFGD_GGXDisneyDiffuse); height += GetPropertyHeight(m_PreIntegratedFGD_CharlieFabricLambert); height += GetPropertyHeight(m_PreIntegratedFGD_Marschner); return height; } private float GetPropertyHeight(SerializedProperty prop) { if (prop != null) { return EditorGUI.GetPropertyHeight(prop) + EditorGUIUtility.standardVerticalSpacing; } return 0f; } } }