纳金网

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

作者: yeu1233    时间: 2012-9-7 11:30
标题: 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 );
}

}




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