纳金网

标题: Unity3d 状态存储 PlayerPrefs高级用法 [打印本页]

作者: 狂风大尉    时间: 2014-9-30 21:25
标题: Unity3d 状态存储 PlayerPrefs高级用法

using UnityEngine;
using System.Xml.Serialization;
using System.IO;

public class StateStorage{

public static T LoadData<T>( string key ){
if ( PlayerPrefs.HasKey( key ) ) {
XmlSerializer serializer = new XmlSerializer( typeof( T ) );
StringReader sr = new StringReader( PlayerPrefs.GetString( key ) );
return ( T )serializer.Deserialize( sr );
}else{
return default(T);
}
}

public static void SaveData<T>( string key, T source ){
XmlSerializer serializer = new XmlSerializer( typeof( T ) );
StringWriter sw = new StringWriter();
serializer.Serialize( sw, source );
PlayerPrefs.SetString( key, sw.ToString() );
}

public static void ClearData( string key ){
PlayerPrefs.DeleteKey( key );
}

}
作者: oelongeo    时间: 2014-10-1 09:10
谢谢狂风大尉指导...顶一顶
作者: HIDEOKOJIMA    时间: 2014-10-1 09:15
Thanks for sharing this !
作者: humazi    时间: 2014-10-1 14:10
就是序列化和反序列化保存数据类嘛。不过存在playerPrefs中。话说pp有没有键值的长度限制。




欢迎光临 纳金网 (http://c-www.narkii.com/club/) Powered by Discuz! X2.5