Search Unity

DirectoryNotFoundException: Could not find a part of the path

Discussion in 'Editor & General Support' started by SophiaKramer, Dec 29, 2020.

  1. SophiaKramer

    SophiaKramer

    Joined:
    Feb 14, 2016
    Posts:
    9
    Hello! I'm having a problem trying to load an Streaming Asset on my Android game. It works neatly on the Editor, but not on the build. The error log is this one:

    The code for loading the asset consists of two methods:
    Code (CSharp):
    1.    public void LoadLocalizedText(string fileName){
    2.        localizedText = new Dictionary<string, string> ();
    3.        string filePath = Path.Combine (Application.streamingAssetsPath, fileName);
    4.  
    5.        string newPath = Path.Combine(Application.persistentDataPath, fileName);
    6.  
    7.        DoWWW_Android(fileName, filePath, newPath);
    8.  
    9.        if (File.Exists(newPath)) {
    10.            string dataAsJson = File.ReadAllText(filePath);
    11.            LocalizationData loadedData = JsonUtility.FromJson<LocalizationData>(dataAsJson);
    12.  
    13.            for (int i = 0; i < loadedData.items.Length; i++){
    14.                localizedText.Add(loadedData.items[i].key, loadedData.items[i].value);
    15.            }
    16.  
    17.            Debug.Log("Data loaded, dictionary contains " + localizedText.Count.ToString() + " entries");
    18.        } else {
    19.            Debug.LogError("Cannot Find Localization File!");
    20.        }
    21.  
    22.        isReady = true;
    23.    }
    and
    Code (CSharp):
    1.    static void DoWWW_Android(string name, string origPath, string newPath) {
    2.        using (UnityWebRequest reader = UnityWebRequest.Get(origPath)) {
    3.            reader.SendWebRequest();
    4.            while (!reader.isDone) {
    5.            }
    6.            if (!reader.isHttpError && !reader.isNetworkError) {
    7.                File.WriteAllBytes(newPath, reader.downloadHandler.data);
    8.            }
    9.            else {
    10.                Debug.LogError("Database " + name + " not found at " + origPath);
    11.                Debug.LogError("Error: " + reader.error);
    12.            }
    13.        }
    14.    }
    Can anyone help me to find what's going wrong with this??
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Joe-Censored likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,740
    Everything that Praetor says above, plus this is my directory finder since Unity for some inscrutible bizarre reason thinks we want to care about these nitty gritty dirty laundry details of paths on different platforms:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public static class StreamingAssetsPath
    4. {
    5.     public static string StreamingAssetPathForWWW()
    6.     {
    7.         #if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN
    8.         return "file://" + Application.dataPath + "/StreamingAssets/";
    9.         #endif
    10.         #if UNITY_ANDROID
    11.         return "jar:file://" + Application.dataPath + "!/assets/";
    12.         #endif
    13.         #if UNITY_IOS
    14.         return "file://" + Application.dataPath + "/Raw/";
    15.         #endif
    16.         throw new System.NotImplementedException( "Check the ifdefs above.");
    17.     }
    18.     public static string StreamingAssetPathForFileOpen()
    19.     {
    20.         #if !UNITY_EDITOR && UNITY_ANDROID
    21.         throw new System.NotImplementedException( "You cannot open files on Android. Must use WWW");
    22.         #endif
    23.  
    24.         Debug.Log( "Application.streamingAssetsPath:" + Application.streamingAssetsPath);
    25.         return Application.streamingAssetsPath + "/";
    26.     }
    27. }
     
  4. RiviV33

    RiviV33

    Joined:
    Sep 10, 2021
    Posts:
    4
    Hi,
    Can you show me an example of how to use UnityWebRequest to access a file in the android build
     
    AB498 likes this.
  5. AB498

    AB498

    Joined:
    May 4, 2019
    Posts:
    17
    an android example will be
    Code (CSharp):
    1. string path = Path.Combine (Application.streamingAssetsPath, "fileName.txt");
    2. var loadingRequest = UnityWebRequest.Get(path);
    3. loadingRequest.SendWebRequest();
    4. while (!loadingRequest.isDone && !loadingRequest.isNetworkError && !loadingRequest.isHttpError);
    5. string result = System.Text.Encoding.UTF8.GetString(loadingRequest.downloadHandler.data);
     
  6. divy_unity

    divy_unity

    Joined:
    Jul 7, 2023
    Posts:
    1
    hi, I want to load a unity.sentis ML model but getting this compilation error (Argument 1: cannot convert from 'byte[]' to 'Unity.Sentis.ModelAsset') while loading the download data.
    Code (CSharp):
    1. void LoadModel()
    2.     {
    3.         //Load model
    4.         const string modelName = "yolov8n.sentis";
    5.         string modelPath = Path.Combine(Application.streamingAssetsPath, modelName);
    6.         var loadingRequest = UnityWebRequest.Get(modelPath);
    7.         loadingRequest.SendWebRequest();
    8.         while (!loadingRequest.isDone && loadingRequest.result == UnityWebRequest.Result.ConnectionError && loadingRequest.result == UnityWebRequest.Result.ProtocolError);
    9.      
    10.         model = ModelLoader.Load(loadingRequest.downloadHandler.data);
    11.  
    12.     }
     
    dannykoh likes this.
  7. m22ai873

    m22ai873

    Joined:
    Jan 14, 2024
    Posts:
    8
    which Unity version are you using?