示例#1
0
        public void OldUsage2()
        {
            /* Its possible to stay closer to the original Unity syntax by using PlayerPrefsV2 where you typically
             * would write PlayerPrefs but this will couple the code to Unity. Typically Preferences.instance is
             * the recommended way to interact with persisted preferences. Below are some examples if you want to
             * stick to PlayerPrefsV2 anyways:
             */

            // PlayerPrefsV2.SetBool and PlayerPrefsV2.GetBool example:
            bool myBool = true;

            PlayerPrefsV2.SetBool("myBool", myBool);
            Assert.AreEqual(myBool, PlayerPrefsV2.GetBool("myBool", defaultValue: false));

            // PlayerPrefsV2.SetStringEncrypted and PlayerPrefsV2.GetStringDecrypted example:
            PlayerPrefsV2.SetStringEncrypted("mySecureString", "some text to encrypt", password: "******");
            var decryptedAgain = PlayerPrefsV2.GetStringDecrypted("mySecureString", null, password: "******");

            Assert.AreEqual("some text to encrypt", decryptedAgain);

            // PlayerPrefsV2.SetObject and PlayerPrefsV2.GetObject example (uses JSON internally):
            MyClass1 myObjectToSave = new MyClass1()
            {
                myString = "Im a string", myInt = 123
            };

            PlayerPrefsV2.SetObject("myObject1", myObjectToSave);
            MyClass1 objLoadedAgain = PlayerPrefsV2.GetObject <MyClass1>("myObject1", defaultValue: null);

            Assert.AreEqual(myObjectToSave.myInt, objLoadedAgain.myInt);
        }
示例#2
0
        public void ExampleUsage()
        {
            // PlayerPrefsV2.SetBool and PlayerPrefsV2.GetBool example:
            bool myBool = true;

            PlayerPrefsV2.SetBool("myBool", myBool);
            Assert.AreEqual(myBool, PlayerPrefsV2.GetBool("myBool", defaultValue: false));

            // PlayerPrefsV2.SetStringEncrypted and PlayerPrefsV2.GetStringDecrypted example:
            PlayerPrefsV2.SetStringEncrypted("mySecureString", "some text to encrypt", password: "******");
            var decryptedAgain = PlayerPrefsV2.GetStringDecrypted("mySecureString", null, password: "******");

            Assert.AreEqual("some text to encrypt", decryptedAgain);

            // PlayerPrefsV2.SetObject and PlayerPrefsV2.GetObject example (uses JSON internally):
            MyClass1 myObjectToSave = new MyClass1()
            {
                myString = "Im a string", myInt = 123
            };

            PlayerPrefsV2.SetObject("myObject1", myObjectToSave);
            MyClass1 objLoadedAgain = PlayerPrefsV2.GetObject <MyClass1>("myObject1", defaultValue: null);

            Assert.AreEqual(myObjectToSave.myInt, objLoadedAgain.myInt);
        }
示例#3
0
    private void TestJsonSerialization()
    {
        var prefsKey = "testObj1";
        var myObj    = new MyClass1()
        {
            theCurrentTime = "It is " + DateTime.Now, myInt = 123
        };

        PlayerPrefsV2.SetObject(prefsKey, myObj);
        AssertV2.AreEqual(myObj.theCurrentTime, PlayerPrefsV2.GetObject <MyClass1>(prefsKey, null).theCurrentTime);
        AssertV2.AreEqual(myObj.myInt, PlayerPrefsV2.GetObject <MyClass1>(prefsKey, null).myInt);
        links.Get <Text>("JsonOutput").text = JsonWriter.GetWriter().Write(PlayerPrefsV2.GetObject <MyClass1>(prefsKey, null));
    }
示例#4
0
        public void TestGetAndSetComplexObjects()
        {
            var key   = "b1";
            var myObj = new MyClass1()
            {
                myString = "Im a string", myInt = 123
            };

            Assert.AreEqual(null, PlayerPrefsV2.GetObject <MyClass1>(key, null));
            PlayerPrefsV2.SetObject(key, myObj);
            Assert.AreEqual(myObj.myString, PlayerPrefsV2.GetObject <MyClass1>(key, null).myString);
            Assert.AreEqual(myObj.myInt, PlayerPrefsV2.GetObject <MyClass1>(key, null).myInt);
            PlayerPrefsV2.DeleteKey(key);
        }