本文主要是介绍【Unity】数据持久化及方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 一、Unity常见路径以及文件夹
- 1.1 常见路径
- Android平台
- IOS平台
- 1.2 文件夹
- Resources
- StreamingAssets
- AssetBundle
- PersistentDataPath
- 二、PlayerPrefs
- 三、二进制序列化
- 三、JSON
- 3.1 JSON介绍
- 四、XML
一、Unity常见路径以及文件夹
1.1 常见路径
Unity中包含很多特殊文件夹以及路径。
属性名 | 作用 |
---|---|
Application.dataPath | 此属性用于返回程序的数据文件所在文件夹的路径,例如Editor中的Assets。 |
Application.streamingAssetsPath | 此属性用于返回流数据的缓存目录,返回路径为相对路径,适合设置一些外部数据文件的路径。该路径下的文件是只读的。 |
Application.persistentDataPath | 此属性用于返回一个持久化数据存储目录的路径,可以在此路径下存储一些持久化的数据文件。 |
Application.temporaryCachePath | 此属性用于返回一个临时数据的缓存目录。 |
Android平台
属性名 | 路径 |
---|---|
Application.dataPath | /data/app/xxx.xxx.xxx.apk |
Application.streamingAssetsPath | jar:file:///data/app/xxx.xxx.xxx.apk/!/assets |
Application.persistentDataPath | /data/data/xxx.xxx.xxx/files |
Application.temporaryCachePath | /data/data/xxx.xxx.xxx/cache |
IOS平台
属性名 | 路径 |
---|---|
Application.dataPath | Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data |
Application.streamingAssetsPath | Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw |
Application.persistentDataPath | Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents |
Application.temporaryCachePath | /data/data/xxx.xxx.xxx/Library/Cache |
注:Application.dataPath该路径在移动端没有访问权限!
1.2 文件夹
Resources
是一个Unity3D的保留文件夹,里面的内容在打包时会被无条件的打到发布包中
它的特点简单总结就是:
- 只读,即不能动态修改,所以动态更新的资源不要放在其中
- 会将文件夹内的资源打包集成到.asset文件里面,因此可以放入一些Prefab,因为Prefab打包时会自动过滤不需要的资源
- 主线程加载,资源读取使用Resources.Load()
StreamingAssets
和Resources类似,同样是一个只读的Unity3D的保留文件夹
不同的是Resources文件夹中的内容在打包时会被压缩和加密
而StreamingAssets文件夹中的内容则会原封不动的打入包中,因此主要用来存放一些二进制文件
- 同样只读不可写。
- 主要用来存放二进制文件。
- 只能用WWW类来下载,在Unity2018以后使用UnityWebRequest类。
实际代码:
- WWW方法(高版本已弃用)
IEnumerator LoadStreamingAssetsByWWW()
{string filePath = Application.streamingAssetsPath + "/test.txt";WWW www = new WWW(filePath);yield return www;_result = www.text;
}
- UnityWebRequest方法
IEnumerator LoadStreamingAssetsByUnityWebRequest()
{string filePath = Application.streamingAssetsPath + "/test.txt";UnityWebRequest webRequest = UnityWebRequest.Get(filePath);yield return webRequest.SendWebRequest();//异常处理if (webRequest.isHttpError || webRequest.isNetworkError){Debugger.Log(webRequest.error);}// webRequest 就是最后获取到的文件数据// webRequest.downloadHandler.data 获取二进制数据
}
注:这段代码编写完成之后,在Mac或者编辑器里测试,无法输出内容。Editor和IOS在读取本地文件的时候,地址需要加一个file://。
string filePath = "file://" + Application.streamingAssetsPath + "/test.txt"; if (Application.isMobilePlatform) {if (Application.platform == RuntimePlatform.Android){filePath = Application.streamingAssetsPath + "/test.txt";} }
AssetBundle
就是俗称的AB包
- 是Unity3D定义的一种二进制类型
- 最好将prefab封装成AssetBundle
- 使用WWW类来下载
PersistentDataPath
这个路径是可读写的
在IOS上就是程序的沙盒,但是在Android上可以是程序的沙盒,也可以是sdcard
并且在Android打包的时候,ProjectSetting页面有一个选项Write Access,可以设置它的路径是沙盒还是sdcard
- 内容可读写,不过只能运行时才能写入或者读取。不能提前将数据存入
- 无内容限制,可以从StreamingAsset中读取二进制文件或者从AssetBundle读取文件来写入PersistentDataPath中
- 写入的文件,可以在电脑上进行读写
从StreamingAsset中读取二进制文件写入的例子
string savePath = Application.persistentDataPath + "/test.txt";
using (FileStream fs = File.Create(savePath))
{fs.Write(webRequest.downloadHandler.data, 0, webRequest.downloadHandler.data.Length);
}
二、PlayerPrefs
PlayerPrefs是Unity自带的数据结构,位于UnityEngine命名空间下
它可以对整数,浮点数,字符串3种类型的数据进行存取
它是持久存储于设备上的,只要用户没有删除应用或者手动去清除数据,PlayerPrefs的数据会一直保留
适合存储一些设置相关的信息
PlayerPrefs采用键值对的方式进行数据的查询与存储,存储数据的方式如下:
PlayerPrefs.SetInt ("ID",1);
PlayerPrefs.SetFloat ("WEIGHT",20.5f);
PlayerPrefs.SetString ("NAME","SNAGE");
读取数据的方式如下:
int id = PlayerPrefs.GetInt ("ID");
float weight = PlayerPrefs.GetFloat ("WEIGHT");
string name = PlayerPrefs.GetString ("NAME");
删除数据的方式如下:
PlayerPrefs.DeleteKey ("NAME");
PlayerPrefs.DeleteAll ();
三、二进制序列化
这里举例使用 BinaryFormatter 进行二进制序列化,但比较建议使用ProtoBuf
假设有一个Player类
[System. Serializable]
public class Player
{public int health;public int power;public Vector3 position;
}
由于BinaryFormatter序列化不支持Unity的Vector3类型,所以需要包装一下,创建好PlayerData类准备序列化
public class PlayerData{public int level;public int health;public float[] position;public PlayerData(Player player){this.level = player.level;this.health = player.health;this.position = new float[3];this.position[0] = player.transform.position.x;this.position[1] = player.transform.position.y;this.position[2] = player.transform.position.z;}
}
对PlayerData进行保存和读取,读取出来的PlayerData可以赋值给Player类
public static class SaveSystem{//保存数据public static void SavePlayer(Player player){BinaryFormatter formatter = new BinaryFormatter();string path = Application.persistentDataPath + "/player.fun";FileStream stream = new FileStream(path,FileMode.Create);PlayerData data = new PlayerData(player);formatter.Serialize(stream,data);stream.Close();}//读取数据public static PlayerData LoadPlayer(){string path = Application.persistentDataPath+"/player.fun";if(File.Exists(path)){BinaryFormatter formatter = new BinaryFormatter();FileStream stream = new FileStream(path,FileMode.Open);PlayerData data = formatter.Deserialize(stream) as PlayerData;stream.Close();return data;}else{Debug.LogError("Save file not found in "+path);return null;}}
}
三、JSON
3.1 JSON介绍
JSON是一种轻量级的数据交换和存储格式
可以用于对数据的设备(如手机的本地存储)和向Web服务器上传,并且符合面向对象编程的思想
JSON采用完全独立于语言的文本格式,但也使用了类似于C语言家族的习惯(包括C,C++,C#,JAVA等)
这些特性使JSON成为理想的数据交换语言,易于人阅读和编写,同时也易于机器解析和生成
可以使用NewtonJson来进行序列化。
假设有一个玩家存档类
public class PlayerSaveData
{public string versionId { get; set; } // 版本号public int gold { get; set; } // 金币数量public int energy { get; set; } // 体力public int mainWeapon { get; set; } // 主武器public int towerUp { get; set; } // 上炮台public int towerDown { get; set; } // 下炮台public Dictionary<int, int> chapterProgress; // 字典测试
}
通过使用NewtonJson序列化并输出,并且NewtonJson还可以将字典进行序列化
// NewtonJson测试
// 创建字典Dictionary<int, int> dic = new Dictionary<int, int>
{{ 1000, 1 },{ 1001, 2 },
};
PlayerSaveData data = new PlayerSaveData()
{versionId = "0.01",gold = 1000,energy = 50,mainWeapon = 20000,towerUp = 40000,towerDown = 40000,chapterProgress = dic, // 将字典赋值
};
string json = JsonConvert.SerializeObject(data);
最后输出结果,其中{"chapterProgress":{"1000":1,"1001":2}
是从字典转化而来:
反序列化
PlayerSaveData data = new PlayerSaveData();
data = JsonConvert.DeserializeObject<PlayerSaveData>(json);
四、XML
这篇关于【Unity】数据持久化及方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!