字典序列化(结构体/普通类 + 列表 == 显示字典内容)
结论
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
}
}
效果
视频后半部分代码不完整的,做不了