やまめも

【 ゲーム開発・日々のことについて 】

json使用例

//宣言が必要
using LitJson;

public class HostageInfo : MonoBehaviour {
	public List<HostageCollection> MyCollectionHS;
	public List<Sprite> SpriteSeriesHS;
	// public List<String> HSName;

	public static HostageInfo instance = null;
	void Awake(){
		if(instance == null){
			instance = this;
		}
		InitHostageData();
	}
	private void InitHostageData(){
		MyCollectionHS = new List<HostageCollection> ();
		TextAsset jsonText = Resources.Load<TextAsset>("Database/HostagesData");
		MyCollectionHS = LoadAbilityTableFromJSON (jsonText.text);
	}
	private List<HostageCollection> LoadAbilityTableFromJSON(string json){
		JsonData jresult = JsonMapper.ToObject (json);
		List<HostageCollection> data = new List<HostageCollection> ();

		for(int i=0;i<jresult.Count;i++){
			data.Add(new HostageCollection(){
				_Id = (int)jresult[i]["id"],
				_Name = (string)jresult[i]["name"],
				_Explain = (string)jresult[i]["explain"],
				_HostageSprite = Resources.Load<Sprite>("SpriteHS/"+(string)jresult[i]["sprite"]) as Sprite
			});
		}
		return data;
	}
}