void Start() { BinaryFilePreferencesFactory factory = new BinaryFilePreferencesFactory(); /*Custom a ISerializationSurrogate for the type of DateTime. */ SurrogateSelector selector = factory.Formatter.SurrogateSelector as SurrogateSelector; selector.AddSurrogate(typeof(DateTime), new StreamingContext(StreamingContextStates.All), new DateTimeSerializationSurrogate()); Preferences.Register(factory); // Preferences.Register (new PlayerPrefsPreferencesFactory ()); /* This is a global preferences. */ Preferences prefs = Preferences.GetGlobalPreferences(); prefs.SetString("username", "*****@*****.**"); prefs.SetString("name", "clark"); prefs.SetInt("zone", 5); prefs.Save(); /* This is a preferences that it's only clark's data in the fifth zone. */ Preferences userPrefs = Preferences.GetPreferences("clark@5"); /* username:clark, zone:5 */ userPrefs.SetObject <Vector3> ("role.logout.map.position", new Vector3(1f, 2f, 3f)); userPrefs.SetObject <Vector3> ("role.logout.map.forward", new Vector3(0f, 0f, 1f)); userPrefs.SetObject <DateTime> ("role.logout.time", DateTime.Now); userPrefs.Save(); //----------------- Debug.LogFormat("username:{0}; name:{1}; zone:{2};", prefs.GetString("username"), prefs.GetString("name"), prefs.GetInt("zone")); Debug.LogFormat("position:{0} forward:{1} logout time:{2}", userPrefs.GetObject <Vector3> ("role.logout.map.position"), userPrefs.GetObject <Vector3> ("role.logout.map.forward"), userPrefs.GetObject <DateTime> ("role.logout.time")); }
// Start is called before the first frame update void Start() { BinaryFilePreferencesFactory factory = new BinaryFilePreferencesFactory(); Preferences.Register(factory); Preferences preferences = Preferences.GetGlobalPreferences(); bool b = preferences.ContainsKey("ChinesePeople"); Debug.Log("名为ChinesePeople的Preferences数据是否存在:" + b); preferences.SetString("ChinesePeople", "lilei"); PlayerModel playerModel = new PlayerModel(this.transform, 10); preferences.SetInt("score", playerModel.Score); preferences.SetObject("position", playerModel.Position); preferences.SetObject("rotation", playerModel.Rotation); preferences.Save(); Debug.Log("存储Preferences数据的地址Application.persistentDataPath为:" + Application.persistentDataPath); Debug.Log("Score为:" + preferences.GetInt("score")); Debug.Log("Position为:" + preferences.GetObject <Vector3>("position")); Debug.Log("Rotation为:" + preferences.GetObject <Vector3>("rotation")); }
public void 测试存储GlobalPreferences() { Preferences.Factory = new BinaryFilePreferencesFactory(); //lobalPreferences var globalPreferences = Preferences.GetGlobalPreferences(); globalPreferences.SetInt("Level", 1); globalPreferences.SetObject("DATA_VERSION", new Version(1, 1)); globalPreferences.Save(); Preferences.Factory = new PlayerPrefsPreferencesFactory(); globalPreferences = Preferences.GetGlobalPreferences(); Assert.AreEqual(globalPreferences.GetInt("Level"), 1); //创建自己的preference var playerPreferences = Preferences.GetPreferences("soso"); playerPreferences.SetInt("Level", 12); playerPreferences.Save(); Assert.AreEqual(playerPreferences.GetInt("Level"), 12); //自定义TypeEncode var binFactory = new BinaryFilePreferencesFactory(); binFactory.Serializer.AddTypeEncoder(new SelfTypeEncoder()); Preferences.Factory = binFactory; }
public PlayerService() { BinaryFilePreferencesFactory factory = new BinaryFilePreferencesFactory(); Preferences.Register(factory); preferences = Preferences.GetGlobalPreferences(); }
void Start() { //file持久化 BinaryFilePreferencesFactory factory = new BinaryFilePreferencesFactory(); /* Custom a ITypeEncoder for the type of CustomData. */ //添加自定义的类型编码器 factory.Serializer.AddTypeEncoder(new CustomDataTypeEncoder()); //注册一个Preference的工厂,默认是PlayerPrefsPreferencesFactory工厂,只有使用File持久化才需要改为BinaryFilePreferencesFactory工厂 Preferences.Register(factory); //Preferences.Register (new PlayerPrefsPreferencesFactory ()); /* This is a global preferences. */ //获得全局配置,如果不存在则自动创建 Preferences prefs = Preferences.GetGlobalPreferences(); prefs.SetString("username", "*****@*****.**"); prefs.SetString("name", "clark"); prefs.SetInt("zone", 5); prefs.Save(); /* This is a preferences that it's only clark's data in the fifth zone. */ //根据key值"clark@zone5"获得配置,如果不存在则自动创建,这里的意思是获得游戏第5区名为clark的用户的配置信息 //在Preferences.GetPreferences()函数中,name只是一个存取的Key,你可以完全按自己的意思组合使用。 Preferences userPrefs = Preferences.GetPreferences("clark@5"); /* username:clark, zone:5 */ userPrefs.SetString("role.name", "clark"); userPrefs.SetObject("role.logout.map.position", new Vector3(1f, 2f, 3f)); userPrefs.SetObject("role.logout.map.forward", new Vector3(0f, 0f, 1f)); userPrefs.SetObject("role.logout.time", DateTime.Now); userPrefs.SetObject("test.custom.data", new CustomData("test", "This is a test.")); userPrefs.Save(); //----------------- Debug.LogFormat("username:{0}; name:{1}; zone:{2};", prefs.GetString("username"), prefs.GetString("name"), prefs.GetInt("zone")); Debug.LogFormat("position:{0} forward:{1} logout time:{2}", userPrefs.GetObject <Vector3>("role.logout.map.position"), userPrefs.GetObject <Vector3>("role.logout.map.forward"), userPrefs.GetObject <DateTime>("role.logout.time")); Debug.LogFormat("CustomData name:{0} description:{1}", userPrefs.GetObject <CustomData>("test.custom.data").name, userPrefs.GetObject <CustomData>("test.custom.data").description); }
void Start() { BinaryFilePreferencesFactory factory = new BinaryFilePreferencesFactory(); /* Custom a ITypeEncoder for the type of CustomData. */ factory.Serializer.AddTypeEncoder(new CustomDataTypeEncoder()); Preferences.Register(factory); //Preferences.Register (new PlayerPrefsPreferencesFactory ()); /* This is a global preferences. */ Preferences prefs = Preferences.GetGlobalPreferences(); prefs.SetString("username", "*****@*****.**"); prefs.SetString("name", "clark"); prefs.SetInt("zone", 5); prefs.Save(); /* This is a preferences that it's only clark's data in the fifth zone. */ Preferences userPrefs = Preferences.GetPreferences("clark@5"); /* username:clark, zone:5 */ userPrefs.SetString("role.name", "clark"); userPrefs.SetObject("role.logout.map.position", new Vector3(1f, 2f, 3f)); userPrefs.SetObject("role.logout.map.forward", new Vector3(0f, 0f, 1f)); userPrefs.SetObject("role.logout.time", DateTime.Now); userPrefs.SetObject("test.custom.data", new CustomData("test", "This is a test.")); userPrefs.Save(); //----------------- Debug.LogFormat("username:{0}; name:{1}; zone:{2};", prefs.GetString("username"), prefs.GetString("name"), prefs.GetInt("zone")); Debug.LogFormat("position:{0} forward:{1} logout time:{2}", userPrefs.GetObject <Vector3>("role.logout.map.position"), userPrefs.GetObject <Vector3>("role.logout.map.forward"), userPrefs.GetObject <DateTime>("role.logout.time")); Debug.LogFormat("CustomData name:{0} description:{1}", userPrefs.GetObject <CustomData>("test.custom.data").name, userPrefs.GetObject <CustomData>("test.custom.data").description); }