Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PropertyFetcher.cs 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /// <summary>
  35. /// Find a property based on an expression.
  36. ///
  37. /// To use with extreme caution. It not really get the property but try to find a field with similar name
  38. /// Hence inheritance override of property is not supported.
  39. /// Also variable rename will silently break the search.
  40. /// </summary>
  41. /// <typeparam name="TValue">Type of the serialized object.</typeparam>
  42. /// <param name="expr">Expression for the property.</param>
  43. /// <returns>Required property if it exists, null otherwise.</returns>
  44. public SerializedProperty Find<TValue>(Expression<Func<T, TValue>> expr)
  45. {
  46. string path = CoreEditorUtils.FindProperty(expr);
  47. return obj.FindProperty(path);
  48. }
  49. /// <summary>
  50. /// Disposable pattern implementation.
  51. /// </summary>
  52. public void Dispose()
  53. {
  54. // Nothing to do here, still needed so we can rely on the using/IDisposable pattern
  55. }
  56. }
  57. /// <summary>
  58. /// Relative property fetcher.
  59. /// </summary>
  60. /// <typeparam name="T">SerializedObject type.</typeparam>
  61. public sealed class RelativePropertyFetcher<T> : IDisposable
  62. {
  63. /// <summary>
  64. /// Serialized object associated with the fetcher.
  65. /// </summary>
  66. public readonly SerializedProperty obj;
  67. /// <summary>
  68. /// Constructor
  69. /// </summary>
  70. /// <param name="obj">Serialized object containing properties to fetch.</param>
  71. public RelativePropertyFetcher(SerializedProperty obj)
  72. {
  73. Assert.IsNotNull(obj);
  74. this.obj = obj;
  75. }
  76. /// <summary>
  77. /// Find a property by name.
  78. /// </summary>
  79. /// <param name="str">Property name.</param>
  80. /// <returns>Required property if it exists, null otherwise.</returns>
  81. public SerializedProperty Find(string str)
  82. {
  83. return obj.FindPropertyRelative(str);
  84. }
  85. /// <summary>
  86. /// Find a property based on an expression.
  87. ///
  88. /// Use with extreme caution as this method does not directly retrieve the property but instead searches for a field with a similar name.
  89. /// Inheritance and property overrides are not supported, and renaming a variable may break the linkage without warning.
  90. /// </summary>
  91. /// <typeparam name="TValue">Type of the serialized object.</typeparam>
  92. /// <param name="expr">Expression for the property.</param>
  93. /// <returns>Required property if it exists, null otherwise.</returns>
  94. public SerializedProperty Find<TValue>(Expression<Func<T, TValue>> expr)
  95. {
  96. string path = CoreEditorUtils.FindProperty(expr);
  97. return obj.FindPropertyRelative(path);
  98. }
  99. /// <summary>
  100. /// Disposable pattern implementation.
  101. /// </summary>
  102. public void Dispose()
  103. {
  104. // Nothing to do here, still needed so we can rely on the using/IDisposable pattern
  105. }
  106. }
  107. /// <summary>
  108. /// Property fetcher extension class.
  109. /// </summary>
  110. public static class PropertyFetcherExtensions
  111. {
  112. /// <summary>
  113. /// Retrieves a <see cref="SerializedProperty"/> by using a lambda expression to reference its containing class and field.
  114. /// </summary>
  115. /// <typeparam name="TSource">The class type containing the field.</typeparam>
  116. /// <typeparam name="TValue">The field type.</typeparam>
  117. /// <param name="obj">The <see cref="SerializedObject"/> being searched.</param>
  118. /// <param name="expr">A lambda expression pointing to the field within the source class.</param>
  119. /// <returns>The corresponding <see cref="SerializedProperty"/>, or null if not found.</returns>
  120. public static SerializedProperty Find<TSource, TValue>(this SerializedObject obj, Expression<Func<TSource, TValue>> expr)
  121. {
  122. var path = CoreEditorUtils.FindProperty(expr);
  123. return obj.FindProperty(path);
  124. }
  125. /// <summary>
  126. /// Retrieves a relative <see cref="SerializedProperty"/> based on a lambda expression pointing to a specific field within the source object.
  127. /// </summary>
  128. /// <typeparam name="TSource">The class type containing the field.</typeparam>
  129. /// <typeparam name="TValue">The field type.</typeparam>
  130. /// <param name="obj">The instance of <see cref="SerializedProperty"/> to begin the search from.</param>
  131. /// <param name="expr">>A lambda expression pointing to the field within the source class.</param>
  132. /// <returns>The relative <see cref="SerializedProperty"/> if found; otherwise, null.</returns>
  133. public static SerializedProperty Find<TSource, TValue>(this SerializedProperty obj, Expression<Func<TSource, TValue>> expr)
  134. {
  135. var path = CoreEditorUtils.FindProperty(expr);
  136. return obj.FindPropertyRelative(path);
  137. }
  138. }
  139. }