c# - 初始化字典问题
问题描述
而不仅仅是初始化
Screens (Dictionary<GameObject, string> Screens) values
此代码使用这些关联值创建一个新的游戏对象屏幕。
问题在这里:
Screens[new GameObject(cont.Key)] = cont.Value;
这是屏幕的结果:“屏幕再次使用关联值 RightOuterPad=、LeftOuterPad=、LeftScreen=、RightOuterPad=、OPENPAD1-video-2、LeftOuterPad=OPENPAD1-video-1、LeftScreen=MAINBOARD1-video-2 创建
Dictionary<string, string> content = new Dictionary<string, string>();
content = ReadConfigFile(path);
Dictionary<GameObject, string> Screens = config.Screens;
List<string> ScreensNames = new List<string>();
foreach (var screen in Screens)
{
ScreensNames.Add(screen.Key.name);
}
foreach (KeyValuePair<string, string> cont in content)
{
if (ScreensNames.Contains(cont.Key))
{
Debug.Log(Screens);
Screens[new GameObject(cont.Key)]= cont.Value;
}
}
解决方案
听起来你想做的是
using System.Linq;
...
var keyObjectReference = Screens.keys.FirstOrDefault(o => string.Equals(o.name, cont.Key));
config.Screens[keyObjectReference] = cont.Value;
而是检索第一个具有匹配名称的 GameObject,cont.Key
并使用该引用来访问config.Screens
字典中的正确条目。
推荐阅读
- reactjs - 将 Chakra-UI 添加到现有的 Create-React-App 会搞砸很多事情
- laravel - laravel withsum 附加 where 子句
- symfony - 如何仅为本地环境启用 Symfony Profiler 工具栏?
- android - 如何在中文 android 盒子的 Google Playstore 控制台中查找设备名称,以便将它们从支持的设备中排除?
- python - 将 ARIMAX 预测分解为 exog 变量和 ARIMA 参数的贡献
- android - 如何检测 Jetpack Compose 中变换手势的结束?
- node.js - Node Express - PayloadTooLargeError:请求实体太大
- java - 为什么记录视图和记录按钮会抛出异常?(不是任何其他问题的重复,也没有找到解决方案)
- python-3.x - 将数据框的一列压缩到基于 Python3 中具有相同值的另一列的列表
- python - 关于win10toast和win10toast-click,通知中心有问题