I have a scriptable object that is a wrapper around a typical c# List, but with my own Add, Remove, and indexer methods.
I want to fully support Unity.Properties and runtime binding using the CreateProperty attribute. Currently I notify that a property has changed when changing count and capacity, but I’m not sure what the best way to do so when changing list items.
Here’s some simplified code to show how I currently do it:
public class MyList : ScriptableObject, IList<T>, IDataSourceViewHashProvider, INotifyBindablePropertyChanged
{
[DontCreateProperty]
private readonly List<T> list = new List<T>();
// Do I place a CreateProperty attribute here?
public T this[int index]
{
get { return list[index]; }
set
{
list[index] = value;
// Do I do NotifyPropertyChanged(nameof(this[index])); here?
}
}
public event EventHandler<BindablePropertyChangedEventArgs> propertyChanged;
[CreateProperty]
public int Count
{
get { return list.Count; }
}
public void Add(T item)
{
list.Add(item);
NotifyPropertyChanged(nameof(Count));
}
// Etc...
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
propertyChanged?.Invoke(this, new BindablePropertyChangedEventArgs(propertyName));
}
public long GetViewHashCode()
{
unchecked
{
long hash = 17;
foreach (var item in list)
{
hash = hash * 23 + item.GetHashCode();
}
hash = hash * 23 + Count.GetHashCode();
return hash;
}
}
}
Any advice would be helpful!