프로그램 실행 시 사용되는 메모리 영역(RAM) 은 휘발성, 프로그램을 종료하면 데이터들이 날아감
따라서 데이터들을 보존 하고 싶다면. 파일을 만들어 비-휘발성 영역에 저장하고, 이를 읽어올 수 있어야 함
public void Save()
{
string path1 = "C:\\\\Users\\\\JunHyoung\\\\Unity-RPG\\\\test.txt";
// 직접적으로 경로를 명시하는 방법
string Path2 = Application.dataPath;
// 유니티에서 지원하는 데이터 파일의 경로를 반환하는 메서드를 사용.
//- 저장 경로 : 프로젝트 폴더 내부(Asset)
string path3 = Path.Combine(Application.dataPath, "Test.txt");
// 문자열을 합칠 필요 없이, 두 경로를 합쳐주는 메서드 또한 있음.
string path => $"{Application.persistentDataPath}/Data";
// 특정 운영 체제에서 앱이 사용하도록 *허용한 경로를 반환함 (*Window의 경우 AppData 폴더)
//- 저장 경로 : C:\\Users\\[user name]\\AppData\\LocalLow\\[company name]\\[product name]
if (Directory.Exists(path) ==false)
{
Debug.Log("No Exisit Path");
Directory.CreateDirectory(path);
}
string filePath = Path.Combine(path, "Test.txt");
File.WriteAllText(filePath,"Test Text ABCDEF"); //작성은 C#의 System.IO.File 클래스
}
Unity - Scripting API: Application.persistentDataPath
직접적으로 경로를 명시하거나, Application.dataPath
를 사용하는 경우, 작업중인 환경내에서는 별 문제가 없어 보인다.
다만, 실제 빌드를 거쳐서 다른 기기, 다른 OS에서 사용하고자 할 때에는 달라지는 파일 경로 때문에 오류가 발생할것이니 Application.persistentDataPath
메서드를 사용해 플랫폼 별로 맞춰서 대응 할 수 있도록 하자.
Application.persistentDataPath
저장 경로
- On iOS: It maps to
<Application_Home>/Documents
. This is backed up by iTunes and iCloud.- On Android: It points to
/storage/emulated/0/Android/data/<packagename>/files
which is a private folder accessible only to the app. It is deleted when the user uninstalls the app.- On Windows: It typically points to
C:\\Users\\<username>\\AppData\\LocalLow\\<companyname>\\<productname>
.- On macOS: The path usually is
~/Library/Application Support/<companyname>/<productname>
.