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

news2025/6/9 17:19:55

1、ObservableList 基础 List 类型测试

    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);
				}
			}
		}
	}

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

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}");
			}
		}
	}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2405627.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

LangGraph--Agent工作流

Agent的工作流 下面展示了如何创建一个“计划并执行”风格的代理。 这在很大程度上借鉴了 计划和解决 论文以及Baby-AGI项目。 核心思想是先制定一个多步骤计划&#xff0c;然后逐项执行。完成一项特定任务后&#xff0c;您可以重新审视计划并根据需要进行修改。 般的计算图如…

Spring Boot 常用注解面试题深度解析

&#x1f91f;致敬读者 &#x1f7e9;感谢阅读&#x1f7e6;笑口常开&#x1f7ea;生日快乐⬛早点睡觉 &#x1f4d8;博主相关 &#x1f7e7;博主信息&#x1f7e8;博客首页&#x1f7eb;专栏推荐&#x1f7e5;活动信息 文章目录 Spring Boot 常用注解面试题深度解析一、核心…

Linux系统的CentOS7发行版安装MySQL80

文章目录 前言Linux命令行内的”应用商店”安装CentOS的安装软件的yum命令安装MySQL1. 配置yum仓库2. 使用yum安装MySQL3. 安装完成后&#xff0c;启动MySQL并配置开机自启动4. 检查MySQL的运行状态 MySQL的配置1. 获取MySQL的初始密码2. 登录MySQL数据库系统3. 修改root密码4.…

408第一季 - 数据结构 - 栈与队列

栈 闲聊 栈是一个线性表 栈的特点是后进先出 然后是一个公式 比如123要入栈&#xff0c;一共有5种排列组合的出栈 栈的数组实现 这里有两种情况&#xff0c;&#xff0c;一个是有下标为-1的&#xff0c;一个没有 代码不用看&#xff0c;真题不会考 栈的链式存储结构 L ->…

【RTP】Intra-Refresh模式下的 H.264 输出,RTP打包的方式和普通 H.264 流并没有本质区别

对于 Intra-Refresh 模式下的 H.264 输出,RTP 打包 的方式和普通 H.264 流并没有本质区别:你依然是在对一帧一帧的 NAL 单元进行 RTP 分包,只不过这些 NAL 单元内部有部分宏块是 “帧内编码” 而已。下面分步骤说明: 1. 原理回顾:RFC 6184 H.264 over RTP 按照 RFC 6184 …

Redis实战-消息队列篇

前言&#xff1a; 讲讲做消息队列遇到的问题。 今日所学&#xff1a; 异步优化消息队列基于stream实现异步下单 1. 异步优化 1.1 需求分析 1.1.1 现有下单流程&#xff1a; 1.查询优惠劵 2.判断是否是秒杀时间&#xff0c;库存是否充足 3.实现一人一单 在这个功能中&…

(三)Linux性能优化-CPU-CPU 使用率

CPU使用率 user&#xff08;通常缩写为 us&#xff09;&#xff0c;代表用户态 CPU 时间。注意&#xff0c;它不包括下面的 nice 时间&#xff0c;但包括了 guest 时间。nice&#xff08;通常缩写为 ni&#xff09;&#xff0c;代表低优先级用户态 CPU 时间&#xff0c;也就是进…

佰力博科技与您探讨材料介电性能测试的影响因素

1、频率依赖性 材料的介电性能通常具有显著的频率依赖性。在低频下&#xff0c;偶极子的取向极化占主导&#xff0c;介电常数较高&#xff1b;而在高频下&#xff0c;偶极子的取向极化滞后&#xff0c;导致介电常数下降&#xff0c;同时介电损耗增加。例如&#xff0c;VHB4910…

K8S认证|CKS题库+答案| 4. RBAC - RoleBinding

目录 4. RBAC - RoleBinding 免费获取并激活 CKA_v1.31_模拟系统 题目 开始操作&#xff1a; 1&#xff09;、切换集群 2&#xff09;、查看SA和role 3&#xff09;、编辑 role-1 权限 4&#xff09;、检查role 5&#xff09;、创建 role和 rolebinding 6&#xff0…

React 新项目

使用git bash 创建一个新项目 建议一开始就创建TS项目 原因在Webpack中改配置麻烦 编译方法:ts compiler 另一种 bable 最好都配置 $ create-react-app cloundmusic --template typescript 早期react项目 yarn 居多 目前npm包管理居多 目前pnpm不通用 icon 在public文件夹中…

