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

MemoryStream GetBuffer

Discussion in 'Windows' started by xlarrode, Nov 26, 2013.

  1. xlarrode

    xlarrode

    Joined:
    Nov 9, 2012
    Posts:
    19
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    This seems to be a very specfial use of MemoryStream. What are you using it for?
     
  3. xlarrode

    xlarrode

    Joined:
    Nov 9, 2012
    Posts:
    19
    I'm loading some zip files at runtime :

    Code (csharp):
    1.  
    2. foreach (ZipEntry e in zip)
    3.                 {
    4.                     //...
    5.                         MemoryStream outputStream = new MemoryStream ();
    6.                         e.Extract (outputStream);
    7.                         Texture2D texture = new Texture2D (4, 4);
    8.                         if (texture.LoadImage (outputStream.GetBuffer ()))
    9.                         {
    10.                             //...
    11.                         }
    12.                                                //...
    13. }
    14.  
    It's not a very special use or memoryStream, is it?
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Looks like you caould use MemoryStream.ToArray(). It's less efficient (creates copy), but available on WinStore.
     
  5. xlarrode

    xlarrode

    Joined:
    Nov 9, 2012
    Posts:
    19
    Yes i was trying that but right now my textures don't look good ;)

    Code (csharp):
    1.  
    2. MemoryStream outputStream = new MemoryStream ();
    3.                         e.Extract (outputStream);
    4.                         string outputAsString = System.Text.Encoding.UTF8.GetString(outputStream.ToArray());
    5.                         Texture2D texture = new Texture2D (4, 4);
    6.                         if (texture.LoadImage(System.Text.Encoding.UTF8.GetBytes(outputAsString)))
    7.                         {
    8.  
    $textureWrong.PNG
     
    Last edited: Nov 27, 2013
  6. xlarrode

    xlarrode

    Joined:
    Nov 9, 2012
    Posts:
    19
    So this seems to work on editor :

    Code (csharp):
    1.  
    2. string outputAsString = GetStringFromMemory(outputStream, System.Text.Encoding.Default);
    3.                     if (texture.LoadImage(System.Text.Encoding.Default.GetBytes(outputAsString)))
    4. {
    5.  
    6. }
    7.  
    8.  
    9. public static string GetStringFromMemory(MemoryStream memoryStream, System.Text.Encoding encoding)
    10.     {
    11.         string text = string.Empty;
    12.         memoryStream.Position = 0;
    13.         using (TextReader reader = new StreamReader(memoryStream, encoding))
    14.         {
    15.             text = reader.ReadToEnd();
    16.         }
    17.         return text;
    18.     }
    19.  
    But unfortunately System.Text.Encoding.Default don't exist on METRO and neither, UTF8 or Ascii are working :(
    Any ideas ?
     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,644
    What do you mean by "it's not working"?
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    I think he means, that Encoding classes are missing as well :(


    What you can try is to use MemoryStream.Read() to read into byte array. Just allocate the array that has the same Length as the stream and Seek() to beginning of the stream before reading. Hopefully this will work...
     
  9. xlarrode

    xlarrode

    Joined:
    Nov 9, 2012
    Posts:
    19
    Ok, for the textures i don't need to play with encoding, memoryStream.ToArray() is perfect...
    Thanks a lot.

    But i'v got another issue now with NGUI but it will be on another topic
    Thanks
     
  10. xlarrode

    xlarrode

    Joined:
    Nov 9, 2012
    Posts:
    19
    Encoding.Default is missing, but Encoding.UTF8 and Encoding.ASCII are available...
     
  11. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,644
    So again:

    It should work fine, and if it doesn't that smells like a bug!
     
  12. xlarrode

    xlarrode

    Joined:
    Nov 9, 2012
    Posts:
    19
    Sorry for my english ;)
    So the windows8 version doesn't have MemoryStream.GetBuffer and no Encoding.Default.
    But i manage without so everything is ok now
    Thanks