代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
//序列化,方便后面存储到json文件中
[System.Serializable]
public class PlayerAttribute
{
public float speed;
public float jumpforce;
public int maxJumpNum;
public float maxHealth;
public float currentHealth;
}
public class PlayerAttriSave : MonoBehaviour
{
public PlayerController player;
PlayerAttribute pa = new PlayerAttribute();
private void Start()
{
string filepath = Application.streamingAssetsPath + "/PlayerAttribute.json";
if (File.Exists(filepath))
{
LoadData();
}
else
{
Initialize();
}
}
private void GenerateData()
{
pa.speed = player.speed;
pa.jumpforce = player.jumpforce;
pa.maxJumpNum = player.maxJumpNum;
pa.currentHealth = player.currentHealth;
pa.maxHealth = player.maxHealth;
}
public void SaveData()
{
string json = JsonUtility.ToJson(pa, true);
string filepath = Application.streamingAssetsPath + "/PlayerAttribute.json";
using (StreamWriter sw = new StreamWriter(filepath))
{
sw.WriteLine(json);
sw.Close();
sw.Dispose();
}
}
public void OnClickQuit()
{
GenerateData();
SaveData();
UnityEditor.EditorApplication.isPlaying = false;
Application.Quit();
}
void LoadData()
{
string json;
string filepath = Application.streamingAssetsPath + "/PlayerAttribute.json";
using (StreamReader sr = new StreamReader(filepath))
{
json = sr.ReadToEnd();
sr.Close();
}
pa = JsonUtility.FromJson<PlayerAttribute>(json);
Change();
}
void Change()
{
player.speed = pa.speed;
player.jumpforce = pa.jumpforce;
player.maxJumpNum = pa.maxJumpNum;
player.currentHealth = pa.currentHealth;
player.maxHealth = pa.maxHealth;
player.SetHealth();
player.SetMaxHealth();
}
void Initialize()
{
player.speed = 5f;
player.jumpforce = 10f;
player.maxJumpNum = 2;
player.maxHealth = 10f;
player.currentHealth = 5f;
}
//如何调试
}
流程图:
知识点:
1.
string filepath = Application.streamingAssetsPath + "/PlayerAttribute.json";
Application.streamingAssetsPath为StreamingAssets 文件夹的路径(只读)。
使用 StreamingAssets 文件夹来存储资源。在运行时,Application.streamingAssetsPath 提供文件夹路径。将资源名称添加到 Application.streamingAssetsPath。构建的应用程序即可加载此地址的资源。
2.
using (StreamWriter sw = new StreamWriter(filepath))
{
sw.WriteLine(json);
sw.Close();
sw.Dispose();
}
using(){}其实就相当于{},不同的是using(){}可以用于定义一个范围,在此范围的末尾自动将对象释放。小括号内声明要使用的对象,那个对象必须实现IDisposable接口或者其基类实现了IDisposable接口。
使用方法:using(){}常常用于避免如一些文件流,文件句柄,数据库接口在使用完了后顽疾了释放而处于一直被占用的状态。
使用场景:当在某个代码段中使用了类的实例,而希望无论因为什么原因,只要离开了此代码段就自动调用这个类实例的Dispose。
结尾不用显式调用,官方文档也没调用(StreamReader 类)
3.
sw.WriteLine(json);
写入Json
json = sr.ReadToEnd();
读取来自流的当前位置到结尾的所有字符。返回String
StreamReader sr = new StreamReader(filepath)
filepath:要读取的完整文件路径。
4.
pa = JsonUtility.FromJson<PlayerAttribute>(json);
Json转对象
< PlayerAttribute >对象的类型
(json)Json文件的字符串形式
string json = JsonUtility.ToJson(pa, true);
对象转为Json的字符串形式,true表示实现可读性
5.
File.Exists(filepath)
检测文件是否存在
filepath为文件完整路径
6.
UnityEditor.EditorApplication.isPlaying = false;
Application.Quit();
第一个函数:编辑器当前是否处于播放模式?,用于未发布阶段
第二个函数:退出播放器应用程序,用于发布阶段
参考资料:
unity Application 本地目录资源路径详解 Application.StreamingAssetsPath、Application.PersistentDataPath等
Application.streamingAssetsPath