将项目从Cocos2dX迁移到Unity Android时获取共享首选项 [英] Getting Shared Preferences while Migrating Project from Cocos2dX to Unity Android

查看:635
本文介绍了将项目从Cocos2dX迁移到Unity Android时获取共享首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于各种原因,我不得不将我的游戏从Cocos2dX移植到Unity. 我现在已经成功移植了该项目,但是要启动它,我必须建立一种机制来获取旧的用户数据并将其存储在新的结构中,例如锁定的级别数,较高的用户分数等. 搜索时,我发现COCOS2dX在以下路径上将数据存储在用户设备上

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

文件在那里,我可以通过Root Browser在根目录设备上看到该文件,但是在同一设备上运行该应用程序时出现错误. 我需要做的只是以编程方式访问该文件,稍后我将对其进行解析,并将其通过Unity存储以供将来使用.

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.

推荐答案

我不知道为什么我无法通过Unity Engine获取文件,我不得不编写自定义插件是Java,并在Unity Project中使用它,以及一些如何工作了.

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();
}

这篇关于将项目从Cocos2dX迁移到Unity Android时获取共享首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