栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 游戏开发 > 其他

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

其他 更新时间:发布时间: 百科书网 趣学号
字典序列化(结构体/普通类 + 列表 == 显示字典内容) 结论

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

准备

InitDictionary()中加载Sprite,自己加图和修改路径:Resources.Load("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 structList = new List();
        #endregion
        #region 类与列表
        [System.Serializable]
        public class PlaneClass
        {
            public int level;
            public Sprite sprite;
        }
        public List classList = new List();
        #endregion
        #region MonoBehaviour类与列表
        [System.Serializable]
        public class PlaneMonoClass:MonoBehaviour
        {
            public int level;
            public Sprite sprite;

        }
        public List monoClassList = new List();
        #endregion
        #region 字典
        public Dictionary dictionary = new Dictionary();
        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 InitDictionary()
        {
            Dictionary dictionary = new Dictionary();
            for (int i = 1; i < maxLevel + 1; i++)
            {
                int level = i;
                Sprite sprite = Resources.Load("5_25 aircraft/PNG/" + level + "-01");
                if (sprite != null)
                    dictionary.Add(level, sprite);
                else
                    throw new System.Exception("最高级没图片异常");
            }

            return dictionary;
        }
        /// 为了显示字典内容,用列表来装
        List DictionaryToStructList(Dictionary dictionary)
        {
            List list = new List();
            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;
        }
        /// 为了显示字典内容,用列表来装
        List DictionaryToClassList(Dictionary dictionary)
        {
            List list = new List();
            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;
        }
        /// 为了显示字典内容,用列表来装
        List DictionaryToMonoClassList(Dictionary dictionary)
        {
            List list = new List();
            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 
    }
}

效果

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

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/1065670.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号