using System; using System.Linq.Expressions; using UnityEngine.Assertions; namespace UnityEditor.Rendering { public sealed class PropertyFetcher : IDisposable { public readonly SerializedObject obj; public PropertyFetcher(SerializedObject obj) { Assert.IsNotNull(obj); this.obj = obj; } 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. /// public SerializedProperty Find(Expression> expr) { string path = CoreEditorUtils.FindProperty(expr); return obj.FindProperty(path); } public void Dispose() { // Nothing to do here, still needed so we can rely on the using/IDisposable pattern } } public sealed class RelativePropertyFetcher : IDisposable { public readonly SerializedProperty obj; public RelativePropertyFetcher(SerializedProperty obj) { Assert.IsNotNull(obj); this.obj = obj; } 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. /// public SerializedProperty Find(Expression> expr) { string path = CoreEditorUtils.FindProperty(expr); return obj.FindPropertyRelative(path); } public void Dispose() { // Nothing to do here, still needed so we can rely on the using/IDisposable pattern } } public static class PropertyFetcherExtensions { /// /// 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. /// public static SerializedProperty Find(this SerializedObject obj, Expression> expr) { var path = CoreEditorUtils.FindProperty(expr); return obj.FindProperty(path); } /// /// 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. /// public static SerializedProperty Find(this SerializedProperty obj, Expression> expr) { var path = CoreEditorUtils.FindProperty(expr); return obj.FindPropertyRelative(path); } } }