I Defined an array of 67 elements in a script (C#). Each element is an object of a class that I defined that includes some variables. Like Below:
// the Definition of the array
public Pack[] packsInfo;
// the class
[Serializable]
public class Pack
{
public int number;
public int angle;
public float zPosition;
public float beatCaseDistance;
public float gunDistance;
}
Then, I Assign all the values in Inspector. like Below:
So ... if i suddenly change any of the values or even worse, ( the values gone for a any reason. like changing the class parameters or any thing ) then I must spend hours on setting up all the values. So i want to print all the values in a text, or a file and save them. for recovering them later. i want a backup solution.I want to do it whit Programming. ( I don't want to write them manually ;) .It is waste of time. )
To save yor array you can use for example JsonUtillity.
It is simple! Good Luck!
void Start () {
/////////////////////// save to json
JSONObject jsonSave = new JSONObject();
for(int i=0; i < packsInfo.Length; i++) {
JSONObject packJson = new JSONObject();
packJson.AddField("number", packsInfo[i].number);
packJson.AddField("angle", packsInfo[i].angle);
packJson.AddField("z_position", packsInfo[i].zPosition);
packJson.AddField("beat_case_distance", packsInfo[i].beatCaseDistance);
packJson.AddField("gun_distance", packsInfo[i].gunDistance);
jsonSave.Add(packJson);
}
System.IO.StreamWriter streamWritter = new System.IO.StreamWriter(@"C:\MyGameFiles\packs.json");
streamWritter.WriteLine(jsonSave.Print(true));
streamWritter.Close();
streamWritter = null;
/////////////////////// read json
System.IO.StreamReader reader = new System.IO.StreamReader(@"C:\MyGameFiles\packs.json");
string jsonString = reader.ReadToEnd();
reader.Close();
reader = null;
JSONObject jsonRead = new JSONObject(jsonString);
packsInfo = new Pack[jsonRead.Count];
for(int i =0; i < jsonRead.Count; i ++) {
Pack pack = new Pack();
pack.number = (int)jsonRead[i]["number"].i;
pack.angle = (int)jsonRead[i]["angle"].i;
pack.zPosition = jsonRead[i]["z_position"].f;
pack.beatCaseDistance =jsonRead[i]["beat_case_distance"].f;
pack.gunDistance = jsonRead[i]["gun_distance"].f;
packsInfo[i] = pack;
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加