namespace Sandbox;
///
/// An object (or data) that can be accessed as an object
///
public class MultiSerializedObject : SerializedObject
{
List children = new List();
private static TResult SelectDistincSingleOrFallback( IEnumerable source, Func selector, TResult fallback )
{
// Last 2 elements is all we need to check if there is more than 2 we want to return fallback
var distinct = source.Select( selector ).Distinct().Take( 2 );
var isSingle = distinct.Count() == 1;
return isSingle ? distinct.FirstOrDefault() : fallback;
}
public override string TypeIcon => SelectDistincSingleOrFallback( children, x => x.TypeIcon, "multiple-icon" );
public override string TypeName => SelectDistincSingleOrFallback( children, x => x.TypeName, "MultipleTypes" );
public override string TypeTitle => SelectDistincSingleOrFallback( children, x => x.TypeTitle, "Multiple Objects" );
public override bool IsValid => children.Where( x => x.IsValid() ).Any();
public MultiSerializedObject()
{
PropertyList = new List();
}
///
/// Add an object. Don't forget to rebuild after editing!
///
public void Add( SerializedObject obj )
{
obj.OnPropertyChanged += p => OnPropertyChanged?.Invoke( p );
obj.OnPropertyPreChange += p => OnPropertyPreChange?.Invoke( p );
obj.OnPropertyStartEdit += p => OnPropertyStartEdit?.Invoke( p );
obj.OnPropertyFinishEdit += p => OnPropertyFinishEdit?.Invoke( p );
children.Add( obj );
}
///
/// Rebuild the object after modifying. This updates PropertyList.
///
public void Rebuild()
{
PropertyList.Clear();
SerializedProperty[] childProperties = children.SelectMany( x => x ).ToArray();
var groupedProperties = childProperties.GroupBy( x => x.Name ).ToArray();
foreach ( var group in groupedProperties )
{
if ( group.Count() == 1 )
{
PropertyList.Add( group.First() );
continue;
}
var msp = new MultiSerializedProperty( this, group.Select( x => x ) );
PropertyList.Add( msp );
}
}
///
/// True if the target is multiple objects
///
public override bool IsMultipleTargets => children.Count > 1;
///
/// A list of actual target objects - if applicable
///
public override IEnumerable