You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity C#中如何用值为列表的字典存储多组定义数据?

方案可行性与实现指南

用Dictionary<string, List<string>>来存储每个键对应8个定义的需求完全可行,这种结构能很好地实现“单个键关联多组结构化数据”的场景,非常适配你的需求。

先修正你现有代码的问题

你当前的代码存在两个核心错误:

  1. 直接在类的字段区域调用myDict.Add()——这类操作必须放在方法内部(比如Start()),否则会编译失败。
  2. 字典类型是Dictionary<string, string>,只能存储单值,无法满足“一个键对应8个定义”的需求,需要改成Dictionary<string, List<string>>。

完整实现代码示例

using System;
using System.Collections.Generic;
using UnityEngine;

public class Dictionary_matches : MonoBehaviour
{
    // 定义字典:键为字符串,值为包含8个字符串的列表
    public static Dictionary<string, List<string>> Matches_dic = new Dictionary<string, List<string>>();

    void Start()
    {
        // 1. 添加新条目(示例:给Australia添加8个定义)
        AddNewEntry("Australia", new List<string> 
        { 
            "Canberra", "Oceania", "English", "25.7 million", 
            "Dollar", "Kangaroo", "Uluru", "Summer in December" 
        });

        // 2. 读取整个条目
        if (TryGetEntry("Australia", out List<string> australiaInfo))
        {
            Debug.Log("Australia的所有定义:");
            foreach (string info in australiaInfo)
            {
                Debug.Log(info);
            }
        }

        // 3. 按索引读取特定项(比如取第3个定义,索引从0开始)
        string thirdDefinition = GetEntryByIndex("Australia", 2);
        if (!string.IsNullOrEmpty(thirdDefinition))
        {
            Debug.Log($"Australia的第3个定义:{thirdDefinition}");
        }

        // 4. 修改特定索引的内容
        UpdateEntryByIndex("Australia", 0, "Canberra (Capital)");
        Debug.Log($"修改后Australia的第1个定义:{GetEntryByIndex("Australia", 0)}");

        // 5. 删除条目
        RemoveEntry("Australia");
    }

    // 封装添加条目的方法
    public static void AddNewEntry(string key, List<string> definitions)
    {
        // 确保传入的列表正好有8个元素
        if (definitions.Count != 8)
        {
            Debug.LogError($"添加失败:{key}的定义数量必须是8个");
            return;
        }

        // 避免重复添加相同键
        if (!Matches_dic.ContainsKey(key))
        {
            Matches_dic.Add(key, definitions);
        }
        else
        {
            Debug.LogWarning($"键{key}已存在,跳过添加");
        }
    }

    // 封装读取整个条目的方法(安全处理键不存在的情况)
    public static bool TryGetEntry(string key, out List<string> definitions)
    {
        return Matches_dic.TryGetValue(key, out definitions);
    }

    // 封装按索引读取特定项的方法
    public static string GetEntryByIndex(string key, int index)
    {
        if (Matches_dic.TryGetValue(key, out List<string> definitions))
        {
            // 检查索引是否合法(0到7,因为8个元素)
            if (index >= 0 && index < 8)
            {
                return definitions[index];
            }
            else
            {
                Debug.LogError($"索引{index}非法,必须在0-7之间");
            }
        }
        else
        {
            Debug.LogError($"键{key}不存在");
        }
        return null;
    }

    // 封装修改特定索引项的方法
    public static void UpdateEntryByIndex(string key, int index, string newContent)
    {
        if (Matches_dic.TryGetValue(key, out List<string> definitions))
        {
            if (index >= 0 && index < 8)
            {
                definitions[index] = newContent;
            }
            else
            {
                Debug.LogError($"索引{index}非法,必须在0-7之间");
            }
        }
        else
        {
            Debug.LogError($"键{key}不存在");
        }
    }

    // 封装删除条目的方法
    public static void RemoveEntry(string key)
    {
        if (Matches_dic.ContainsKey(key))
        {
            Matches_dic.Remove(key);
            Debug.Log($"已删除键{key}的条目");
        }
        else
        {
            Debug.LogWarning($"键{key}不存在,无法删除");
        }
    }
}

关键操作说明

  • 添加条目:通过AddNewEntry方法强制每个条目包含8个定义,同时避免重复键的问题。
  • 按索引读取:用GetEntryByIndex方法,先检查键是否存在,再验证索引是否在0-7范围内(列表从0开始计数,8个元素的索引区间是0到7),避免运行时异常。
  • 安全读取:使用TryGetValue代替直接索引字典,防止键不存在时抛出异常。

内容的提问来源于stack exchange,提问作者Çağan Kahraman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 22:00:57