通过反射获取数据库的数据 将其转化为相应类型的数据
时间:2021-07-01 10:21:17
帮助过:2人阅读
using System;
2 using UnityEngine;
3
4 namespace ARPGSimpleDemo.Skill
5 {
6 /// <summary>
7 /// 技能数据
8 /// </summary>
9 [Serializable]
10 public class SkillData
11 {
12 /// <summary>技能拥有者</summary>
13 [HideInInspector]
14 public GameObject Owner;
15 /// <summary>技能编号</summary>
16 public int skillID{
set;
get;}
17 /// <summary>技能图标</summary>
18 public string skillIcon {
set;
get;}
19 /// <summary>描述</summary>
20 public string description{
set;
get;}
21 /// <summary>技能名称</summary>
22 public string name{
set;
get;}
23 /// <summary>持续时间</summary>
24 public float durationTime{
set;
get;}
25 /// <summary>在持续时间内,两次伤害之间的间隔时间</summary>
26 public float damageInterval{
set;
get;}
27 /// <summary>伤害比</summary>
28 public float damage {
set;
get;}
29 /// <summary>冷却时间</summary>
30 public int coolTime{
set;
get;}
31 /// <summary>冷却剩余</summary>
32 public int coolRemain;
33 /// <summary>魔法消耗</summary>
34 public int costSP{
set;
get;}
35 /// <summary>攻击距离</summary>
36 public float attackDisntance{
set;
get;}
37 /// <summary>攻击目标</summary>
38 [HideInInspector]
39 public GameObject[] attackTargets;
40 /// <summary>攻击目标的TAG</summary>
41 public string[] attckTargetTags{
set;
get;}
42 /// <summary>技能等级</summary>
43 public int level{
set;
get;}
44 /// <summary>技能预制对象</summary>
45 public GameObject skillPrefab;
46 /// <summary>预制文件名</summary>
47 public string prefabName{
set;
get;}
48 /// <summary>攻击范围 线形,矩形,扇形,圆形</summary>
49 public DamageMode damageMode{
set;
get;}
50 /// <summary>攻击类型,单攻,群攻</summary>
51 public SkillAttackType attackType{
set;
get;}
52 /// <summary>是否激活</summary>
53 public bool Activated;
54 /// <summary>技能对应的动画名称 </summary>
55 public string animtionName{
set;
get;}
56 /// <summary> 攻击范围角度</summary>
57 public int attackAngle{
set;
get;}
58 /// <summary>目标受击特效</summary>
59 public string hitFxName{
set;
get;}
60 public GameObject hitFxPrefab;
61 /// <summary>下一个连击技能编号</summary>
62 public int nextBatterId{
set;
get;}
63
64 }
65 }
下面是获取数据 并放入
1 /// <summary>
2 /// 将数据库数据放入技能管理类中
3 /// </summary>
4 /// <param name="jobId">职业ID</param>
5 void InitSkill(int jobId)
6 {
7 //先将数据库打开
8 OperatingDB.Instance.CreateDataBase();
9 //遍历表中所有行
10 SqliteDataReader skill = OperatingDB.Instance.db.ReadFullTable("T_Skill" + jobId);
11 while (skill.Read())
12 {
13 SkillData sd = new SkillData();
14 //反射获取
15 Type t = typeof(SkillData);
16 int i = 0;
17 //遍历SkillData所有属性 t.GetProperties
18 foreach (var item in t.GetProperties())
19 {
20 i++;
21 //获取属性 判断 是否为 string
22 if (item.PropertyType.Equals(typeof(string)))
23 item.SetValue(sd, skill[i].ToString(), null); //赋值
24 //获取属性 判断 是否为 float
25 else if (item.PropertyType.Equals(typeof(float)))
26 item.SetValue(sd, float.Parse(skill[i].ToString()), null);
27 //获取属性 判断 是否为 string[]
28 else if (item.PropertyType.Equals(typeof(string[])))
29 {
30 string[] str = skill[i].ToString().Split(‘,‘);
31 item.SetValue(sd, str, null);
32 }
33 //获取属性 其余 其中枚举可以与int做转换
34 else
35 item.SetValue(sd, int.Parse(skill[i].ToString()), null);
36 }
37 //获取物体本身的技能管理类 将得到的类传入
38 GetComponent<CharacterSkillManager>().skills.Add(sd);
39 }
40 //关闭数据库
41 OperatingDB.Instance.db.CloseSqlConnection();
42 }
通过反射获取数据库的数据 将其转化为相应类型的数据
标签: