using System; using System.Linq; using System.Reflection; using UnityEngine.Assertions; using UnityEngine.Rendering; namespace UnityEditor.Rendering { /// /// A serialization wrapper for . /// public sealed class SerializedDataParameter { /// /// The serialized property for . /// public SerializedProperty overrideState { get; private set; } /// /// The serialized property for /// public SerializedProperty value { get; private set; } /// /// A pre-fetched list of all the attributes applied on the . /// public Attribute[] attributes { get; private set; } /// /// The actual type of the serialized . /// public Type referenceType { get; private set; } SerializedProperty m_BaseProperty; object m_ReferenceValue; /// /// The generated display name of the for the inspector. /// public string displayName => m_BaseProperty.displayName; internal SerializedDataParameter(SerializedProperty property) { // Find the actual property type, optional attributes & reference var path = property.propertyPath.Split('.'); object obj = property.serializedObject.targetObject; FieldInfo field = null; foreach (var p in path) { field = obj.GetType().GetField(p, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); obj = field.GetValue(obj); } Assert.IsNotNull(field); m_BaseProperty = property.Copy(); overrideState = m_BaseProperty.FindPropertyRelative("m_OverrideState"); value = m_BaseProperty.FindPropertyRelative("m_Value"); attributes = field.GetCustomAttributes(false).Cast().ToArray(); referenceType = obj.GetType(); m_ReferenceValue = obj; } /// /// Gets and casts an attribute applied on the base . /// /// /// public T GetAttribute() where T : Attribute { return (T)attributes.FirstOrDefault(x => x is T); } /// /// Gets and casts the underlying reference of type . /// /// The type to cast to /// The reference to the serialized type public T GetObjectRef() { return (T)m_ReferenceValue; } } }