Unity/Unity 스파르타
13주차 - 맵 데이터화..
휘게31
2024. 3. 15. 18:43
728x90
맵 에디터 데이터 생성 및 불러오기
맵크기 영역 그리드 안에 오브젝트 추가 맵 데이터 세이브,
저장된 Json 파일로 맵 불러오기.
(에디터를 통한 오브젝트 추가는 아직 미구현)
모든 오브젝트에는 BuildObj 클래스를 상속 받고, 해당 클래스 안에는 TileData 라는 구조체가 있는데 안에 기본적인 현재 오브젝트의 타입과, 오브젝트의 리소스 경로, 생성위치가 들어있습니다.
[System.Serializable]
public class BuildObj : MonoBehaviour
{
private TileType tileType;
private Vector2 position;
public string path;
public TileData tileData;
public void SetTileData(TileType tileType,Vector2 position)
{
this.tileType = tileType;
this.position = position;
tileData = new TileData(tileType, position, path);
}
}
에디터 모드일때 ,오브젝트를 생성하면 BuildObj에 TileData가 생성되고
TileData의 타입에 따라 특정 Transform 자식으로 오브젝트가 생성되는데
데이터를 세이브 할때 특정 Transform에 TileData를 리스트로 모아 데이터 화 시켰고
void CreateJsonFile()
{
mapTileDataList = GetList(floorTransform);
mapObjectDataList = GetList(objectTransform);
Map map = new Map(new Vector2(width, height), mapID, playerSpawnPosition, new ExitObjStruct(playerExitPosition, condition_KeyAmount), mapTileDataList,mapObjectDataList, cellSize);
string json = JsonUtility.ToJson(map, true);
string filePath = Path.Combine(folderPath, $"{map.mapID}.json");
File.WriteAllText(filePath, json);
UnityEditor.AssetDatabase.Refresh();
}
Map 클래스를 새로 만들어, 만들어진 Map 클래스를 Json으로 파싱해 저장했습니다.
맵 크기를 정하면 자동으로 아웃라인과 그리드 생성.
그리드 영역 안에 오브젝트 생성 후 데이터 세이브
저장된 데이터 불러오기
주말 동안 맵 오브젝트 데이터 테이블 만들어서 TileData 부분 대체하기
728x90