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