Unity C# 字典序列化(结构体/普通类 + 列表 == 显示字典内容)

字典序列化(结构体/普通类 + 列表 == 显示字典内容)

结论

unity C# 不支持字典序列化
文下三种:
可以,结构体+数组/列表
可以,类([System.Serializable],不:MonoBehavior)+ 数组/列表
不可以,类([System.Serializable],:MonoBehavior)+ 数组/列表
再给字典赋值,也就是字典本身不进行序列化显示

准备

InitDictionary()中加载Sprite,自己加图和修改路径:Resources.Load<Sprite>("5_25 aircraft/PNG/" + level + "-01");

代码

using System.Collections.Generic;
using UnityEngine;


namespace NewBehaviourScript0602
{
    public class GameManager : MonoBehaviour
    {
        #region 结构体与列表
        [System.Serializable]
        public struct PlaneStruct
        {
            public int level;
            public Sprite sprite;
        }
        public List<PlaneStruct> structList = new List<PlaneStruct>();
        #endregion
        #region 类与列表
        [System.Serializable]
        public class PlaneClass
        {
            public int level;
            public Sprite sprite;
        }
        public List<PlaneClass> classList = new List<PlaneClass>();
        #endregion
        #region MonoBehaviour类与列表
        [System.Serializable]
        public class PlaneMonoClass:MonoBehaviour
        {
            public int level;
            public Sprite sprite;

        }
        public List<PlaneMonoClass> monoClassList = new List<PlaneMonoClass>();
        #endregion
        #region 字典
        public Dictionary<int, Sprite> dictionary = new Dictionary<int, Sprite>();
        public int maxLevel = 21;
        #endregion

    
        // Start is called before the first frame update
        void Start()
        {
            dictionary=InitDictionary();
            structList= DictionaryToStructList( dictionary);
            classList= DictionaryToClassList(dictionary);
            monoClassList= DictionaryToMonoClassList(dictionary);

        }
        #region 辅助方法
        Dictionary<int, Sprite> InitDictionary()
        {
            Dictionary<int, Sprite> dictionary = new Dictionary<int, Sprite>();
            for (int i = 1; i < maxLevel + 1; i++)
            {
                int level = i;
                Sprite sprite = Resources.Load<Sprite>("5_25 aircraft/PNG/" + level + "-01");
                if (sprite != null)
                    dictionary.Add(level, sprite);
                else
                    throw new System.Exception("最高级没图片异常");
            }

            return dictionary;
        }
        /// <summary>为了显示字典内容,用列表来装</summary>
        List<PlaneStruct> DictionaryToStructList(Dictionary<int, Sprite> dictionary)
        {
            List<PlaneStruct> list = new List<PlaneStruct>();
            for (int i = 0; i < dictionary.Count; i++)
            {
                int level = i + 1;
                PlaneStruct planeStruct;
                planeStruct.level = level;
                dictionary.TryGetValue(level, out planeStruct.sprite);
                list.Add(planeStruct);
            }

            return list;
        }
        /// <summary>为了显示字典内容,用列表来装</summary>
        List<PlaneClass> DictionaryToClassList(Dictionary<int, Sprite> dictionary)
        {
            List<PlaneClass> list = new List<PlaneClass>();
            for (int i = 0; i < dictionary.Count; i++)
            {
                int level = i + 1;
                PlaneClass plane = new PlaneClass() ;
                plane.level = level;
                dictionary.TryGetValue(level, out plane.sprite);
                list.Add(plane);
            }

            return list;
        }
        /// <summary>为了显示字典内容,用列表来装</summary>
        List<PlaneMonoClass> DictionaryToMonoClassList(Dictionary<int, Sprite> dictionary)
        {
            List<PlaneMonoClass> list = new List<PlaneMonoClass>();
            for (int i = 0; i < dictionary.Count; i++)
            {
                int level = i + 1;
                PlaneMonoClass plane = new PlaneMonoClass();
                plane.level = level;
                dictionary.TryGetValue(level, out plane.sprite);
                list.Add(plane);
            }

            return list;
        }
        #endregion
    }
}


在这里插入图片描述

Jason Storey

链接敏感
在这里插入图片描述

文本赋值----------------------------

using System;
using UnityEngine;


namespace NewBehaviourScript00
{
    public class Test : MonoBehaviour
    {
        #region 属性

        [Header("姓名")]
        //[SerializeField, Required]//找不到Required
        [SerializeField]
        TextAsset _file;
        //
        [SerializeField]
        string[] _names;
        
        #endregion
        #region 系统函数
        
        //验证,编辑器有值改变就运行
        void OnValidate()
        {
            _names = _file
                ? _file.text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                : null;
        }
        #endregion 
    }
}

效果

在这里插入图片描述

视频后半部分代码不完整的,做不了

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值