Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Android Unity3d limitation streamingAssetsPath only Start method

Discussion in 'Android' started by LiefLayer, Apr 2, 2019.

  1. LiefLayer

    LiefLayer

    Joined:
    Jan 6, 2013
    Posts:
    65
    Here the details:
    https://stackoverflow.com/questions...ngassetspath-only-start-method-updated-device

    Summary:
    - I cannot read the streamingAssetsPath from android device, it will fail on
    yield return www.SendWebRequest();

    - The problem should not be the code since Jambla (the user that tried to answer my stack question said it works with his code).

    I'm not sure if this is a problem with my build settings or with my device/android version, I'm just sure the same code works if I call it on the Start method.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Most likely you are doing something wrong in those other scenarios. Could you show a more complete version of the code, best - a small full script that doesn't work for you.
    Please also provide the error that you get.
    The UWR code snipped you pasted is perfectly fine, so the issue with the code that snipped is put into.
     
  3. LiefLayer

    LiefLayer

    Joined:
    Jan 6, 2013
    Posts:
    65
    Problem is I do not get any error (at least in the editor, and I don't really know how to debug android with unity).
    I'm sure the problem is there because I tried to create a folder before and after that point, and the second folder fail to be created. But in the Start method everything works

    Still here the full code:
    Code (CSharp):
    1.  
    2. public IEnumerator CopyDialogs() {
    3. DirectoryInfo dirInf = new DirectoryInfo(Application.persistentDataPath + "/" + "dialogs");
    4.         if (!dirInf.Exists) {
    5.             dirInf.Create();
    6.         }
    7.         if (!File.Exists(Application.persistentDataPath + "/dialogs/dialog_number.json")) {
    8.             if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.WebGLPlayer) {
    9.                 UnityWebRequest www = UnityWebRequest.Get(Application.streamingAssetsPath + "/dialogs/dialog_number.json");
    10.                 yield return www.SendWebRequest();
    11.                 File.WriteAllBytes(Application.persistentDataPath + "/dialogs/dialog_number.json", www.downloadHandler.data);
    12.                 www.Dispose();
    13.                 www = null;
    14.             } else {
    15.                 File.Copy(Application.streamingAssetsPath + "/dialogs/dialog_number.json", Application.persistentDataPath + "/dialogs/dialog_number.json");
    16.             }
    17.         }
    Like you can see there is nothing else in the full code... everything works until
    UnityWebRequest www = UnityWebRequest.Get(Application.streamingAssetsPath + "/dialogs/dialog_number.json");

    (if I try to create a folder after that line it will be created)

    yield return www.SendWebRequest();
    //after this line it fail to do everything.

    Before I used:
    StartCoroutine(CopyDialogs());
    in a button.
    I also tried in the Update method (with a bool variable to stop it after one time).

    Now I call
    StartCoroutine(CopyDialogs());
    in the Start method.

    Also, you should understand that if I try to copy "dialog_number.json" manually before that point everything else is executed even if I not in the Start method.
    That's why I'm sure the problem is there.

    Still there is one last thing that I do after the Coroutine:
    Code (CSharp):
    1. Destroy(GameObject.FindWithTag("ToDestroy"));
    2.             SceneManager.LoadSceneAsync("Livello 0", LoadSceneMode.Additive);
    I don't know, maybe the process is slower on android so it destroy the gameobject with my script before the yield return anything... But I also tried to put that code at the end of the Coroutine so it should not be it.

    If work for everyone else maybe the problem is just my project set up.
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    The's not the full code. Show the entire MonoBehaviour. The problem is likely at the call point.
    Add error checking after SendWebRequest() (www.isNetworkError/www.error).
    You can debug Android using adb tool from Android SDK. `adb logcat` will print you the device log.
     
  5. LiefLayer

    LiefLayer

    Joined:
    Jan 6, 2013
    Posts:
    65
    The call point is in the Update

    Code (CSharp):
    1. private bool call = false;
    2.  
    3. void Update(){
    4. if(call){
    5. StartCoroutine(CopyDialogs());
    6. call = false;
    7. }
    8. }
    And there is a public method called from a button where I put call to false.
    Or I use
    StartCoroutine(CopyDialogs());

    from that method.
    Not sure how is possible to make it more simple than that.

    I will try to debug with adb logcat