如何从StreamingAssets文件夹中读取文件

时间:2017-12-15 04:28:44

标签: c# android json unity3d apk

在我的# UNITY_STANDALONE中,这行代码可以读取我的json文件,我就这样做了

Define_ConstantValue.cs

#if UNITY_STANDALONE
        string path = string.Format("{0}/datacenter.json", Application.streamingAssetsPath);
                     if (File.Exists(path)) {
                        StreamReader reader = new StreamReader(path);
                        try
                        {
                            CheckJSonManager.Instance._DataCenterJson = LitJson.JsonMapper.ToObject<DataCenterJson>(reader.ReadToEnd().Trim());
                            DataCenter_BaseURL = CheckJSonManager.Instance._DataCenterJson.dataCenter;
                        }
                        catch (Exception ex)
                        {
                            Debug.LogError("path : " + path + "\n" + ex);
                        }
                        reader.Close();
                    }


                    // (Get dealer server connection information) 딜러서버 접속 정보를 가져온다. 
                    // (If the json file below is present, 'I am a dealer console) 아래 json파일이 존재하면, '나는 딜러콘솔이다'
                        path = string.Format("{0}/dealerserver.json", Application.streamingAssetsPath);
                        if (File.Exists(path))
                        {
                            StreamReader reader = new StreamReader(path);
                            try
                            {
                                Debug.Log("*********************   DealerServer.JSON   ***************************");
                                CheckJSonManager.Instance._DealerServerJson = LitJson.JsonMapper.ToObject<DealerServerJson>(reader.ReadToEnd().Trim());
                            }
                            catch (Exception ex)
                            {
                                Debug.LogError("path : " + path + "\n" + ex);
                            }
                            reader.Close();
                        }
                        else
                        {
                            CheckJSonManager.Instance._DealerServerJson = new DealerServerJson();
                        }

                        // (Server list URL) 서버리스트 URL.
                        serverListJsonURL = string.Format("{0}/pc_version/ph/check/serverList.json", DataCenter_BaseURL);
                        NOTICE_URL = string.Format("{0}/pc_version/ph/check/notice.json", DataCenter_BaseURL);
   #endif

在我的

中显示如下

LogoUI.cs

 // URL을 로드한다 ( Load the URL ).
    CheckJSonManager.Instance._AwsDatacenterURL.SetURL();

    // notice를 다운로드한다 ( download 'notice.json ).
    WWW www = new WWW(CheckJSonManager.Instance._AwsDatacenterURL.NOTICE_URL);
    yield return www;

    //Debug PK 12/13/2017
    Debug.Log("www notice url = " + CheckJSonManager.Instance._AwsDatacenterURL.NOTICE_URL);

    // notice를 저장한다. ( save 'notice.json' )
    StreetUtility.SaveJson_mk2(www.text, string.Format("{0}/notice.json", Application.streamingAssetsPath));

    // (Download server list) 서버 리스트를 다운로드한다.
    www = new WWW(CheckJSonManager.Instance._AwsDatacenterURL.SERVERLIST_URL);
    yield return www;

    Debug.Log("www serverlist url = " + CheckJSonManager.Instance._AwsDatacenterURL.SERVERLIST_URL);

现在此代码返回值

  

www notice url = https://*********-***.amazonaws.com/pc_version/ph/check/notice.json

     

www serverlist url = https://*******-****.amazonaws.com/pc_version/ph/check/serverList.json

现在我的问题出现在我的# UNITY_ANDROID代码行中,因为它无法从我的streamingasset路径读取我的json文件。这是我的代码:

#if UNITY_ANDROID

        string path = "jar:file://" + Application.dataPath + "!/assets/datacenter.json";
        //string dataAsJson;

            if (File.Exists(path)) {
                WWW reader = new WWW(path);
                while (!reader.isDone) ;
                string filePath = string.Format("{0}/{1}", Application.persistentDataPath, "datacenter.json");
                File.WriteAllBytes(filePath, reader.bytes);
            //byte[] dataAsBytes = reader.bytes;
            //dataAsJson = System.Text.Encoding.Default.GetString(dataAsBytes);

            StreamReader wr = new StreamReader(filePath);
            string line;
            while ((line = wr.ReadLine())!= null)
            {
                try
                {
                    CheckJSonManager.Instance._DataCenterJson = LitJson.JsonMapper.ToObject<DataCenterJson>(reader.text);
                    DataCenter_BaseURL = CheckJSonManager.Instance._DataCenterJson.dataCenter;
                }
                catch (Exception ex)
                {
                    Debug.LogError("Path : " + path + "\n" + ex);
                }
            }  
            }

            path = "jar:file://" + Application.dataPath + "!/assets/dealerserver.json";

            if (File.Exists(path))
            {
                WWW reader = new WWW(path);
                while (!reader.isDone) { }
                string filePath = string.Format("{0}/{1}", Application.persistentDataPath, "dealerserver.json");
                File.WriteAllBytes(filePath, reader.bytes);
            //byte[] dataAsBytes = reader.bytes;
            //dataAsJson = System.Text.Encoding.Default.GetString(dataAsBytes);

            StreamReader wr = new StreamReader(filePath);
            string line;
            while ((line = wr.ReadLine()) != null)
            { 
                try
                {
                    Debug.Log("*************************  DealerServer.JSON *************************");
                    CheckJSonManager.Instance._DealerServerJson = LitJson.JsonMapper.ToObject<DealerServerJson>(reader.text);
                }
                catch (Exception ex)
                {
                    Debug.LogError("Path : " + path + "\n" + ex);
                }

            }
            } else
            {
                CheckJSonManager.Instance._DealerServerJson = new DealerServerJson();
            }
        serverListJsonURL = string.Format("{0}/pc_version/ph/check/serverList.json", DataCenter_BaseURL);
        NOTICE_URL = string.Format("{0}/pc_version/ph/check/notice.json", DataCenter_BaseURL);
  #endif

我在#LogoUI.cs上将其称为相同,并返回值

  

www serverlist url = /pc_version/ph/check/serverList.json

     

www notice url = /pc_version/ph/check/notice.json

我猜这行代码在android上不起作用?

serverListJsonURL = string.Format("{0}/pc_version/ph/check/serverList.json", DataCenter_BaseURL);
NOTICE_URL = string.Format("{0}/pc_version/ph/check/notice.json", DataCenter_BaseURL);

更新:

当我将项目构建为.apk并使用logcat进行调试时,我得不到# UNITY_STANDALONE# UNITY_ANDROID的结果。

在我的

# UNITY_STANDALONE

它可以获得此值

  

www notice url = https://*********-***.amazonaws.com/pc_version/ph/check/notice.json

     

www serverlist url = https://*******-****.amazonaws.com/pc_version/ph/check/serverList.json

在我的

# UNITY_ANDROID

它不会返回我在datacenter.json上的这个值

  

www serverlist url = /pc_version/ph/check/serverList.json

     

www notice url = /pc_version/ph/check/notice.json

它应该返回通知url和serverlist url这样的值

  

www notice url = https://*********-***.amazonaws.com/pc_version/ph/check/notice.json

     

www serverlist url = https://*******-****.amazonaws.com/pc_version/ph/check/serverList.json

0 个答案:

没有答案