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

Addressable Assets loading a binary file (TextAsset - .bytes file)

Discussion in 'Addressables' started by Serge144, Feb 16, 2021.

  1. Serge144

    Serge144

    Joined:
    Oct 2, 2018
    Posts:
    64
    I have a group of binary files (.byte files) which I'm currently loading through the Resources folder and everything is working as intended. For example, to load one of the files I'm doing the following:

    Code (CSharp):
    1. public static int[,] LoadTerrainDataBytes(string terrainName) {
    2.         TextAsset ta = Resources.Load("Terrain Details/" + terrainName, typeof(TextAsset)) as TextAsset;
    3.         using (MemoryStream ms = new MemoryStream(ta.bytes)) {
    4.             BinaryFormatter bf = new BinaryFormatter();
    5.             TerrainStorageData tsd = (TerrainStorageData)bf.Deserialize(ms);
    6.             return tsd.map;
    7.         }
    8.     }
    But, after some more investigation I noticed that using resources folder isn't the best practice, and found about Addressable Assets. I have seen some examples on how to load prefabs and scenes with this new System, but I haven't found any example with binary files (TextAsset)
    How can I replace the above method but using Addressable Assets system, and converting to my TerrainStorageData object?

    Thank you in advance.
     
    AldeRoberge, LLZ_T and morhun_EP like this.
  2. AldeRoberge

    AldeRoberge

    Joined:
    Jun 23, 2017
    Posts:
    48
    I'm facing the problem, and seeing as this thread has no answer, I would like to know.

    The question is simple : How do I load a file containing raw binary data using addressables?

    _______________________

    Edit : We can use the TextAsset, since it supports binary files too.
    But it's a little misleading, because for Unity to treat the file as a TextAsset, the extension must be .txt, .csv, .json, or something like that. It can't be .bin, .bytes or .dat, since it would be treated as a DefaultAsset.
     
    Last edited: Mar 31, 2023
  3. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    979
    AldeRoberge likes this.