[유니티3D엔진] C# 어레이리스트 dat 파일 로드 및 저장 (바이너리 파일로 변환)
1. 먼저 클래스 직렬화로 만들기
using UnityEngine;
using System.Collections;
using System;
// ( 이것 때문에 하루 걸렸다 ㅠ 클래스 앞에 꼭 [Serializable] 붙이기 )
[Serializable]
public class Sm_mlevel_st_entry
{
private string stl1;
private string stl2;
private string stl3;
private string stl4;
private string stl5;
public Sm_mlevel_st_entry(string _stl1, string _stl2, string _stl3, string _stl4, string _stl5)
{
this.stl1 = _stl1; //ml_level
this.stl2 = _stl2; //ml_1
this.stl3 = _stl3; //ml_2
this.stl4 = _stl4; //ml_3
this.stl5 = _stl5; //ml_4
}
public string getStl1()
{
return stl1;
}
public string getStl2()
{
return stl2;
}
public string getStl3()
{
return stl3;
}
public string getStl4()
{
return stl4;
}
public string getStl5()
{
return stl5;
}
}
2. 저장 및 로드 함수
public object LoadData(string file_Name)
{
if (File.Exists(Application.dataPath + "/Sm_data_table/" + file_Name + ".dat"))
{
var b = new BinaryFormatter();
var f = File.Open(Application.dataPath + "/Sm_data_table/" + file_Name + ".dat", FileMode.Open);
object obj = b.Deserialize(f);
f.Close();
return obj;
}
else
{
return null;
}
}
public void SaveData( string file_Name, object tmp_list )
{
var b = new BinaryFormatter();
var f = File.Create(Application.dataPath + "/Sm_data_table/" + file_Name + ".dat");
b.Serialize(f, tmp_list);
f.Close();
}
3. 사용하기
List<Sm_mlevel_st_entry> tmpList = new List<Sm_mlevel_st_entry>();
for (int i = 0; i < 5; i++)
{
Sm_mlevel_st_entry entry1 = new Sm_mlevel_st_entry("1q " + i.ToString(), "2w " + i.ToString(), "3e " + i.ToString(), "4r " + i.ToString(), "5t " + i.ToString());
tmpList.Add(entry1);
}
Sm_data_table.instance.SaveData("sm_mlevel_st", tmpList); // 세이브
List<Sm_mlevel_st_entry> tmpList2 = new List<Sm_mlevel_st_entry>();
tmpList2 = (List<Sm_mlevel_st_entry>) Sm_data_table.instance.LoadData("sm_mlevel_st"); // 로드