Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
First, requirement description
Single-player game (user data stored locally) : Previously developed with Cocos, now developed with Unity. So there needs to be a requirement for data (user assets) inheritance.
Two, try to implement
Read Unity’s PlayerPrefs class in the local store file (since Cocos is not familiar with it, I think read Unity’s store data is similar to Cocos’s data)
Implementation steps:
- Use it on your phone
PlayerPrefs.Set..
Storing data; (Take the “Gold” field as an example) - Write the following code in Xcode:
How does Cocos creator read Unity native storage (PlayerPrefs) data files
// Get the path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory.NSUserDomainMask.YES);
/ / return, example: "/ var/mobile/Containers/data/Application/B9B904d - perhaps a EA23-4 - A3TD - KLH54G54 / Library"
NSString *libraryDirectoryPath = [paths objectAtIndex:0];
NSLog(@" Read library path %@", libraryDirectoryPath);
// Preferences/ [bundle identifier].plist"
NSString *plistPath = [libraryDirectoryPath stringByAppendingString:@"/Preferences/com.czygame.test.plist"];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
NSLog(@" Read content %@", data);
Copy the code
Test results:
-
Preprocessing strings: Processing strings after they are obtained: can be processed directly in Xcode and then called back to Unity; You can also pass strings directly to Unity for processing in Unity.
Because the package name I tested is the same as the previous Cocos bundle identifier. The fields saved in Cocos and playerpfs.set in Unity are found in the resulting string. At the same time. : |
Doesn’t that confirm what I suspected at the beginning of this paragraph? (I think the storage data read from Unity is similar to Cocos data)
I didn’t parse the string handling directly at this point (because I realized that it wasn’t a standard Json and that the print log was incomplete).
-
Then I hit on a hypothesis:
The data stored on both sides is stored as key:value in.plist. Can we assume that they are stored identically? So I wrote the code directly in Unity:
Debug.Log(playerprefs.haskey (“CocosKey”)); Repackage to the phone and find the log: True.
Third, draw a conclusion
Using data stored using Cocos is surprisingly common to Unity’s PlayerPrefs class.
That is, if the names and meanings of the fields stored in the two items are the same, no changes are required.
For example, Cocos uses the string “gold” to hold the number of coins held by the user. In Unity, you can directly playerprefs.getint (“gold”); Gets the number of coins currently held by this user
== there are two points to note: ==
- The two Bundle identifiers must be consistent
- The conversion type needs to be correct (e.g. GetInt();
Since I don’t have the Cocos Android package, I only verified that the above conclusion is true on IOS. If you have Cocos on Android, try to see if this is the same conclusion (tested, let me know in the comments). Tip: When testing on AndRoid, make sure the package name and signature are the same.
The clown was me.
As I did not know that they could be used, I tried the above process, but fortunately I finally found this conclusion. Record to share, hope this article can help you, if you have any other views or questions, please feel free to comment and exchange