Getting Shared Preferences while Migrating Project from Cocos2dX to Unity Android

陌路散爱 提交于 2019-12-11 07:36:37

问题


I had to port my game from Cocos2dX to Unity for various reasons. I have now ported the project successfully but to launch it I have to make a mechanism to get old user data and store it in new structure, like number of levels locked, high score of users etc. While searching I came to know that COCOS2dX stored data on user's device on following path

system/data/data/mygamepackage/shared_prefs/Cocos2dxPrefsFile.xml

Is there anyway to get data from above mentioned path? The above path should be accessible by the app it self. I am not able to read that file I get following error.

ENOENT No Such file or Directory

The file is there I can see that on rooted device via Root Browser but I get the error when I run the app on same device. All I need is to access the file programmatically and later I will parse it and will store it via Unity for future use.

Looking forward for a positive and quick response.


回答1:


I dont know why I am unable to get the file through Unity Engine, I had to write custom plugin is Java and use that in Unity Project and that some how has worked.

Following is the code which return string after reading the file on both rooted and non rooted devices

    public static String getCocos2DPrefsFile(String mPackageName)
{
    //Get the text file
            File file = new File("/data/data/"+mPackageName+"/shared_prefs/","Cocos2dxPrefsFile.xml");

            //Read text from file
            StringBuilder text = new StringBuilder();

            try {
                BufferedReader br = new BufferedReader(new FileReader(file));
                String line;

                while ((line = br.readLine()) != null) {
                    text.append(line);
                    text.append('\n');
                }
                br.close();
            }
            catch (IOException e) {
                //You'll need to add proper error handling here
                Log.d("Error", e.toString());
            }
            return text.toString();
}


来源:https://stackoverflow.com/questions/36721931/getting-shared-preferences-while-migrating-project-from-cocos2dx-to-unity-androi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!