123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using System;
- using System.Linq.Expressions;
- using UnityEngine.Assertions;
-
- namespace UnityEditor.Rendering
- {
- /// <summary>
- /// Serialized property fetcher.
- /// </summary>
- /// <typeparam name="T">Serialized object type.</typeparam>
- public sealed class PropertyFetcher<T> : IDisposable
- {
- /// <summary>
- /// Serialized object associated with the fetcher.
- /// </summary>
- public readonly SerializedObject obj;
-
- /// <summary>
- /// Constructor
- /// </summary>
- /// <param name="obj">Serialized object containing properties to fetch.</param>
- public PropertyFetcher(SerializedObject obj)
- {
- Assert.IsNotNull(obj);
- this.obj = obj;
- }
-
- /// <summary>
- /// Find a property by name.
- /// </summary>
- /// <param name="str">Property name.</param>
- /// <returns>Required property if it exists, null otherwise.</returns>
- public SerializedProperty Find(string str)
- {
- return obj.FindProperty(str);
- }
-
- /// <summary>
- /// Find a property based on an expression.
- ///
- /// To use with extreme caution. It not really get the property but try to find a field with similar name
- /// Hence inheritance override of property is not supported.
- /// Also variable rename will silently break the search.
- /// </summary>
- /// <typeparam name="TValue">Type of the serialized object.</typeparam>
- /// <param name="expr">Expression for the property.</param>
- /// <returns>Required property if it exists, null otherwise.</returns>
- public SerializedProperty Find<TValue>(Expression<Func<T, TValue>> expr)
- {
- string path = CoreEditorUtils.FindProperty(expr);
- return obj.FindProperty(path);
- }
-
- /// <summary>
- /// Disposable pattern implementation.
- /// </summary>
- public void Dispose()
- {
- // Nothing to do here, still needed so we can rely on the using/IDisposable pattern
- }
- }
-
- /// <summary>
- /// Relative property fetcher.
- /// </summary>
- /// <typeparam name="T">SerializedObject type.</typeparam>
- public sealed class RelativePropertyFetcher<T> : IDisposable
- {
- /// <summary>
- /// Serialized object associated with the fetcher.
- /// </summary>
- public readonly SerializedProperty obj;
-
- /// <summary>
- /// Constructor
- /// </summary>
- /// <param name="obj">Serialized object containing properties to fetch.</param>
- public RelativePropertyFetcher(SerializedProperty obj)
- {
- Assert.IsNotNull(obj);
- this.obj = obj;
- }
-
- /// <summary>
- /// Find a property by name.
- /// </summary>
- /// <param name="str">Property name.</param>
- /// <returns>Required property if it exists, null otherwise.</returns>
- public SerializedProperty Find(string str)
- {
- return obj.FindPropertyRelative(str);
- }
-
- /// <summary>
- /// Find a property based on an expression.
- ///
- /// Use with extreme caution as this method does not directly retrieve the property but instead searches for a field with a similar name.
- /// Inheritance and property overrides are not supported, and renaming a variable may break the linkage without warning.
- /// </summary>
- /// <typeparam name="TValue">Type of the serialized object.</typeparam>
- /// <param name="expr">Expression for the property.</param>
- /// <returns>Required property if it exists, null otherwise.</returns>
- public SerializedProperty Find<TValue>(Expression<Func<T, TValue>> expr)
- {
- string path = CoreEditorUtils.FindProperty(expr);
- return obj.FindPropertyRelative(path);
- }
-
- /// <summary>
- /// Disposable pattern implementation.
- /// </summary>
- public void Dispose()
- {
- // Nothing to do here, still needed so we can rely on the using/IDisposable pattern
- }
- }
-
- /// <summary>
- /// Property fetcher extension class.
- /// </summary>
- public static class PropertyFetcherExtensions
- {
- /// <summary>
- /// Retrieves a <see cref="SerializedProperty"/> by using a lambda expression to reference its containing class and field.
- /// </summary>
- /// <typeparam name="TSource">The class type containing the field.</typeparam>
- /// <typeparam name="TValue">The field type.</typeparam>
- /// <param name="obj">The <see cref="SerializedObject"/> being searched.</param>
- /// <param name="expr">A lambda expression pointing to the field within the source class.</param>
- /// <returns>The corresponding <see cref="SerializedProperty"/>, or null if not found.</returns>
- public static SerializedProperty Find<TSource, TValue>(this SerializedObject obj, Expression<Func<TSource, TValue>> expr)
- {
- var path = CoreEditorUtils.FindProperty(expr);
- return obj.FindProperty(path);
- }
-
- /// <summary>
- /// Retrieves a relative <see cref="SerializedProperty"/> based on a lambda expression pointing to a specific field within the source object.
- /// </summary>
- /// <typeparam name="TSource">The class type containing the field.</typeparam>
- /// <typeparam name="TValue">The field type.</typeparam>
- /// <param name="obj">The instance of <see cref="SerializedProperty"/> to begin the search from.</param>
- /// <param name="expr">>A lambda expression pointing to the field within the source class.</param>
- /// <returns>The relative <see cref="SerializedProperty"/> if found; otherwise, null.</returns>
- public static SerializedProperty Find<TSource, TValue>(this SerializedProperty obj, Expression<Func<TSource, TValue>> expr)
- {
- var path = CoreEditorUtils.FindProperty(expr);
- return obj.FindPropertyRelative(path);
- }
- }
- }
|