PropertyFetcher.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Linq.Expressions;
  3. using UnityEngine.Assertions;
  4. namespace UnityEditor.Rendering
  5. {
  6. /// <summary>
  7. /// Serialized property fetcher.
  8. /// </summary>
  9. /// <typeparam name="T">Serialized object type.</typeparam>
  10. public sealed class PropertyFetcher<T> : IDisposable
  11. {
  12. /// <summary>
  13. /// Serialized object associated with the fetcher.
  14. /// </summary>
  15. public readonly SerializedObject obj;
  16. /// <summary>
  17. /// Constructor
  18. /// </summary>
  19. /// <param name="obj">Serialized object containing properties to fetch.</param>
  20. public PropertyFetcher(SerializedObject obj)
  21. {
  22. Assert.IsNotNull(obj);
  23. this.obj = obj;
  24. }
  25. /// <summary>
  26. /// Find a property by name.
  27. /// </summary>
  28. /// <param name="str">Property name.</param>
  29. /// <returns>Required property if it exists, null otherwise.</returns>
  30. public SerializedProperty Find(string str)
  31. {
  32. return obj.FindProperty(str);
  33. }
  34. /// To use with extreme caution. It not really get the property but try to find a field with similar name
  35. /// Hence inheritance override of property is not supported.
  36. /// Also variable rename will silently break the search.
  37. /// <summary>
  38. /// Find a property based on an expression.
  39. /// </summary>
  40. /// <typeparam name="TValue">Type of the serialized object.</typeparam>
  41. /// <param name="expr">Expression for the property.</param>
  42. /// <returns>Required property if it exists, null otherwise.</returns>
  43. public SerializedProperty Find<TValue>(Expression<Func<T, TValue>> expr)
  44. {
  45. string path = CoreEditorUtils.FindProperty(expr);
  46. return obj.FindProperty(path);
  47. }
  48. /// <summary>
  49. /// Disposable pattern implementation.
  50. /// </summary>
  51. public void Dispose()
  52. {
  53. // Nothing to do here, still needed so we can rely on the using/IDisposable pattern
  54. }
  55. }
  56. /// <summary>
  57. /// Relative property fetcher.
  58. /// </summary>
  59. /// <typeparam name="T">SerializedObject type.</typeparam>
  60. public sealed class RelativePropertyFetcher<T> : IDisposable
  61. {
  62. /// <summary>
  63. /// Serialized object associated with the fetcher.
  64. /// </summary>
  65. public readonly SerializedProperty obj;
  66. /// <summary>
  67. /// Constructor
  68. /// </summary>
  69. /// <param name="obj">Serialized object containing properties to fetch.</param>
  70. public RelativePropertyFetcher(SerializedProperty obj)
  71. {
  72. Assert.IsNotNull(obj);
  73. this.obj = obj;
  74. }
  75. /// <summary>
  76. /// Find a property by name.
  77. /// </summary>
  78. /// <param name="str">Property name.</param>
  79. /// <returns>Required property if it exists, null otherwise.</returns>
  80. public SerializedProperty Find(string str)
  81. {
  82. return obj.FindPropertyRelative(str);
  83. }
  84. /// To use with extreme caution. It not really get the property but try to find a field with similar name
  85. /// Hence inheritance override of property is not supported.
  86. /// Also variable rename will silently break the search.
  87. /// <summary>
  88. /// Find a property based on an expression.
  89. /// </summary>
  90. /// <typeparam name="TValue">Type of the serialized object.</typeparam>
  91. /// <param name="expr">Expression for the property.</param>
  92. /// <returns>Required property if it exists, null otherwise.</returns>
  93. public SerializedProperty Find<TValue>(Expression<Func<T, TValue>> expr)
  94. {
  95. string path = CoreEditorUtils.FindProperty(expr);
  96. return obj.FindPropertyRelative(path);
  97. }
  98. /// <summary>
  99. /// Disposable pattern implementation.
  100. /// </summary>
  101. public void Dispose()
  102. {
  103. // Nothing to do here, still needed so we can rely on the using/IDisposable pattern
  104. }
  105. }
  106. /// <summary>
  107. /// Property fetcher extension class.
  108. /// </summary>
  109. public static class PropertyFetcherExtensions
  110. {
  111. /// <summary>
  112. ///
  113. /// </summary>
  114. /// <typeparam name="TSource"></typeparam>
  115. /// <typeparam name="TValue"></typeparam>
  116. /// <param name="obj"></param>
  117. /// <param name="expr"></param>
  118. /// <returns></returns>
  119. public static SerializedProperty Find<TSource, TValue>(this SerializedObject obj, Expression<Func<TSource, TValue>> expr)
  120. {
  121. var path = CoreEditorUtils.FindProperty(expr);
  122. return obj.FindProperty(path);
  123. }
  124. /// <summary>
  125. ///
  126. /// </summary>
  127. /// <typeparam name="TSource"></typeparam>
  128. /// <typeparam name="TValue"></typeparam>
  129. /// <param name="obj"></param>
  130. /// <param name="expr"></param>
  131. /// <returns></returns>
  132. public static SerializedProperty Find<TSource, TValue>(this SerializedProperty obj, Expression<Func<TSource, TValue>> expr)
  133. {
  134. var path = CoreEditorUtils.FindProperty(expr);
  135. return obj.FindPropertyRelative(path);
  136. }
  137. }
  138. }