Skip to content

Instantly share code, notes, and snippets.

@buddypia
Created June 5, 2015 17:54
Show Gist options
  • Save buddypia/63d43babc436ec00c8af to your computer and use it in GitHub Desktop.
Save buddypia/63d43babc436ec00c8af to your computer and use it in GitHub Desktop.
【Unity/JsonFx】UnityでJSONの読み書き ref: http://qiita.com/shoridevel/items/6f9b3477af1a17341fdb
public class BlockDto : ObjectBaseDto
{
public BlockDto ()
{
}
public BlockDto (PositionDto _Position, float _SpawnTime, int _Type) : base (_Position, _SpawnTime)
{
Type = _Type;
}
public int Type { get; set; }
}
var levelData = (Resources.Load ("Json/LEVEL") as TextAsset).text;
Debug.Log ("levelData : " + levelData);
_levelDataList = JsonFx.Json.JsonReader.Deserialize<LevelDataList> (levelData);
// 上のレベル全体データからブロックデータのみ抽出
List<Transform> _blockList = levelDataList.BlockData;
// レベルデータ全体DTO
LevelDataList _levelDataList = new LevelDataList ();
// ブロックデータDTO
List<BlockDto> _blockDataList = new List<BlockDto> ();
// ループしながらデータを代入
for (int i=0; i<10; i++)
{
PositionDto p = new PositionDto (blockTran.position.x, blockTran.position.y);
float s = Random.Range (1.0f, 3.0f);
int t = Random.Range (1, 2);
BlockDto b = new BlockDto (p, s, t);
_blockDataList.Add (b);
}
_levelDataList.BlockData = _blockDataList;
// レベルをJSON化する
var levelDataListJson = JsonFx.Json.JsonWriter.Serialize (_levelDataList);
Debug.Log ("levelDataListJson : " + levelDataListJson);
// レベルデータ(JSON)を永久保存
File.WriteAllText (Application.dataPath + "/Resources/Json/levelDataTest.json", levelDataListJson);
using System.Collections.Generic;
public class LevelDataList
{
public List<BlockDto> BlockData { get; set; }
public List<PowerUpItemDto> PowerUpItemData { get; set; }
}
public class ObjectBaseDto
{
public ObjectBaseDto ()
{
}
public ObjectBaseDto (PositionDto _Position, float _SpawnTime)
{
Position = _Position;
SpawnTime = _SpawnTime;
}
public PositionDto Position { get; set; }
public float SpawnTime { get; set; }
}
public class PositionDto
{
public PositionDto ()
{
}
public PositionDto (float _X, float _Y)
{
X = _X;
Y = _Y;
}
public float X { get; set; }
public float Y { get; set; }
}
public class PowerUpItemDto : ObjectBaseDto
{
public PowerUpItemDto ()
{
}
public PowerUpItemDto (PositionDto _Position, float _SpawnTime, int _Type) : base (_Position, _SpawnTime)
{
Type = _Type;
}
public int Type { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment