using System;using System.Collections.Specialized;using ObservableCollections;using UnityEngine;namespace Aladdin.Standard.Observable.Collections.List{public class ObservableListTest : MonoBehaviour{protected readonly ObservableList<int> observableList = new ObservableList<int>();public void Start(){ISynchronizedView<int, string> observableListSynchronizedView = this.observableList.CreateView((value) => $"这是测试数据: {value}");observableListSynchronizedView.ViewChanged += this.ObservableListSynchronizedViewViewChanged;this.observableList.Add(1);this.observableList.Add(2);this.observableList.Add(3);this.observableList.AddRange(new[] { 4, 5, 6 });this.observableList[4] = 8;this.observableList.RemoveAt(0);this.observableList.Move(2, 0);foreach (int value in this.observableList){Debug.LogError($"{value}");}foreach (string value in observableListSynchronizedView){Debug.LogError($"{value}");}this.observableList.Sort();this.observableList.Reverse();this.observableList.Clear();observableListSynchronizedView.Dispose();}internal void ObservableListSynchronizedViewViewChanged(in SynchronizedViewChangedEventArgs<int, string> eventArgs){switch (eventArgs.Action){case NotifyCollectionChangedAction.Add:Debug.LogError($"添加数据:{eventArgs.NewItem.View}");break;case NotifyCollectionChangedAction.Move:Debug.LogError($"移动数据:{eventArgs.NewItem.View}");break;case NotifyCollectionChangedAction.Remove:Debug.LogError($"删除数据:{eventArgs.OldItem.View}");break;case NotifyCollectionChangedAction.Replace:Debug.LogError($"替换数据:{eventArgs.NewItem.View} {eventArgs.OldItem.View}");break;case NotifyCollectionChangedAction.Reset:Debug.LogError($"重置数据:{eventArgs.SortOperation.IsSort} {eventArgs.SortOperation.IsClear} {eventArgs.SortOperation.IsReverse}");break;default:throw new ArgumentOutOfRangeException();}}}}
2、List ObservableCollections Filter 过滤
using ObservableCollections;using UnityEngine;namespace Aladdin.Standard.Observable.Collections.List{public class ObservableListFilter : MonoBehaviour{protected readonly ObservableList<int> observableList = new ObservableList<int>();public void Start(){this.observableList.Add(1);this.observableList.Add(20);this.observableList.AddRange(new[] { 30, 31, 32 });foreach (int value in this.observableList){Debug.LogError($"未过滤:{value}");}ISynchronizedView<int, string> observableListSynchronizedView = this.observableList.CreateView((value) => $"过滤: {value}");Debug.LogError($"数据长度:{observableListSynchronizedView.Count} ========================");observableListSynchronizedView.AttachFilter(value => value % 2 == 0);foreach (string value in observableListSynchronizedView){Debug.LogError($"{value}");}Debug.LogError($"数据长度:{observableListSynchronizedView.Count} ========================");observableListSynchronizedView.AttachFilter(x => x % 2 == 1);foreach (string value in observableListSynchronizedView){Debug.LogError($"{value}");}Debug.LogError($"数据长度:{observableListSynchronizedView.Count} ========================");observableListSynchronizedView.Dispose();}}}
3、List ObservableCollections Sort And Reverse(排序和反转)
using System.Collections.Generic;using ObservableCollections;using UnityEngine;namespace Aladdin.Standard.Observable.Collections.List{public class ObservableListSortAndReverse : MonoBehaviour{protected readonly ObservableList<int> observableList = new ObservableList<int>();public void Start(){this.observableList.AddRange(new[] { 1, 301, 20, 50001, 4000 });foreach (int value in this.observableList){Debug.LogError($"正常数据:{value}");}ISynchronizedView<int, string> observableListSynchronizedView = this.observableList.CreateView((value) => $"数据: {value}");Debug.LogError($"数据长度:{observableListSynchronizedView.Count} ========================");observableListSynchronizedView.AttachFilter(value => value % 2 == 0);foreach (string value in observableListSynchronizedView){Debug.LogError($"{value}");}Debug.LogError($"======================== 数据正常过滤 ========================");this.observableList.Reverse();foreach (string value in observableListSynchronizedView){Debug.LogError($"{value}");}Debug.LogError($"======================== 数据反转数据长度 ========================");observableListSynchronizedView.ResetFilter();foreach (string value in observableListSynchronizedView){Debug.LogError($"{value}");}Debug.LogError($"======================== 取消数据过滤:{observableListSynchronizedView.Count} ========================");this.observableList.Sort();foreach (string value in observableListSynchronizedView){Debug.LogError($"{value}");}Debug.LogError($"======================== 数据排序 Sort ========================");this.observableList.Sort(new DescendantComparer());foreach (string value in observableListSynchronizedView){Debug.LogError($"{value}");}Debug.LogError($"======================== 数据排序 自定义 ========================");observableListSynchronizedView.Dispose();}struct DescendantComparer : IComparer<int>{public int Compare(int x, int y){return y.CompareTo(x);}}}}