标签:des style class blog code tar
这里主要是简单的文件读写,不推荐使用,最好用的还是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();
}
}
}
标签:des style class blog code tar
原文地址:http://www.cnblogs.com/shihui142857/p/3796112.html