c#字典实现(一个目标下多个一一对应的属性、属性值对)

效果如下视频:

sorry,视频才上传,暂时无法在这里显示

参照以下程序修改(由AI生成)

unity中的UI界面(dpd是dropdown组件,后面相对应的为InputField组件)

结果内容:如下

一,同一类中,针对不同IP进行属性值存储;二,不同类时,区分的相同IP作为不同,并行存在

c#程序如下:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text;
public class test : MonoBehaviour
{
    Dictionary<string, Dictionary<string, string>> dict = new Dictionary<string, Dictionary<string, string>>();//设计字典格式
    List<string> attribute_dic = new List<string>();//字典属性
    List<string> value_dic = new List<string>();//字典属性值
    int attribute_count=0;//在自动保存时启用计数
    public InputField question_in_uri,question_in_attribute;
    public Dropdown[] dpd;
    void Start()
    {//在修改或选择时,实现自动增加或修改字典内容
        question_in_attribute.onValueChanged.AddListener(OnInputFieldValueChanged);
        question_in_uri.onValueChanged.AddListener(OnInputFieldValueChanged_uri);
        dpd[1].onValueChanged.AddListener(Showfollow);
        dpd[0].onValueChanged.AddListener(Showfollow_uri);
    }
    void OnInputFieldValueChanged(string text)
    {//为了获取当前属性的值,以实现前后的替换
        attribute_count=dpd[1].value;
    }
    void OnInputFieldValueChanged_uri(string text)
    {
        // 打印出当前的文本内容在question_in_uri切换时
        if (dict.ContainsKey(dpd[0].captionText.text+":"+text))
        {
            Dictionary<string, string> appleSubValues = dict[dpd[0].captionText.text+":"+text];//获取该目标
            List<string> appleSubValues_min = new List<string>();
            foreach (var pair in appleSubValues)//读取目标下的信息
            {
                attribute_dic.Add(pair.Key);
                string subValue = pair.Key + ":" + pair.Value;
                appleSubValues_min.Add(subValue);
            }
            foreach (var item in appleSubValues_min)//答应目标信息
            {
                if (item.Split(":")[0]==dpd[1].captionText.text)//进行输出,使得input中有字
                {
                    question_in_attribute.text=item.Split(":")[1];
                    break;
                }
                else
                {
                    question_in_attribute.text=null;
                }
            }
        }else{question_in_attribute.text=null;}
    }
    void Showfollow_uri(int x)
    {// 打印出当前的文本内容在dpd[0]切换时
        OnInputFieldValueChanged_uri(question_in_uri.text);
    }
    void Showfollow(int x)
    {//主函数
        attribute_dic.Clear();//先清除列表内上一阶段信息
        value_dic.Clear();
        String uri_represent=dpd[0].captionText.text+":"+question_in_uri.text;//URI的代替,即dpd[0]内容文本
        List<string> appleSubValues_min = new List<string>();
        
        if (dict.ContainsKey(uri_represent))//查询字典中是否有该值
        {
            Dictionary<string, string> appleSubValues = dict[uri_represent];//有值则取值
            foreach (var pair in appleSubValues)
            {
                attribute_dic.Add(pair.Key);//将字典中该值的信息进行保存,后续统一重新导入,这里是子值,即属性
                string subValue = pair.Key + ":" + pair.Value;
                appleSubValues_min.Add(subValue);//为了最后的取值作为输出显示
            }
            foreach (var item in appleSubValues_min)
            {
                value_dic.Add(item.Split(":")[1]);//将字典中该值的信息进行保存,后续统一重新导入,这里是子值的值,即属性值
            }
            dict[uri_represent].Clear();//清除字典中该值的信息
        }
        if (question_in_uri.text!=null&&question_in_attribute.text!=null)//确保不为空
        {
            if (dict.ContainsKey(uri_represent))//判断是否存在uri
            {dict[uri_represent].Add(dpd[1].options[attribute_count].text, question_in_attribute.text);}//存在则修改
            else{dict.Add(uri_represent, new Dictionary<string, string> { { dpd[1].options[attribute_count].text, question_in_attribute.text } });}//不存在则增加
            appleSubValues_min.Add(dpd[1].options[attribute_count].text+":"+question_in_attribute.text);//将新的值并入到先前存储的位置
            attribute_dic.Add(dpd[1].options[attribute_count].text);value_dic.Add(question_in_attribute.text);//同步增加
            for (int i = 0; i < value_dic.Count; i++)
            {//这里是属性--属性值,读取上一阶段储存的信息,重新归入该值字典
                if (dict[uri_represent].ContainsKey(attribute_dic[i]))//存在,则为修改
                {
                    dict[uri_represent][attribute_dic[i]]=question_in_attribute.text;
                }
                else{dict[uri_represent].Add(attribute_dic[i],value_dic[i]);//不存在,则为新增
                }
            }
        }
        foreach (var item in appleSubValues_min)
        {//打印出信息,作为更改uri时或者更改属性时的显示输出
            if (item.Split(":")[0]==dpd[1].captionText.text)//进行输出,使得input中有字
            {
                question_in_attribute.text=item.Split(":")[1];
                break;
            }
            else
            {
                question_in_attribute.text=null;
            }
        }
        
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值