Search Unity

Size of binary asset.bytes wrong.

Discussion in 'Scripting' started by raypendergraph, Dec 28, 2009.

  1. raypendergraph

    raypendergraph

    Joined:
    Sep 8, 2009
    Posts:
    12
    I am having an issue loading binary assets via Resources.Load(). I know this has been beat to death in the forums (found a few older posts), but I cannot seem to get it to work. What I am trying to do is launch educational content in PDF files via the browser or external PDF reader. These files are bundled with the game so I have to put them in an external location. Here is the code I am using to do that:

    Code (csharp):
    1.          
    2. TextAsset asset = (TextAsset)Resources.Load(assetPath);
    3. string path = Path.Combine(Path.GetTempPath(), assetPath);
    4. Directory.CreateDirectory(Path.GetDirectoryName(path));
    5.    if (!File.Exists(path)) {
    6.       File.WriteAllBytes(path, asset.bytes);
    7.    }
    8. Application.OpenURL(path);
    9.  
    Everything is working except the fact that the bytes that are returned from the Load (asset.bytes.Length) for PDF and some other binary formats is way too short. It works for an XML (foo.xml.txt) text file fine, but not for PDF (foo.pdf.txt). The file that is written is the same size as the buffer (too short) and opens the external application but displays nothing, of course. Is it possible that there are characters in the PDF files that confuse Unity and make it think that it is EOF or something? Can someone show me where I am going wrong?
     
  2. raypendergraph

    raypendergraph

    Joined:
    Sep 8, 2009
    Posts:
    12
    Some more testing. This seems to work for arbitrarily large property files or xml files (i.e., text files). One comes up in textpad and the xml file opens IE. Just as I would suspect. All sizes of the externally copied file (to the temp directory) is correct. But when the resource is really a binary format, this will not work. The buffer is only a fraction of the size it should be but there is data in it.

    Maybe it works for small cases I guess they really do mean "text asset" despite some past posts about being able to get the raw resource bytes via the bytes property. I feel like I am doing something wrong because this seems to be something that is supported.
     
  3. raypendergraph

    raypendergraph

    Joined:
    Sep 8, 2009
    Posts:
    12
    We got around the binary vs text issue by converting our binary assets to Base64 and using the code posted above to write it out to the drive. Takes up more space (about 133%) but we only have a few of these. Maybe an enhancement to this would be zip -> base64 then just run the result from the Convert class (the bits) through a zip stream before writing to disk. That would balance the bloat caused by the base64 conversion.
     
  4. Rikus

    Rikus

    Joined:
    Nov 19, 2010
    Posts:
    2
    You can't read binary files in Unity?
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    yes you can.

    But you would use bytes then, but you would not use bytes for texts as text due to the encoding is loaded differently, which is why there is www.text. you can naturally also get it from bytes but then you must decode it first (I'm doing that to serialize objects to and from xml for WWW transmission)
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Unity 2.6 had a bug with TextAsset binary files. In Unity 3 it's fixed.

    --Eric