Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

OBB file as Streaming Asset for Android

Discussion in 'Android' started by Algoros, Mar 8, 2021.

  1. Algoros

    Algoros

    Joined:
    Jun 11, 2015
    Posts:
    18
    Hello,

    we currently working an a application with only one scene (not a game).
    Because the application is more then 600 MB big, I a moved most of the content to a Streaming asset and checked the split checkbox in the build settings. After compiling, the split worked and the Obb has most auf the size.
    Now I try to access the data in the Android OS (only images, zip and audio, but 30.000 files).
    The follow code is not working, like I read in the documentation:
    Code (CSharp):
    1. DataPath = Path.Combine(Application.streamingAssetsPath, DataFolder).Replace("\\", "/");
    The documentation itself has deadlinks:
    https://docs.huihoo.com/unity/5.5/Documentation/Manual/android-OBBsupport.html

    as example:
    "the asset store has a plugin (adapted from the Google Apk Expansion examples) which does this for you. It will download the .obb and put it in the right place on the sdcard. See http://u3d.as/content/unity-technologies/google-play-obb-downloader/2Qq"

    I dont know how I fix this file access issues on Android OS or maybe there is another option.
    This is the reason why Im asking here:
    Is there some example how I access Streaming Assets Obb files on Android or maybe is there a better solution?

    KR Martin
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    I don't quite understand your issue. Do you have problems with obb or accessing streaming assets?
    When you build and run from Unity, obb should be pushed to device and available out of the box.
    As for streaming assets, how are you reading them?
     
  3. Algoros

    Algoros

    Joined:
    Jun 11, 2015
    Posts:
    18
    Hello @Aurimas-Cernius , thanks for the fast answer.
    Its about accessing the streaming assets content as obb on Android.
    On Windows all streaming assets are loaded fine, on android the apk works and I see my user interface but all other content stored as Streaming assets as obb. are not loaded.

    currently getting the path to the streaming assets looks like this:
    Code (CSharp):
    1. #if UNITY_ANDROID
    2.             DataPath = Path.Combine(Application.streamingAssetsPath, DataFolder).Replace("\\", "/");
    3. #endif
    4.  
    5. #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
    6.  
    7.             DataPath = Path.Combine(Application.streamingAssetsPath, DataFolder).Replace("\\", "/");
    After this, a massive script is running to load more then 30.000 files Images, Audio and thumbs

    Loading data looks like this:
    Code (CSharp):
    1.  
    2.     public IEnumerator LoadData()
    3.     {
    4.         if (!IsInitialized)
    5.         {
    6.             Debug.Log("Loading data...");
    7.             try
    8.             {
    9.                 var textData = File.ReadAllText(DataPath + QuestionsFile);
    10.  
    11.                 LoadData(textData);
    12.  
    13.                 RetryExtractAudio();
    14.                 _zipFile = new ZipFile(File.OpenRead(DataPath + SettingsManager.Instance.DataZip));
    15.  
    16.                 //Update item count
    17.                 UpdateItemCount();
    18.  
    19.            
    20.             } catch (Exception exc) { Debug.LogError($"Error loading data: {exc.Message} \n {exc.InnerException}"); }
    21.         }
    22.         yield return null;
    23.     }
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    On Android streaming assets are inside apk or obb. In both cases you have a file inside zip archive, so the .NET File APIs won't work.
    You can read streaming assets via UnityWebRequest. The path is already a jar:file:// uri on Android, on Windows you have to make it an URI, best by passing path to constructor of System.Uri class.
     
  5. Algoros

    Algoros

    Joined:
    Jun 11, 2015
    Posts:
    18
    Hello @Aurimas-Cernius,

    thanks for your answer.
    This means we have to change all .NET File usage to UnityWebRequest?
    Or is there a option to "extract" the obb und put the files back to unity so that I can continue using File .Net Solution.

    currently I have no idea and I cant find any helpful example to change following to UnityWebRequest:
    Code (CSharp):
    1.  public void CheckGetFile(string url)
    2.     {
    3.         if (!File.Exists(DataPath + url))
    4.         {
    5.             var filename = Path.GetFileName(url);
    6.             foreach (ZipEntry entry in _zipFile)
    7.             {
    8.                 if (entry.Name.Contains(filename))
    9.                 {
    10.                     var dirPath = Path.GetDirectoryName(DataPath + MediaPath + entry.Name);
    11.                     if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); }
    12.  
    13.                     if (!File.Exists(DataPath + MediaPath + entry.Name) && entry.IsFile)
    14.                     {
    15.                         using (var zipStream = _zipFile.GetInputStream(entry))
    16.                         {
    17.                             using (var writer = File.OpenWrite(DataPath + MediaPath + entry.Name))
    18.                             {
    19.                                 while ((_size = zipStream.Read(_buffer, 0, _buffer.Length)) > 0)
    20.                                 {
    21.                                     writer.Write(_buffer, 0, _size);
    22.                                 }
    23.                                 writer.Close();
    24.                             }
    25.                         }
    26.                     }
    27.                 }
    28.             }
    29.         }
    30.     }
    Is there a option to test the implementation in Windows without uploading the solution to playstore?
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    You can keep using File on Windows and other platforms, only Android is special. OBB created by Unity is itself a zip file, so if you obtain a path to it, you use zip reader to get files from inside of it. UnityWebRequest is a universal way.
    Extracting streaming assets and storing them on disk is a waste of time and disk space in my opinion.
     
  7. Algoros

    Algoros

    Joined:
    Jun 11, 2015
    Posts:
    18
    Hello @Aurielle_Aurilie,

    I think I understand.
    So because it is a archive .Net file will not work.
    But I do not understand why UnityWebRequest is a universal way.
    I use UnityWebRequest to communicate with my backend and download files from my server.

    Code (CSharp):
    1.     private void Download(string address)
    2.     {
    3.         var webRequest = UnityWebRequest.Get(address);
    4.  
    5.         var operation = webRequest.SendWebRequest();
    6.         StartCoroutine(CoDownloadProgress(operation));
    7.     }
    More important:
    Is there a way to test the obb implementation without uploading all the stuff to PlayStore?

    I found a package in Asset Store from @gwiazdorrr called better streaming asset,
    maybe, this one can help as well?

    https://assetstore.unity.com/packages/tools/input-management/better-streaming-assets-103788
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    UnityWebRequest supports reading files via file:// URI, while on Android it has a special support for jar:file:// which can read from inside apk/obb.
    Doesn't Unity build and run push obb to device?
     
    yalchibaev_ likes this.
  9. Algoros

    Algoros

    Joined:
    Jun 11, 2015
    Posts:
    18
    @Aurimas-Cernius
    push obb on device works fine. My question was only regarding if I can test reading obb on a windows platform.

    I tried now reading a json file with:
    Code (CSharp):
    1.             DataPath = Path.Combine("file://"+ Application.streamingAssetsPath, DataFolder).Replace("\\", "/");
    2. .......
    3.             using (var www = UnityWebRequest.Get(DataPath + QuestionsFile))
    4.             {
    5.                 yield return www.SendWebRequest();
    6.  
    7.                 if (www.isNetworkError || www.isHttpError)
    8.                 {
    9.                     Debug.LogErrorFormat(this, "Unable to load texture due to {0} - {1}", www.responseCode, www.error);
    10.                 }
    11.                 else
    12.                 {
    13.                     //Texture myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
    14.                     LoadData(www.downloadHandler.text);
    15.                 }
    16.             }
    Which works fine for me. Similar results like:

    Code (CSharp):
    1. var textData = File.ReadAllText(DataPath + QuestionsFile);
    Is the only think I have to change now file:// to

    Code (CSharp):
    1.   DataPath = Path.Combine("jar:file://"+ Application.streamingAssetsPath, DataFolder).Replace("\\", "/");
    And it should work on my android phone?
     
  10. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    Almost. Application.streamingAssetsPath already includes jar:file:// in it, don't prepend it.
     
  11. Algoros

    Algoros

    Joined:
    Jun 11, 2015
    Posts:
    18
    @Aurielle_Aurilie
    Thank you very much!
    Yep, it worked for the json file now.
    Sorry for another question - but UnityWebRequest is quit new for me.
    Code (CSharp):
    1.         _fileNames = Directory.GetFiles(DataPath + MediaPath + ImagesPath, "*.jpg");
    Is there similar solution with webrequest?
     
    Last edited: Mar 9, 2021
  12. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    No, with streaming assets the general rule is that you are expected to know what you've put in there.
     
  13. Algoros

    Algoros

    Joined:
    Jun 11, 2015
    Posts:
    18
    @Aurimas-Cernius
    Okay, its fine. Something I can change.

    Currently the debugging of the solution is horrible. Change Code, Build APK with OBB, Create new Release on PlayStore, Upload APK, download new App und start test on Android phone.

    Is there a easier way?
    Something like reading the obb with the unity editor?
     
  14. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    Why do you need play store here? Why not build and run from unity to device?
    Unity generated obb file is a zip archive, so you can read it using the similar code that you pasted above.
     
  15. Algoros

    Algoros

    Joined:
    Jun 11, 2015
    Posts:
    18
    @Aurimas-Cernius
    Thank you very much.
    The App is now in the internal Test phase. All Content is visible and works like expected.
    You are the best.

    KR