본문 바로가기
개발/C#

유니티 C# 파일 저장 불러오기 간단 구현 File Save Load

by SPNK 2023. 6. 7.
반응형
  • 코드 작성
using UnityEngine;
using System.IO;

public class FileSaveLoad : MonoBehaviour
{
    private string savePath;

    private void Start()
    {
        savePath = Application.persistentDataPath + "/save.txt";
    }

    private void SaveData(string data) //저장하기
    {
        StreamWriter writer = new StreamWriter(savePath);
        writer.Write(data);
        writer.Close();
        Debug.Log("Data saved to: " + savePath);
    }

    private string LoadData() //불러오기
    {
        string data = "";

        if (File.Exists(savePath))
        {
            StreamReader reader = new StreamReader(savePath);
            data = reader.ReadToEnd();
            reader.Close();
            Debug.Log("Data loaded from: " + savePath);
        }
        else
        {
            Debug.Log("Save file not found!");
        }

        return data;
    }

    private void ExampleUsage()
    {
        string dataToSave = "Hello, World!";
        SaveData(dataToSave);
        string loadedData = LoadData();
        Debug.Log("Loaded Data: " + loadedData);
    }
}
반응형

댓글