AI编程在BOSS项目的实践经验分享

前言 在人工智能技术革新浪潮的推动下&#xff0c;智能编程助手正以前所未有的速度重塑开发领域。这些基于AI的代码辅助工具通过智能提示生成、实时错误检测和自动化重构等功能&#xff0c;显著提升了软件工程的全流程效率。无论是初入行业的开发者还是资深程序员&#xff0c;…

数学:”度量空间”了解一下?

度量空间是现代数学中一种基本且重要的抽象空间。以下是对它的详细介绍&#xff1a; 定义 相关概念 常见的度量空间举例 度量空间的类型 度量空间的作用 度量空间是拓扑空间的一种特殊情况&#xff0c;它为拓扑空间的研究提供了具体的模型和实例。同时&#xff0c;度量空间在…

jenkins脚本查看及备份

位置与备份 要完整备份 Jenkins 的所有脚本和相关配置&#xff0c;包括 Jenkinsfile、构建脚本&#xff08;如 .sh / .bat&#xff09;、Job 配置、插件、凭据等&#xff0c;你可以从两个层面入手&#xff1a; ✅ 一、完整备份 Jenkins 主目录&#xff08;最全面&#xff09; …

用电脑通过网口控制keysight示波器

KEYSIGHT示波器HD304MSO性能 亮点: 体验 200 MHz 至 1 GHz 的带宽和 4 个模拟通道。与 12 位 ADC 相比,使用 14 位模数转换器 (ADC) 将垂直分辨率提高四倍。使用 10.1 英寸电容式触摸屏轻松查看和分析您的信号。捕获 50 μVRMS 本底噪声的较小信号。使用独有区域触摸在几秒…

《如何使用MinGW-w64编译OpenCV和opencv_contrib》

《如何使用MinGW-w64编译OpenCV和opencv_contrib》 在Windows环境下使用MinGW编译OpenCV和opencv_contrib是一个常见需求,尤其是对于那些希望使用GCC工具链而非Visual Studio的开发者。下面我将详细介绍这个过程。 准备工作 首先需要安装和准备以下工具和库: MinGW(建议使…

模拟搭建私网访问外网、外网访问服务器服务的实践操作

目录 实验环境 实践要求 一、准备工作 1、准备四台虚拟机&#xff0c;分别标号 2、 防火墙额外添加两块网卡&#xff0c;自定义网络连接模式 3、 关闭虚拟机的图形管理工具 4、关闭防火墙 5、分别配置四台虚拟机的IP地址&#xff0c;此处举一个例子&#xff08;使用的临…

vue中Echarts的使用

文章目录 Echarts概述什么是EchartsEcharts的好处 Vue中Echarts的使用Echarts的安装Echarts的引入 Echarts概述 什么是Echarts Apache ECharts&#xff1a;一个基于 JavaScript 的开源可视化图表库。 其官网如下&#xff1a;https://echarts.apache.org/zh/index.html Echar…

【C++项目】负载均衡在线OJ系统-1

文章目录 前言项目结果演示技术栈&#xff1a;结构与总体思路compiler编译功能-common/util.hpp 拼接编译临时文件-common/log.hpp 开放式日志-common/util.hpp 获取时间戳方法-秒级-common/util.hpp 文件是否存在-compile_server/compiler.hpp 编译功能编写&#xff08;重要&a…

Spring Boot微服务架构(十一):独立部署是否抛弃了架构优势?

Spring Boot 的独立部署&#xff08;即打包为可执行 JAR/WAR 文件&#xff09;本身并不会直接丧失架构优势&#xff0c;但其是否体现架构价值取决于具体应用场景和设计选择。以下是关键分析&#xff1a; 一、独立部署与架构优势的关系 内嵌容器的优势保留 Spring Boot 独立部署…

(四)Linux性能优化-CPU-软中断

软中断 中断其实是一种异步的事件处理机制&#xff0c;可以提高系统的并发处理能力 由于中断处理程序会打断其他进程的运行&#xff0c;所以&#xff0c;为了减少对正常进程运行调度的影响&#xff0c;中断处理程序就需要尽可能快地运行 Linux 将中断处理过程分成了两个阶段&a…