We know that in unity the files can be called asset files, in the project development need to read the data to be stored, and some data can not be changed, today to write a demo to deal with this data, here will not write the method of reading excal data, If you need to know about Baidu a bit, about such posts and articles have a lot
First we need a class to "mimic" the excal data, and you can define the class:
usingUnityengine;usingSystem.Collections;/// <summary>///Data Classes/// </summary>[system.serializable] Public classitemdata{ //In other words, each row is a class Public stringID; Public stringname; Public intAddattack; Public intADDHP; Public intAdddefense;}
Because there are many lines in a excal, that is, there are many classes, so this class is not enough for us, we need to read out the data after the data put up, here I use a list. As follows:
using Unityengine; using System.Collections; using System.Collections.Generic; /// <SUMMARY> /// store asset data /// </SUMMARY> public class itemdatalist:scriptableobject{ public List<itemdata> ItemData; //represents a excal table }
The next is to generate the data, as below, it is necessary to note that this kind of thing editor class, need to put in editor, put the following script in unity will appear in unity after a demo-createasset in the menu bar, click to generate asset data , the data is placed in the resources, so the premise is to have a resources file, click on the generated file to see the data
usingUnityengine;usingSystem.Collections;usingSystem.Collections.Generic;usingUnityeditor;/// <summary>///package data into a asset file/// </summary> Public classcreateassetdata{[MenuItem ("Demo/createasset")] Static voidCreateasset () {itemdatalist list= itemdatalist.createinstance<itemdatalist>(); List.itemdata=datatest (); Assetdatabase.createasset (list,"Assets/createasset/resources/itemdata.asset"); } Public StaticList<itemdata>datatest ()//test data {List<ItemData> data =NewList<itemdata>(); for(inti =0; I < -; i++) {ItemData d=NewItemData (); D.id=" the"+i; D.name=" the"+i; D.ADDHP=i; D.adddefense= i*2; D.addattack= i*3; Data. ADD (d); } returndata; }}
The last is to use the data, because I was in the resources in the inside, so I use the resources class to load the data, such as,
usingUnityengine;usingSystem.Collections;usingSystem.Collections.Generic;/// <summary>///Loading Data/// </summary> Public classAssetload:monobehaviour {[Serializefield]PrivateList<itemdata>ItemData; voidAwake () {Startcoroutine (Loadassetdata ()); } IEnumerator Loadassetdata () {itemdatalist list= Resources.load<itemdatalist> ("ItemData"); ItemData=List.itemdata; yield return NULL; }}
Attached: You can also package this data into a Assetbundle file, and then use the WWW class to load, and then converted into a itemdatalist type to get the data. In addition, this data into a asset file must be fixed, can not be changed.
Excal data into asset data files