SerializedDataParameter.cs 3.0 KB

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