private void CollectionOnChanged(object sender, NotifyCollectionChangedEventArgs changeArgs) { if (changeArgs.Action == NotifyCollectionChangedAction.Reset) { foreach (var item in ObjectIdLookup.Keys.ToArray()) { RemoveLookup(item); } ObjectIdLookup.Clear(); GameObjectLookup.Clear(); return; } var targetTransform = Parent ?? SourceView.transform; if (changeArgs.NewItems != null) { foreach (var item in changeArgs.NewItems) { ViewBase view = null; if (OnCreateView != null) { view = OnCreateView(item as ViewModel); } else { view = ViewName == null ? SourceView.InstantiateView(item as ViewModel) : SourceView.InstantiateView(ViewName, item as ViewModel) as ViewBase; } if (view != null) { AddLookup(view.gameObject, item as ViewModel); var rectTransform = view.GetComponent <RectTransform>(); if (rectTransform != null) { rectTransform.SetParent(targetTransform, false); } else { view.transform.SetParent(targetTransform); } if (OnAddView != null) { OnAddView(view); } } } } if (changeArgs.OldItems != null && changeArgs.OldItems.Count > 0) { foreach (var oldItem in changeArgs.OldItems) { RemoveLookup(oldItem as ViewModel); } } }
private void CollectionOnChanged(NotifyCollectionChangedEventArgs changeArgs) { if (changeArgs.Action == NotifyCollectionChangedAction.Reset) { foreach (var item in ObjectIdLookup.Keys.ToArray()) { RemoveLookup(item); } ObjectIdLookup.Clear(); GameObjectLookup.Clear(); return; } var targetTransform = Parent != null ? Parent : SourceView.transform; if (changeArgs.NewItems != null) { foreach (var item in changeArgs.NewItems) { ViewBase view = null; if (OnCreateView != null) { view = OnCreateView(item as ViewModel); } else { view = ViewName == null ? SourceView.InstantiateView(item as ViewModel) : SourceView.InstantiateView(ViewName, item as ViewModel) as ViewBase; } if (view != null) { AddLookup(view.gameObject, item as ViewModel); #if (UNITY_4_6 || UNITY_5_0) view.transform.SetParent(targetTransform, false); #else view.transform.parent = targetTransform; #endif if (OnAddView != null) { OnAddView(view); } } } } if (changeArgs.OldItems != null && changeArgs.OldItems.Count > 0) { foreach (var oldItem in changeArgs.OldItems) { RemoveLookup(oldItem as ViewModel); } } }
public override void Unbind() { Collection.CollectionChanged -= CollectionOnChanged; GameObjectLookup.Clear(); base.Unbind(); }