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);
}
///
/// 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.
///
/// 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);
}
///
/// 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.
///
/// 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
{
///
/// Retrieves a by using a lambda expression to reference its containing class and field.
///
/// The class type containing the field.
/// The field type.
/// The being searched.
/// A lambda expression pointing to the field within the source class.
/// The corresponding , or null if not found.
public static SerializedProperty Find(this SerializedObject obj, Expression> expr)
{
var path = CoreEditorUtils.FindProperty(expr);
return obj.FindProperty(path);
}
///
/// Retrieves a relative based on a lambda expression pointing to a specific field within the source object.
///
/// The class type containing the field.
/// The field type.
/// The instance of to begin the search from.
/// >A lambda expression pointing to the field within the source class.
/// The relative if found; otherwise, null.
public static SerializedProperty Find(this SerializedProperty obj, Expression> expr)
{
var path = CoreEditorUtils.FindProperty(expr);
return obj.FindPropertyRelative(path);
}
}
}