Unity3D存储PlayerPrefs 用法
Posted icon169
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity3D存储PlayerPrefs 用法相关的知识,希望对你有一定的参考价值。
using UnityEngine; using System.Xml.Serialization; using System.IO; public class StateStorage{ public static T LoadData( 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( 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 ); } }
以上是关于Unity3D存储PlayerPrefs 用法的主要内容,如果未能解决你的问题,请参考以下文章
[Unity3D]手机3D游戏开发:场景切换与数据存储(PlayerPrefs 类的介绍与使用)