상세 컨텐츠

본문 제목

[Unity] 데이터 Save, Load

유니티

by simstealer 2022. 8. 9. 12:16

본문

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

[Serializable]
public class GameData
{
    public int BGM_Volume = 0;
    public int Effect_Volume = 0;

    public int gold = 0;
    public int hp = 10;
    public float moveSpeed = 5f;

    public List<MonsterData> monsterKillDatas;

    public GameData(int _gold, int _hp, float _moveSpeed)
    {
        gold = _gold;
        hp = _hp;
        moveSpeed = _moveSpeed;
        monsterKillDatas = new List<MonsterData>();
    }
}

[Serializable]
public class MonsterData
{
    public int index;
    public string name;
    public float moveSpeed;
    public float rotationSpeed;
    public string description;

    public MonsterData(int index, string name, float moveSpeed, float rotationSpeed, string description)
    {
        this.index = index;
        this.name = name;
        this.moveSpeed = moveSpeed;
        this.rotationSpeed = rotationSpeed;
        this.description = description;
    }
}

public class DataMgr : MonoBehaviour
{
    static GameObject container;
    static GameObject Container { get => container; }

    static DataMgr instance;
    public static DataMgr Instance
    {
        get
        {
            if (instance == null)
            {
                container = new GameObject();
                container.name = "DataMgr";
                instance = container.AddComponent(typeof(DataMgr)) as DataMgr;

                instance.SetMonsterDataFromCSV();

                DontDestroyOnLoad(container);
            }

            return instance;
        }
    }

    GameData gameDatas;
    public GameData GameData
    {
        get
        {
            if (gameDatas == null)
            {
                LoadGameData();
                SaveGameData();
            }

            return gameDatas;
        }
    }

    void InitGameData()
    {
        gameDatas = new GameData(100, 300, 5f);

        gameDatas.monsterKillDatas.Add(new MonsterData(1, "피그미", 2f, 1f, "작은 괴물"));
        gameDatas.monsterKillDatas.Add(new MonsterData(2, "바이킹", 2.2f, 1f, "용감한 괴물"));
    }

    public void SaveGameData()
    {
        InitGameData();
        string toJsonData = JsonUtility.ToJson(gameDatas, true);
        string filePath = Application.persistentDataPath + GameDataFileName;
        File.WriteAllText(filePath, toJsonData);
    }

    public void LoadGameData()
    {
        string filePath = Application.persistentDataPath + GameDataFileName;

        if (File.Exists(filePath))
        {
            string fromJsonData = File.ReadAllText(filePath);
            gameDatas = JsonUtility.FromJson<GameData>(fromJsonData);

            if (gameDatas == null)
            {
                InitGameData();
            }
        }
        else
        {
            InitGameData();
        }
    }

'유니티' 카테고리의 다른 글

[C#] Nullable  (0) 2022.08.11
[C#] 문자열 -> 숫자, 숫자 -> 문자열 변환  (0) 2022.08.09
[C#] 외부 데이터(CSV 파일)  (0) 2022.08.05
[C#] Event / Action  (0) 2022.07.21
[C#] 추상 클래스  (0) 2022.07.21

관련글 더보기

댓글 영역