Obtain the data of the database through reflection and convert it to the corresponding type of data to obtain the database

Source: Internet
Author: User

Obtain the data of the database through reflection and convert it to the corresponding type of data to obtain the database

Reflection can dynamically obtain the data Type, and the Type class can obtain the fields, methods, attributes, and so on.

In particular, distinguishing fields from attributes allows us to get what we want. Not much nonsense.

First, write down the data import class:

1 using System; 2 using UnityEngine; 3 4 namespace ARPGSimpleDemo. skill 5 {6 /// <summary> 7 // Skill data 8 /// </summary> 9 [Serializable] 10 public class SkillData11 {12 // <summary> skill Owner </summary> 13 [HideInInspector] 14 public GameObject Owner; 15 /// <summary> Skill No. </summary> 16 public int skillID {set; get ;} 17 /// <summary> Skill icon </summary> 18 public string skillIcon {set; get ;} 19 /// <summary> description </summary> 20 public string description {set; get ;} 21 /// <summary> Skill name </summary> 22 public string name {set; get ;} 23 // <summary> duration </summary> 24 public float durationTime {set; get;} 25 /// <summary> within the duration, interval between two injuries </summary> 26 public float damageInterval {set; get ;} 27 /// <summary> Injury ratio </summary> 28 public float damage {set; get ;} 29 // <summary> cool-down time </summary> 30 public int coolTime {set; get ;} 31 // <summary> cool-down remaining </summary> 32 public int coolRemain; 33 // <summary> magic consumption </summary> 34 public int costSP {set; get;} 35 // <summary> attack distance </summary> 36 public float attackDisntance {set; get ;} 37 // <summary> Target </summary> 38 [HideInInspector] 39 public GameObject [] attackTargets; 40 /// <summary> Target TAG </summary> 41 public string [] attckTargetTags {set; get ;} 42 // <summary> skill level </summary> 43 public int level {set; get ;} 44 // <summary> Skill premade object </summary> 45 public GameObject skillPrefab; 46 // <summary> premade file name </summary> 47 public string prefabName {set; get;} 48 // <summary> attack range: linear, rectangular, slice, circular </summary> 49 public DamageMode damageMode {set; get ;} 50 /// <summary> attack type, single attack, group attack </summary> 51 public SkillAttackType attackType {set; get ;} 52 // <summary> activate </summary> 53 public bool Activated; 54 // <summary> animation name corresponding to the skill </summary> 55 public string animtionName {set; get ;} 56 /// <summary> attack range angle </summary> 57 public int attackAngle {set; get ;} 58 // <summary> target special effect </summary> 59 public string hitFxName {set; get;} 60 public GameObject hitFxPrefab; 61 // <summary> next combo skill number </summary> 62 public int nextBatterId {set; get;} 63 64} 65}

The following figure shows how to obtain and put the data

1 /// <summary> 2 // put the database data into the skill management class 3 /// </summary> 4 /// <param name = "jobId"> occupation ID </param> 5 void InitSkill (int jobId) 6 {7 // open the database first 8 OperatingDB. instance. createDataBase (); 9 // traverse all rows in the table 10 SqliteDataReader skill = OperatingDB. instance. db. readFullTable ("T_Skill" + jobId); 11 while (skill. read () 12 {13 SkillData sd = new SkillData (); 14 // get 15 Type t = typeof (SkillData); 16 int I = 0; 17 // traverse all SkillData attributes t. getProperties18 foreach (var item in t. getProperties () 19 {20 I ++; 21 // obtain the property to determine whether it is string22 if (item. propertyType. equals (typeof (string) 23 item. setValue (sd, skill [I]. toString (), null); // value 24 // obtain the attribute to determine whether it is float25 else if (item. propertyType. equals (typeof (float) 26 item. setValue (sd, float. parse (skill [I]. toString (), null); 27 // obtain the attribute to determine whether it is 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 // get the other attributes, of which enumeration can be converted to int 34 else35 item. setValue (sd, int. parse (skill [I]. toString (), null); 36} 37 // get the skill management class of the object to pass in the Class 38 GetComponent <CharacterSkillManager> (). skills. add (sd); 39} 40 // close database 41 OperatingDB. instance. db. closeSqlConnection (); 42}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.