SerializedPropertyHelper.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Linq.Expressions;
  3. using UnityEditor;
  4. namespace Cinemachine.Editor
  5. {
  6. /// <summary>
  7. /// Helpers for the editor relating to SerializedPropertys
  8. /// </summary>
  9. public static class SerializedPropertyHelper
  10. {
  11. /// <summary>
  12. /// This is a way to get a field name string in such a manner that the compiler will
  13. /// generate errors for invalid fields. Much better than directly using strings.
  14. /// Usage: instead of
  15. /// <code>
  16. /// "m_MyField";
  17. /// </code>
  18. /// do this:
  19. /// <code>
  20. /// MyClass myclass = null;
  21. /// SerializedPropertyHelper.PropertyName( () => myClass.m_MyField);
  22. /// </code>
  23. /// </summary>
  24. /// <param name="exp">Magic expression that resolves to a field: () => myClass.m_MyField</param>
  25. /// <returns></returns>
  26. public static string PropertyName(Expression<Func<object>> exp)
  27. {
  28. var body = exp.Body as MemberExpression;
  29. if (body == null)
  30. {
  31. var ubody = (UnaryExpression)exp.Body;
  32. body = ubody.Operand as MemberExpression;
  33. }
  34. return body.Member.Name;
  35. }
  36. /// <summary>
  37. /// A compiler-assisted (non-string-based) way to call SerializedProperty.FindProperty
  38. /// </summary>
  39. /// <param name="obj">The serialized object to search</param>
  40. /// <param name="exp">Magic expression that resolves to a field: () => myClass.m_MyField</param>
  41. /// <returns>The resulting SerializedProperty, or null</returns>
  42. public static SerializedProperty FindProperty(this SerializedObject obj, Expression<Func<object>> exp)
  43. {
  44. return obj.FindProperty(PropertyName(exp));
  45. }
  46. /// <summary>
  47. /// A compiler-assisted (non-string-based) way to call SerializedProperty.FindPropertyRelative
  48. /// </summary>
  49. /// <param name="obj">The serialized object to search</param>
  50. /// <param name="exp">Magic expression that resolves to a field: () => myClass.m_MyField</param>
  51. /// <returns>The resulting SerializedProperty, or null</returns>
  52. public static SerializedProperty FindPropertyRelative(this SerializedProperty obj, Expression<Func<object>> exp)
  53. {
  54. return obj.FindPropertyRelative(PropertyName(exp));
  55. }
  56. /// <summary>Get the value of a proprty, as an object</summary>
  57. /// <param name="property">The property to query</param>
  58. /// <returns>The object value of the property</returns>
  59. public static object GetPropertyValue(SerializedProperty property)
  60. {
  61. var targetObject = property.serializedObject.targetObject;
  62. var targetObjectClassType = targetObject.GetType();
  63. var field = targetObjectClassType.GetField(property.propertyPath);
  64. if (field != null)
  65. return field.GetValue(targetObject);
  66. return null;
  67. }
  68. }
  69. }