using System;
using System.Reflection;
using UnityEngine;
using UnityEditor;
using UnityEngine.Rendering.Universal;
namespace UnityEditor.Rendering.Universal
{
///
/// Class ShadowShape2DProvider_ProperyDrawer is the default property drawer for all shadow providers and will render the providers serialized properties
///
[CustomPropertyDrawer(typeof(ShadowShape2DProvider), true)]
public class ShadowShape2DProvider_ProperyDrawer : PropertyDrawer
{
delegate void ProcessChild(SerializedProperty child);
bool IsChildVisible(Type parentType, SerializedProperty child)
{
// Check to see if the child is public and not hidden in the inspector
FieldInfo fieldInfo = parentType.GetField(child.name, BindingFlags.Public | BindingFlags.Instance);
HideInInspector hideInInspector = parentType.GetCustomAttribute();
return fieldInfo != null && hideInInspector == null;
}
void ProcessChildren(SerializedProperty parentProperty, ProcessChild onProcessChild)
{
if (parentProperty == null)
return;
var enumerator = parentProperty.GetEnumerator();
if (enumerator.Current == null)
return;
object parentObj = parentProperty.managedReferenceValue;
Type parentType = parentObj.GetType();
while (enumerator.MoveNext())
{
SerializedProperty child = enumerator.Current as SerializedProperty;
if (child != null && IsChildVisible(parentType, child))
{
onProcessChild(child);
}
}
}
///
/// Gets the name to be listed in the ShadowCaster2D Casting Option drop down.
///
/// Rectangle on the screen to use for the property GUI.
/// The SerializedProperty to make the custom GUI for.
/// The label of this property.
public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
ProcessChildren(property, (SerializedProperty child) =>
{
rect.height = EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(rect, child);
rect.y += EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight;
});
}
///
/// Gets the name to be listed in the ShadowCaster2D Casting Option drop down.
///
/// The SerializedProperty to make the custom GUI for.
/// The label of this property.
/// The float height in pixels.
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
float height = 0;
ProcessChildren(property, (SerializedProperty child) =>
{
height += EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight;
});
return height;
}
}
}