unity 保存数据怎么把数据存在本地

Unity3D教程:保存或读取数据组的方法_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Unity3D教程:保存或读取数据组的方法
阅读已结束,下载文档到电脑
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩20页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢将自定义类存储在本地并从本地读取【unity3d吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:78,104贴子:
将自定义类存储在本地并从本地读取收藏
如何将对象的属性写入本地以及从本地读取?以下是代码。此脚本可直接用。此外还附加了对象克隆方法(此方法在IOS设备下貌似会有问题),以及其他一些function。声明:只可以存储自定义类并且要标记类为[Serializable]。using UnityEusing System.Cusing Susing System.IO;using System.Tusing System.Security.Cusing System.Runtime.Serialization.Formatters.Bpublic class ObjToByteTest : MonoBehaviour{
//[Serializable]
//public class TestClass
//private TestClass SaveT
//private TestClass ReadT
string mPath
Application.persistentDataPath + "//";
//void Update()
if (Input.GetMouseButtonDown(0))
SaveTest = new TestClass();
SaveTest.aaa = 111;
SaveTest.bbb = 222;
SaveTest.ccc = 333;
SaveTest.ddd = 44;
SaveTest.fff =
Debug.Log("SaveToLocal...");
SaveToLocal&TestClass&(SaveTest, mPath);
if (Input.GetMouseButtonDown(1))
ReadTest = ReadFromLocal&TestClass&(mPath);
Debug.Log(string.Format("{0},{1},{2},{3},{4}", ReadTest.aaa, ReadTest.bbb, ReadTest.ccc, ReadTest.ddd, ReadTest.fff));
/// &summary&
/// 将byte[]转换成string
/// &/summary&
/// &param name="characters"&&/param&
/// &returns&&/returns&
public static string UTF8ByteArrayToString(byte[] mbyte)
UTF8Encoding encoding = new UTF8Encoding();
string constructedString = encoding.GetString(mbyte);
return (constructedString);
/// &summary&
/// 将string转换成byte[]
/// &/summary&
/// &param name="pXmlString"&&/param&
/// &returns&&/returns&
public static byte[] StringToUTF8ByteArray(string pXmlString)
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteArray = encoding.GetBytes(pXmlString);
return byteA
/// &summary&
/// 对象克隆
/// &/summary&
/// &param name="obj"&&/param&
/// &returns&&/returns&
public static T ValueClone&T&(T obj)
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
ms.Seek(0, SeekOrigin.Begin);
return (T)bf.Deserialize(ms);
/// &summary&
/// 对象序列化为字节流
/// &/summary&
/// &typeparam name="T"&&/typeparam&
/// &param name="type"&&/param&
/// &returns&&/returns&
public static byte[] ToBytes&T&(T type)
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, type);
return ms.GetBuffer();
/// &summary&
/// 字节流反序列化为对象
/// &/summary&
/// &typeparam name="T"&&/typeparam&
/// &param name="bytes"&&/param&
/// &returns&&/returns&
public static T BytesTo&T&(byte[] bytes)
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
ms.Write(bytes, 0, bytes.Length);
return (T)bf.Deserialize(ms);
/// &summary&
/// 对象转换为数据流存在本地
/// &/summary&
/// &typeparam name="T"&&/typeparam&
/// &param name="type"&&/param&
/// &param name="path"&&/param&
public static void SaveToLocal&T&(T type, string path)
//.Rec为存储的文件后缀名可自己随便定义但读写的要一致!
FileStream fs = new FileStream(path + "Test.Rec", FileMode.Create);
fs = ToFileStream&T&(type, fs);
fs.Close();
fs.Dispose();
Debug.Log("SaveToLocal Success!" + path);
/// &summary&
/// 本地数据流转换为对象
/// &/summary&
/// &typeparam name="T"&&/typeparam&
/// &param name="path"&&/param&
/// &returns&&/returns&
public static T ReadFromLocal&T&(string path)
FileStream fs = new FileStream(path + "Test.Rec", FileMode.Open);
return FileStreamTo&T&(fs);
/// &summary&
/// 对象序列化为文件流
/// &/summary&
/// &typeparam name="T"&&/typeparam&
/// &param name="type"&&/param&
/// &param name="fs"&&/param&
/// &returns&&/returns&
private static FileStream ToFileStream&T&(T type, FileStream fs)
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, type);
/// &summary&
/// 文件流反序列化为对象
/// &/summary&
/// &typeparam name="T"&&/typeparam&
/// &param name="fs"&&/param&
/// &returns&&/returns&
private static T FileStreamTo&T&(FileStream fs)
BinaryFormatter bf = new BinaryFormatter();
T t = (T)bf.Deserialize(fs);
fs.Close();
fs.Dispose();
达内unity3d培训全程&实战教学&,unity3d金牌讲师授课.免费unity3d课程试听中!到达内unity3d学院学习unity3d,只需4个月速成unity3d游戏工程师.
在定义类中属性较多的情况下用这个方法比较方便,存储或者读取时不必为对象的每个属性在赋值
存储后文件的内容
这个怎么用啊? 要存的数据写在哪里? 存档的时候怎么让他运行?读取的时候怎么让他运行?
如果要存储的对象包含其他对象的引用或是子对象怎么半
小白你又奋斗了一夜?
看起来很高深,先顶一下在研究。
楼主,这个怎么用,表示看不懂。
正好要找方面的资料,顶下。
序列化这个在ios设备上确实有问题。我遇到过
登录百度帐号推荐应用20:59 提问
关于Unity3d WWW 加载本地资源的问题
WWW www = new WWW ("file://E:/项目/Assets/StreamingAssets/Actor.assetbundle");
WWW www = new WWW ("file:///E:/项目/Assets/StreamingAssets/Actor.assetbundle");
两种都试过都不行
You are trying to load data from a www stream which had the following error when downloading.
Couldn't open file /项目/Assets/StreamingAssets/Actor.assetbundle
UnityEngine.WWW:get_assetBundle()c__Iterator17:MoveNext() (at Assets/EffectScripts/ABC.cs:20)
求教大神 如何破解
按赞数排序
格式错了 最好中文路径去掉,然后改成WWW www = new WWW (@"file:///E:/项目/Assets/StreamingAssets/Actor.assetbundle")就行了
不要使用中文路径。。。。
路径格式不能识别。
其他相关推荐}

我要回帖

更多关于 unity保存数据到本地 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信