1
\$\begingroup\$

I need a hand on this . On Unity Editor this pretty works fine but when i'm building the apk and try to run my Language Changer function it can't find the path . Could some point me out where i am lost.

Debug.LogWarning("TODO: Loading Popup on.");

    rh.eLanguage language = tzGlobal.Instance.OPTION.language;

    string json = StreetUtility.LoadJsonFromStreamingAssets("notice.json");
    if (json != null)
    {
        // Separate only the necessary parts.
        LitJson.JsonData data = LitJson.JsonMapper.ToObject(json);
        json = data[language.ToString()].ToJson();

        notice = LitJson.JsonMapper.ToObject<string[]>(json);
    }

    string path = string.Format("{0}/{1}/language", rh.Const.LOCALIZATION_PATH, language);
    json = StreetUtility.LoadJsonFromResources(path);
    if (json != null)
    {
        // json load.
        Dictionary<string, string> dic = LitJson.JsonMapper.ToObject<Dictionary<string, string>>(json);

        // dictionary copy.
        dic_localization_text = new Dictionary<eTextKey, string>();
        eTextKey e;
        for (int i = 0; i < dic.Count; i++)
        {
            e = (eTextKey)i;
            dic_localization_text[e] = dic[e.ToString()];
        }

        // Run registered localize function.
        for (int i = 0; i < list_localize_method.Count; i++)
        {
            complete = false;
            list_localize_method[i].Invoke();
            yield return new WaitUntil(() => complete);
        }
    }

    Debug.LogWarning("TODO: Loading popup off.");

EDITTED:

my rh is a namespace obviously where in i put my file paths for example this rh.Const.LOCALIZATION_PATH here's what it looks like

// FILE PATH.
public static readonly string LOCALIZATION_PATH = "RH/Data/Localization";

And my StreetUtility class is here

using UnityEngine;
using System;
using System.IO;
using LitJson;

public class StreetUtility
{

// Parsing 1 (Unity default parser).
public static string ToJson<T>(T requestObject)
{
    return JsonUtility.ToJson(requestObject);
}

public static T FromJson<T>(string responseJson)
{
    return JsonUtility.FromJson<T>(responseJson);
}

// Parsing 2 (LitJson).
public static string ToJson_mk2<T>(T requestObject)
{
    return LitJson.JsonMapper.ToJson(requestObject);
}

public static T FromJson_mk2<T>(string responseJson)
{
    return LitJson.JsonMapper.ToObject<T>(responseJson);
}

// SAVE 1.
public static void SaveJson(string json, string path)
{
    using (FileStream fs = new FileStream(path, FileMode.Create))
    {
        using (StreamWriter sw = new StreamWriter(fs))
        {
            sw.Write(json);
        }
    }
 #if UNITY_EDITOR
    UnityEditor.AssetDatabase.Refresh();
 #endif
}

// SAVE 2.
public static bool SaveJson_mk2(string json, string path)
{
    try
    {
        StreamWriter writer = new StreamWriter(path, false);
        writer.WriteLine(json);
        writer.Close();
    }
    catch (Exception e)
    {
        Debug.LogWarningFormat("(Failed to save) 저장 실패.\n{0}\n{1}\n{2}", e, path, json);
        return false;
    }

#if UNITY_EDITOR
    UnityEditor.AssetDatabase.Refresh();
#endif
    return true;
}

// Resources 1.
public static string LoadJsonFromResources(string path_without_extention_under_resources_folder)
{
    TextAsset textAsset = Resources.Load<TextAsset>(path_without_extention_under_resources_folder);
    return textAsset.text;
}

// Load 2 (StreamingAssets).
public static string LoadJsonFromStreamingAssets(string path_with_extention_under_streaming_assets_folder)
{
    string json = null;
    try
    {
        //Android Platform
 #if UNITY_ANDROID
        string full_path = string.Format("{0}/{1}", Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
        StreamReader reader = new StreamReader(full_path);
        json = reader.ReadToEnd().Trim();
        reader.Close();

#elif UNITY_IOS //IOS Platform

#elif UNITY_STANDALONE //PC Platform
        string full_path = string.Format("{0}/{1}", Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
        StreamReader reader = new StreamReader(full_path);
        json = reader.ReadToEnd().Trim();
        reader.Close();

        Debug.Log(json);
 #endif
    }
    catch (Exception e)
    {
        Debug.LogWarningFormat("Failed to Load.\n{0}\n{1}", e, path_with_extention_under_streaming_assets_folder);
    }
    return json;
  }
}
\$\endgroup\$
2
  • \$\begingroup\$ I'm not familiar with the rh or StreetUtility you're using. Can you direct us to information about those, or include the relevant snippets of code? \$\endgroup\$
    – DMGregory
    Feb 26, 2018 at 17:58
  • \$\begingroup\$ @DMGregory rh is a namespace sir which includes all my file paths for example on LOCALIZATION_PATH i declared it as a static string which is public static readonly string LOCALIZATION_PATH = "RH/Data/Localization"; and about the StreetUtility i created a gist for that here it is. \$\endgroup\$
    – Gin09
    Feb 27, 2018 at 1:40

1 Answer 1

3
\$\begingroup\$

It won't find the path because Android has a different path when it comes to streaming assets.

Your code won't work, whereas the below will:

if (full_path.Contains("://") || full_path.Contains(":///"))
{
    UnityWebRequest file = new UnityWebRequest(full_path);
    while (!file.isDone) { }
    json = Convert.ToString(file.downloadHandler.data).Trim();
}
else
{
    json = File.ReadAllText(full_path);
}

You must use the UnityWebRequest class when streaming assets on Android.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .