A game about forced loneliness, made by TACStudios
at master 86 lines 3.2 kB view raw
1using System; 2using System.Linq; 3using System.Reflection; 4using UnityEngine.Assertions; 5using UnityEngine.Rendering; 6 7namespace UnityEditor.Rendering 8{ 9 /// <summary> 10 /// A serialization wrapper for <see cref="VolumeParameter{T}"/>. 11 /// </summary> 12 public sealed class SerializedDataParameter 13 { 14 /// <summary> 15 /// The serialized property for <see cref="VolumeParameter.overrideState"/>. 16 /// </summary> 17 public SerializedProperty overrideState { get; private set; } 18 19 /// <summary> 20 /// The serialized property for <see cref="VolumeParameter{T}.value"/> 21 /// </summary> 22 public SerializedProperty value { get; private set; } 23 24 /// <summary> 25 /// A pre-fetched list of all the attributes applied on the <see cref="VolumeParameter{T}"/>. 26 /// </summary> 27 public Attribute[] attributes { get; private set; } 28 29 /// <summary> 30 /// The actual type of the serialized <see cref="VolumeParameter{T}"/>. 31 /// </summary> 32 public Type referenceType { get; private set; } 33 34 SerializedProperty m_BaseProperty; 35 object m_ReferenceValue; 36 37 /// <summary> 38 /// The generated display name of the <see cref="VolumeParameter{T}"/> for the inspector. 39 /// </summary> 40 public string displayName => m_BaseProperty.displayName; 41 42 internal SerializedDataParameter(SerializedProperty property) 43 { 44 // Find the actual property type, optional attributes & reference 45 var path = property.propertyPath.Split('.'); 46 object obj = property.serializedObject.targetObject; 47 FieldInfo field = null; 48 49 foreach (var p in path) 50 { 51 field = obj.GetType().GetField(p, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); 52 obj = field.GetValue(obj); 53 } 54 55 Assert.IsNotNull(field); 56 57 m_BaseProperty = property.Copy(); 58 overrideState = m_BaseProperty.FindPropertyRelative("m_OverrideState"); 59 value = m_BaseProperty.FindPropertyRelative("m_Value"); 60 attributes = field.GetCustomAttributes(false).Cast<Attribute>().ToArray(); 61 referenceType = obj.GetType(); 62 m_ReferenceValue = obj; 63 } 64 65 /// <summary> 66 /// Gets and casts an attribute applied on the base <see cref="VolumeParameter{T}"/>. 67 /// </summary> 68 /// <typeparam name="T">The type of the attribute to retrieve.</typeparam> 69 /// <returns>The first attribute of type T found, or null if no such attribute is present.</returns> 70 public T GetAttribute<T>() 71 where T : Attribute 72 { 73 return (T)attributes.FirstOrDefault(x => x is T); 74 } 75 76 /// <summary> 77 /// Gets and casts the underlying reference of type <typeparamref name="T"/>. 78 /// </summary> 79 /// <typeparam name="T">The type to cast to</typeparam> 80 /// <returns>The reference to the serialized <see cref="VolumeParameter{T}"/> type</returns> 81 public T GetObjectRef<T>() 82 { 83 return (T)m_ReferenceValue; 84 } 85 } 86}