파일을 저장할때.
내가 만든 클래스를 가볍게 리스트로 만들어
파일을 저장하는 방법.
C# 기준.
1. 파일 저장.
//내 저장할 리스트.
List<MyData> m_MyData= new List<MyData> ();
BinaryFormatter _binary_formatter = new BinaryFormatter();
//각 플랫폼별 폴더 지정하는건 http://blog.naver.com/nameisljk/110157303742 <-- 요 블로그에 잘 설명이 되있음
//폴더와 파일이름 을 넣음 .
FileStream _filestream = File.Create(Application.persistentDataPath + "/TestSave.dat");
//내가 저장할 리스트를 넣음.
_binary_formatter .Serialize(filestream, m_MyData);
filestream .Close();
2. 파일 불러오기
//내가 파일을 만든 파일 혹은 불러올 파일이 있는지 없는지를 체크함.
bool _fhile_check = File.Exists(pathForDocumentsFile(Application.persistentDataPath + "/TestSave.dat"));
BinaryFormatter _binary_formatter = new BinaryFormatter();
//파일을 불러옴. FileMode - Open 설정.
FileStream _filestream = File.Open( (pplication.persistentDataPath + "/TestSave.dat") FileMode.Open);
//불러온 파일을 변환.
//내가 저장할 리스트 변수에 형변환해서 넣어줌.
m_MyData= (List<MyData>)_binary_formatter .Deserialize(_lestream );
filestream .Close();
3.
여기에서 Serialize 를 사용하기 위해선 try ~ catch 문으로 감싸줘야 함.
Serialize 에 자신이 만든 클래스를 넣기 위해선 자신이 만든 클래스 위에 [System.Serializable] 를 선언해줘야 함.
ex)
[System.Serializable]
public class MyData
{
private int m_Index = 0;
}
4.
using UnityEngine;
using System.Collections;
//List
using System.Collections.Generic;
//try catch Exctption
using System;
//Filestream
using System.IO;
//BinaryFormatter 등등
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
'Program > Unity' 카테고리의 다른 글
UGUI - On Value Changed 입력 (Static , Dynamic) (0) | 2023.08.18 |
---|---|
Unity - Dictionary 를 Inspector 에 간단하게.. (1) | 2023.08.02 |
NGUI Scroll View - Spring 관련. (2) | 2016.02.23 |
일괄적으로 Texture Import Setting 변경 (0) | 2015.01.30 |
LitJson, SimpleJson, XML 비교 (0) | 2015.01.29 |