[Unity][ILRuntime][C#]热更新中数组Dictionary通过value获得key

这篇博客主要探讨了在Unity使用ILRuntime进行热更新时,如何处理C# Dictionary类型的数据。由于Dictionary的灵活性,作者提到了在热更新环境中通过FirstOrDefault方法减少冗余代码的需求,但指出此方法需要在ILRuntime中注册。文章引用了Unity编辑器的错误信息,并提供了ILRuntimeWrapper.cs文件的相关内容,以及针对热更新报错的解决办法,特别是当涉及通过Dictionary值获取键的问题。

Unity编辑器报错

KeyNotFoundException: Cannot find Delegate Adapter for:scienceTree/<>c__DisplayClass15_0.<click>b__1(KeyValuePair`2 q), Please add following code:
appdomain.DelegateManager.RegisterFunctionDelegate<System.Collections.Generic.KeyValuePair<ILRuntime.Runtime.Intepreter.ILTypeInstance, FairyGUI.GButton>, System.Boolean>();

参考资料1

由于Dictionary<T,T1>太过灵活,T什么类型的类都可以,通过FirstOrDefault减少冗余代码。

但是FirstOrDefault在热更新中得注册。

...
using FairyGUI;
using System.Linq;//FirstOrDefault
...
if (dict.ContainsValue(gb))
            {
                Debug.Log("click  item3:" + gb.id + "/name:" + gb.name + "/");
                //dict.
                //var keys = dict.Where(q => q.Value == gb).Select(q => q.Key);
                var firstKey = dict.FirstOrDefault(q => q.Value == gb).Key;
                Debug.Log("click  item1:" + gb.id + "/name:" + gb.name);
### Unity 字典根据 Value 获取 Key 的方法 在 Unity 中,`Dictionary<TKey, TValue>` 是一种常用的数据结构,用于存储键值对。然而,C# 的 `Dictionary` 并不直接支持通过值(Value)获取键(Key)。因此,需要采用其他方式实现这一功能。以下是几种常见的实现方法,并结合引用内容进行详细说明。 #### 方法 1:使用 `foreach` 遍历字典 可以通过遍历字典中的所有键值对来查找与指定值匹配的键。这种方法简单易懂,适合小型数据集[^4]。 ```csharp private void GetDicKeyByValue(Dictionary<string, string> dic, string targetValue) { foreach (KeyValuePair<string, string> kvp in dic) { if (kvp.Value.Equals(targetValue)) { // 找到对应的键 string key = kvp.Key; Debug.Log($"Key: {key}, Value: {targetValue}"); } } } ``` #### 方法 2:使用 `dic.Keys` 和索引访问 另一种方式是遍历字典的所有键,然后通过键访问对应的值进行比较。这种方式在逻辑上稍显复杂,但功能等价于方法 1。 ```csharp private void GetDicKeyByValue(Dictionary<string, string> dic, string targetValue) { foreach (string key in dic.Keys) { if (dic[key].Equals(targetValue)) { // 找到对应的键 Debug.Log($"Key: {key}, Value: {targetValue}"); } } } ``` #### 方法 3:使用 LINQ 查询 如果项目中允许使用 LINQ,可以利用其强大的查询功能简化代码。LINQ 提供了更简洁的语法来处理集合操作。 ```csharp using System.Linq; private void GetDicKeyByValue(Dictionary<string, string> dic, string targetValue) { // 获取所有匹配的键 var keys = dic.Where(q => q.Value == targetValue).Select(q => q.Key); // 如果只需要第一个键 string firstKey = dic.FirstOrDefault(q => q.Value == targetValue).Key; Debug.Log($"Keys matching value '{targetValue}': {string.Join(", ", keys)}"); } ``` #### 注意事项 - 在 Unity 中使用字典时,确保键的不可变性以避免意外行为[^1]。 - 如果字典中可能存在多个键对应同一个值,则上述方法会返回所有匹配的键。如果只需要第一个匹配的键,可以使用 `FirstOrDefault` 方法。 - 在热更新场景下,若遇到 `KeyNotFoundException`,需检查是否正确注册了委托适配器[^3]。 ### 示例代码 以下是一个完整的示例,展示了如何在 Unity 中通过字典的值获取键: ```csharp using System.Collections.Generic; using System.Linq; using UnityEngine; public class DictionaryExample : MonoBehaviour { private void Start() { Dictionary<string, string> dic = new Dictionary<string, string> { { "1", "1" }, { "2", "2" }, { "3", "2" } }; string targetValue = "2"; // 方法 1: 使用 foreach 遍历 foreach (KeyValuePair<string, string> kvp in dic) { if (kvp.Value.Equals(targetValue)) { Debug.Log($"Method 1 - Key: {kvp.Key}, Value: {targetValue}"); } } // 方法 2: 使用 LINQ 查询 var keys = dic.Where(q => q.Value == targetValue).Select(q => q.Key); Debug.Log($"Method 2 - Keys matching value '{targetValue}': {string.Join(", ", keys)}"); // 方法 3: 获取第一个匹配的键 string firstKey = dic.FirstOrDefault(q => q.Value == targetValue).Key; Debug.Log($"Method 3 - First key matching value '{targetValue}': {firstKey}"); } } ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值