Unity 3 Dファイル読み書き
4819 ワード
ここでは主に簡単なファイルの読み書きで、お勧めしませんが、PlayerPrefsが一番いいです.
using UnityEngine; using System.Collections; using System.IO; public class MyFile : MonoBehaviour { public static bool IsActivated; public static int UserMoney; private static string filePath; void Start () { // ,filePath
string filePath = Application.persistentDataPath + Path.DirectorySeparatorChar + "save.txt"; DontDestroyOnLoad(this); //
LoadFile(); } void Update () { } public static void SaveFile() { //
if (File.Exists(filePath)) File.Delete(filePath); FileStream file = new FileStream(filePath, FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(file); sw.WriteLine(IsActivated); sw.WriteLine(UserMoney); sw.Close(); file.Close(); file.Dispose(); } public static void LoadFile() { if (File.Exists(filePath)) { // , using reader // : SaveFile, SaveFile
using (StreamReader reader = new StreamReader(filePath)) { string value; if ((value = reader.ReadLine()) == null) { Debug.Log("error : ISActivated"); return; } IsActivated = bool.Parse(value); if ((value = reader.ReadLine()) == null) { Debug.Log("error : UserMoney"); return; } UserMoney = int.Parse(value); } } else { // ,
IsActivated = false; UserMoney = 100; SaveFile(); } } }