public class SkillData
{
public int id;
public string name;
public enum Type { Attack, Buff, DeBuff, Special }
public Type type;
[System.Flags]
public enum Tags
{
None = 0,
Melee = 0x1,
Ranged = 0x2,
AoE = 0x4
}
public Tags tags;
public bool read;
public float cooldown;
public int cost;
}
[System.Serializable]
public class SkillDatas
{
public SkillData[] skills;
}
void Start()
{
TextAsset jsonFile = Resources.Load<TextAsset>("SkillDatas");
SkillDatas skillDatas = JsonUtility.FromJson<SkillDatas>(jsonFile.text);
foreach (SkillData s in skillDatas.skills)
{
GameObject button = Instantiate(buttonPrefab, content);
button.GetComponentInChildren<Text>().text = FormatSkillInfo(s);
Debug.Log(s.tags);
}
LayoutRebuilder.ForceRebuildLayoutImmediate(content.GetComponent<RectTransform>());
}
private string FormatSkillInfo(SkillData s)
{
return $"{s.name}\n" +
$"ID: {s.id}\t" +
$"\tType: {s.type}\t" +
$"\tTags: {s.tags.ToString()}\n" +
$"Read: {(s.read ? "Yes" : "No")}\t" +
$"\tCooldown: {s.cooldown}s\t" +
$"\tCost: {s.cost}";
}
上面是一个存放在JSON中的数据类型以及解析代码,JSON具体内容为
{
"skills": [
{
"id": 101,
"name": "Power Slash",
"type": "Attack",
"tags": "Melee",
"read": true,
"cooldown": 3.0,
"cost": 10
},
{
"id": 102,
"name": "Fireball",
"type": "Attack",
"tags": "Ranged, AoE",
"read": true,
"cooldown": 5.0,
"cost": 20
},
//其他数据
}
在解析后分批把内容显示在button的text上,但是唯独tags显示全是None
