using System; using System.Linq.Expressions; using UnityEngine.Assertions; namespace UnityEditor.Rendering { /// /// Serialized property fetcher. /// /// Serialized object type. public sealed class PropertyFetcher : IDisposable { /// /// Serialized object associated with the fetcher. /// public readonly SerializedObject obj; /// /// Constructor /// /// Serialized object containing properties to fetch. public PropertyFetcher(SerializedObject obj) { Assert.IsNotNull(obj); this.obj = obj; } /// /// Find a property by name. /// /// Property name. /// Required property if it exists, null otherwise. public SerializedProperty Find(string str) { return obj.FindProperty(str); } /// 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. /// /// Find a property based on an expression. /// /// Type of the serialized object. /// Expression for the property. /// Required property if it exists, null otherwise. public SerializedProperty Find(Expression> expr) { string path = CoreEditorUtils.FindProperty(expr); return obj.FindProperty(path); } /// /// Disposable pattern implementation. /// public void Dispose() { // Nothing to do here, still needed so we can rely on the using/IDisposable pattern } } /// /// Relative property fetcher. /// /// SerializedObject type. public sealed class RelativePropertyFetcher : IDisposable { /// /// Serialized object associated with the fetcher. /// public readonly SerializedProperty obj; /// /// Constructor /// /// Serialized object containing properties to fetch. public RelativePropertyFetcher(SerializedProperty obj) { Assert.IsNotNull(obj); this.obj = obj; } /// /// Find a property by name. /// /// Property name. /// Required property if it exists, null otherwise. public SerializedProperty Find(string str) { return obj.FindPropertyRelative(str); } /// 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. /// /// Find a property based on an expression. /// /// Type of the serialized object. /// Expression for the property. /// Required property if it exists, null otherwise. public SerializedProperty Find(Expression> expr) { string path = CoreEditorUtils.FindProperty(expr); return obj.FindPropertyRelative(path); } /// /// Disposable pattern implementation. /// public void Dispose() { // Nothing to do here, still needed so we can rely on the using/IDisposable pattern } } /// /// Property fetcher extension class. /// public static class PropertyFetcherExtensions { /// /// /// /// /// /// /// /// public static SerializedProperty Find(this SerializedObject obj, Expression> expr) { var path = CoreEditorUtils.FindProperty(expr); return obj.FindProperty(path); } /// /// /// /// /// /// /// /// public static SerializedProperty Find(this SerializedProperty obj, Expression> expr) { var path = CoreEditorUtils.FindProperty(expr); return obj.FindPropertyRelative(path); } } }