【Unity】R3 CSharp 响应式编程 - 使用篇(集合)(三)

1、ObservableList 基础 List 类型测试

cs 复制代码
    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 过滤

cs 复制代码
    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(排序和反转)

cs 复制代码
    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);
				}
			}
		}
	}

4、List ObservableCollections Subscribe(数据事件订阅)

cs 复制代码
using System.Collections.Generic;
	using ObservableCollections;
	using R3;
	using UnityEngine;

	namespace Aladdin.Standard.Observable.Collections.List
	{
		/// <summary> 这里主要是测试集合添加数据移除数据的回调测试, Package: ObservableCollections.R3 </summary>
		public class ObservableListSubscribe : MonoBehaviour
		{
			protected readonly ObservableList<int> observableList = new ObservableList<int>();

			public void Start()
			{
				this.observableList.ObserveAdd().Subscribe(this.ObservableListAdd);
				this.observableList.ObserveChanged().Subscribe(this.ObservableListChanged);
				this.observableList.ObserveRemove().Subscribe(this.ObservableListRemove);
				this.observableList.ObserveReplace().Subscribe(this.ObservableListReplace);
				this.observableList.ObserveMove().Subscribe(this.ObservableListMove);
				this.observableList.ObserveReset().Subscribe(this.ObservableListReset);
				this.observableList.ObserveClear().Subscribe(this.ObservableListClear);
				this.observableList.ObserveReverse().Subscribe(this.ObservableListReverse);
				this.observableList.ObserveSort().Subscribe(this.ObservableListSort);
				this.observableList.ObserveCountChanged().Subscribe(this.ObservableListCountChanged);


				this.observableList.Add(1);
				this.observableList.Add(20);
				this.observableList.AddRange(new[] { 30, 31, 32 });

				this.observableList[1] = 60;
				this.observableList.Move(1, 0);
				this.observableList.RemoveAt(0);
				this.observableList.Move(1, 0);
				this.observableList.Sort();
				this.observableList.Sort(new DescendantComparer());

				foreach (int value in this.observableList)
				{
					Debug.LogError($"{value}");
				}
			}
			
			struct DescendantComparer : IComparer<int>
			{
				public int Compare(int x, int y)
				{
					return y.CompareTo(x);
				}
			}
			
			internal void ObservableListCountChanged(int count)
			{
				Debug.Log($"数据长度变动:{count}");

			}

			internal void ObservableListSort((int Index, int Count, IComparer<int> Comparer) sortTuple)
			{
				Debug.Log($"数据排序:{sortTuple.Index} {sortTuple.Count} {sortTuple.Comparer}");

			}

			internal void ObservableListReverse((int Index, int Count) reverseTuple)
			{
				Debug.Log($"数据反转:{reverseTuple.Index} {reverseTuple.Count}");

			}
			
			internal void ObservableListClear(Unit unit)
			{
				Debug.Log($"数据清理:{unit}");
			}
			
			internal void ObservableListReset(CollectionResetEvent<int> collectionEvent)
			{
				Debug.Log($"数据重置:{collectionEvent.Index} isClear={collectionEvent.IsClear} isReverse={collectionEvent.IsReverse} isSort={collectionEvent.IsSort}");

			}

			internal void ObservableListMove(CollectionMoveEvent<int> collectionEvent)
			{
				Debug.Log($"数据移动:ni={collectionEvent.NewIndex} oi={collectionEvent.OldIndex} v={collectionEvent.Value}");

			}

			internal void ObservableListChanged(CollectionChangedEvent<int> changedEvent)
			{
				Debug.Log($"数据变动:o={changedEvent.OldItem} n={changedEvent.NewItem}");
			}

			internal void ObservableListReplace(CollectionReplaceEvent<int> replaceEvent)
			{
				Debug.Log($"数据替换:o={replaceEvent.OldValue} n={replaceEvent.NewValue}");
			}

			internal void ObservableListRemove(CollectionRemoveEvent<int> removeEvent)
			{
				Debug.Log($"删除数据:o={removeEvent.Value}");
			}

			internal void ObservableListAdd(CollectionAddEvent<int> addEvent)
			{
				Debug.Log($"添加数据:{addEvent}");
			}
		}
	}
相关推荐
mCell1 小时前
从删库到跑路?这50个Linux命令能保你职业生涯
linux·windows·macos
dualven_in_csdn2 小时前
electron 使用记录
windows
zz9602263 小时前
Windows Server存储池,虚拟磁盘在系统启动后不自动连接需要手动连接
windows
死也不注释4 小时前
【鸡零狗碎记录】
unity·c#
★YUI★7 小时前
学习游戏制作记录(剑投掷技能)7.26
学习·游戏·unity·c#
吳所畏惧9 小时前
NVM踩坑实录:配置了npm的阿里云cdn之后,下载nodejs老版本(如:12.18.4)时,报404异常,下载失败的问题解决
前端·windows·阿里云·npm·node.js·batch命令
leese2339 小时前
FreeMarker模板引擎
windows
love530love10 小时前
命令行创建 UV 环境及本地化实战演示—— 基于《Python 多版本与开发环境治理架构设计》的最佳实践
开发语言·人工智能·windows·python·conda·uv
呉師傅10 小时前
佳能iR-ADV C5560复印机如何扫描文件到电脑
运维·网络·windows·计算机外设·电脑
程序视点10 小时前
【最新专业评测】PDF Reducer专业版:85%超高压缩率的PDF压缩神器|Windows最佳PDF压缩工具推荐
windows