using System.IO;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
string fileName = "scripts.txt";
[SerializeField]
string fileInfo = "Hello World";
string label = "";
private void Start()
{
}
private void OnGUI()
{
if (GUILayout.Button("写入", GUILayout.MaxWidth(100), GUILayout.MaxHeight(30)))
{
WriteDatas(fileName, fileInfo);
}
GUILayout.Space(200);
if (GUILayout.Button("读取", GUILayout.MaxWidth(100), GUILayout.MaxHeight(30)))
{
label = ReadDatas(fileName);
}
GUILayout.Space(100);
GUILayout.Label(label);
}
void WriteDatas(string fileName, string info)
{
string filePath = Application.persistentDataPath + "/" + fileName;
if (File.Exists(filePath))
{
File.WriteAllText(filePath, info);
}
else
{
File.Create(filePath);
File.WriteAllText(filePath, info);
}
}
string ReadDatas(string fileName)
{
string filePath = Application.persistentDataPath + "/" + fileName;
if (File.Exists(filePath))
{
return File.ReadAllText(filePath);
}
return null;
}
}