Recommended for learning unity scripts: index on the unity3d Official Website
The data storage in unity is basically the same as the dictionary storage in IOS. It stores and calls data through keywords.
The following describes the playerprefs class used by unity to store data.
Playerprefs allows you to maintain and access player preferences during game sessions.
Playerprefs on Mac OS X is stored in the-/library/playerprefs folder,
Unity/[companyName] \ [product name]. plist. Here, company and product are set in project setting, the same
Plist is used to run the project and standalone mode in the editor.
In Windows standalone mode, playerprefs is stored under the hkcu software [companyName] \ [product name] key in the registry. Here company and product are set in project setting.
In Web mode, playerprefs is stored in the binary file-/library/preferences/Unity/webplayerprefs of Mac OS X and % appdata % \ unity \ webplayerprefs of windows, A preference file corresponds to a web Player URL and the file size is limited to 1 MB. If this limit is exceeded, setint, setfloat, and setstring will not store the value and get along with a playerprefsexception.
Class Method
◆ Static function deleteall (): void
Description: removes all keys and values from the setting file and uses them with caution.
◆ Static function deletekey (key: string): void
Description: removes the key and its corresponding value from the setting file.
◆ Static function getfloat (key: String, defaultvalue: Float = of): Float
Description: If yes, return the value corresponding to the key in the settings file. If no value exists, it returns defaultvalue.
Print (playerprefs. getflat ("player score "));
◆ Static function getint (key: String, defaultvalue: INT): int
Description: return the value corresponding to the key in the setting file. If yes, defaultvalue is returned if no value exists.
Print (playerprefs. getint ("player score "));
◆ Static function getstring (key: String, defaultvalue: String = **): String
Description: return the value corresponding to the key in the setting file. If yes, defaultvalue is returned if no value exists.
Print (playerprefs. getstring ("player name "));
◆ Static function haskey (key: string): bool
Description: returns true if key exists in the setting file.
◆ Static function setfloat (key: String, value: Float): void
Description: sets the value determined by the key.
Print (playerprefs. setfloat ("player score", 10.0 ));
◆ Static function setint (key: String, value: INT): void
Description: sets the value determined by the key.
Playerprefs. setint ("player score", 10 );
◆ Static function setstring (key: String, value: string): void
Description: sets the value determined by the key.
Playerprefs. setstring ("player name", "foobar ");
The following is a simple example.
First, create two scenarios for scenario jump, named csdnprefabs1 and csdnprefabs2, to perform the experiment.
The basic requirement is to create some data in the first scenario and jump to the second scenario for display.
Create a scene1.cs script:
Using unityengine; using system. collections; public class scene0main: monobehaviour {// Public String teststr; Public String testint; Public String testfloat; // The GUI skin is the skin we added above. // drag it outside and assign the public guiskin fontskin value to it; // The displayed image public texture imagetexture; // use this for initializationvoid start () {// read the key value teststr = playerprefs. getstring ("teststr", "default"); testint = playerprefs. getint ("testint", 0 ). tostring (); testfloat = playerprefs. getfloat ("testfloat", 0 ). tostring () ;}// update is called once per framevoid Update () {}void ongui () {// set the GUI skin to the skin GUI we created. skin = fontskin; // smd gui. drawtexture (New rect (screen. width-imagetexture. width)> 1, 10,120,120), imagetexture); // Add an input box for users to enter information. I did not capture any exceptions, because the user may enter an invalid value teststr = GUI. textfield (New rect (10,200,200, 50), teststr, 50); testint = GUI. textfield (New rect (10,250,200, 50), testint, 50); testfloat = GUI. textfield (New rect (10,300,200, 50), testfloat, 50); // click the button to save all data if (GUI. button (New rect (220,200,150,100), "commit all") {playerprefs. setstring ("teststr", teststr); playerprefs. setint ("testint", Int. parse (testint); playerprefs. setfloat ("testfloat", float. parse (testfloat); // switch the scenario to scene1application. loadlevel ("scene1 ");}}}
Create a scene2.cs script:
Using unityengine; using system. collections; public class scene1main: monobehaviour {Public String teststr; Public String testint; Public String testfloat; Public guiskin fontskin; Public texture imagetexture; // use this for initialization void start () {teststr = playerprefs. getstring ("teststr", "default"); testint = playerprefs. getint ("testint", 0 ). tostring (); testfloat = playerprefs. getfloat ("testfloat", 0 ). tostring ();} void ongui () {GUI. skin = fontskin; GUI. drawtexture (New rect (screen. width-imagetexture. width)> 1, 10,120,120), imagetexture); // display label GUI. label (New rect (10,150,300, 50), "teststr =" + teststr); GUI. label (New rect (10,200,300, 50), "testint =" + testint); GUI. label (New rect (10,250,300, 50), "testfloat =" + testfloat); If (GUI. button (New rect (220,200,150,100), "clean all") {// delete all key values playerprefs. deleteall (); // return scenario 0 application. loadlevel ("scene0");} If (GUI. button (New rect (220,320,150,100), "only return") {// return scenario 0 application. loadlevel ("scene0 ");}}}
Then add the guiskin of the relevant texture to complete the deployment:
In this case, an error is reported because build is required for these two scenarios:
This is a simple demo.
After entering the information here, click commit all:
It can also be displayed in another scenario.