Persistent storage of data in unity

Source: Internet
Author: User

Unity provides the Playerprefs class for storing game data on a computer's hard drive. This class has 10 functions that can be used

Class Functionsclass Functions
    • Setintsets the value of the preference identified by key.
      Sets the value of the parameter determined by key.
    • Getintreturns the value corresponding to key in the preference file if it exists.
      If present, returns the value corresponding to the key in the preference file.
    • Setfloatsets the value of the preference identified by key.
      Sets the value of the parameter determined by key.
    • Getfloatreturns the value corresponding to key in the preference file if it exists.
      If present, returns the value corresponding to key in the game archive file.
    • Setstringsets the value of the preference identified by key.
      Sets the value of the parameter determined by key.
    • Getstringreturns the value corresponding to key in the preference file if it exists.
      If present, returns the value corresponding to key in the game archive file.
    • Haskeyreturns true if key exists in the preferences.
      Returns true if key exists in the game archive.
    • Deletekeyremoves key and its corresponding value from the preferences.
      Removes the key and its corresponding value from the game archive.
    • Deleteallremoves all keys and values from the preferences. Use with caution.
      Remove all keys from your preferences. Please use it carefully.
    • Savewrites all modified preferences to disk.
      Write all modification parameters to the hard disk.

The data saved by the Playerprefs class function is saved to the hard disk as a collection of key-value pairs.

Where the first parameter of the Set class function is a string representing the name of the data to be set, the second parameter depends on the type of data to be saved. If you want to save a floating-point data with the name Playerscore, you should write it like this:

PlayerPrefs.SetFloat("Player Score", 10.5);

同样的如果想要博存的是int 类型的数据或者 string类型的数据,那么只需要把第二个参数指定为相应的类型就可以了。



关于get 类函数参数也有两个。第一个必须是 sting 类型的参数,表示你要取出的值对应的键名称,第二个参数则表示 如果没找到对应的键的情况下返回的默认值,下面是 一个例子:

print (PlayerPrefs.GetString("Player Name","没找到,我是默认返回值"));

像这样,如果买有找到 PlayerName 这个键 ,那么将会在控制台 打印
"没找到,我是默认返回值"

还用两个常用的函数;

//删除 PlayerPrefs 中某一个key的值
Playerprefs. deletekey ("key"); //Determine if this key exists in the Playerprefsbool b = playerprefs. Haskey("key");

欢迎志同道合的朋友一起学习, 一起进步。


Persistent storage of data in unity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.