Created
December 9, 2015 07:20
-
-
Save kyubuns/7d3efa48bcb2f8a08123 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class JsonTest : MonoBehaviour | |
{ | |
void Start() | |
{ | |
var json = Serialize(); | |
Desirialize(json); | |
} | |
string Serialize() | |
{ | |
var enemy1 = new Enemy() | |
{ | |
Name = "MonsterA", | |
Hp = 10, | |
Mp = 5, | |
}; | |
var enemy2 = new Enemy() | |
{ | |
Name = "MonsterB", | |
Hp = 9, | |
Mp = 6, | |
}; | |
var enemy3 = new Enemy() | |
{ | |
Name = "MonsterC", | |
Hp = 8, | |
Mp = 7, | |
}; | |
var stage = new Stage() | |
{ | |
StageName = "DummyStage", | |
Enemies = new List<Enemy>{ enemy1, enemy2, enemy3 } | |
}; | |
return JsonUtility.ToJson(stage); | |
} | |
void Desirialize(string json) | |
{ | |
var stage = JsonUtility.FromJson<Stage>(json); | |
Debug.LogFormat("StageName: {0}", stage.StageName); | |
foreach(var enemy in stage.Enemies) Debug.LogFormat("Name: {0} | {1}/{2}", enemy.Name, enemy.Hp, enemy.Mp); | |
} | |
} | |
[Serializable] | |
public class Stage | |
{ | |
public string StageName; | |
public List<Enemy> Enemies; | |
} | |
[Serializable] | |
public class Enemy | |
{ | |
public string Name; | |
public int Hp; | |
public int Mp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment