By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I am trying to convert a RawImage in a array bytes bytes[] but the RawImage don't have a encondePNG or something else to get a bytes for a RawImage, any idea how I can get the array of bytes?

RawImage is just a component rendering a Texture assigned to its texture property. To get the byte array, you need to access that Texture first then cast it to Texture2D.

To also convert the the Color32[] to byte array see this post.

Unity 5 Tutorial - Importing Textures and Creating Materials in Unity 5

Learn more. Asked 1 year, 5 months ago. Active 1 year, 5 months ago. Viewed 1k times.

Converting Render Textures to RBGA byte array via native plugin

Programmer Active Oldest Votes. GetPixels32 ; To also convert the the Color32[] to byte array see this post. Programmer Programmer Nothing to add. The Png file growed from kb to 1. How could we archieve the same compression level on PNG?

That's interesting. When I said "uncompressed data", I meant the one loaded in and represented as in Texture data format which is obtained by GetPixels32. I suspect that the loaded data is bigger because Unity has to store more information about the texture than just the actual size.

Also, there is mipmaps being applied to the loaded texture. We can't tell without the source code of Unity. No I totally agree with you. I just thought maybe you came across this as well but maybe I should open a question for that? Sure you can. I think that would probably require someone that works at Unity to answer that.Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

For some reason your suggested change could not be submitted. And thank you for taking the time to help us improve the quality of Unity Documentation. This function fills texture pixel memory with raw data.

This is mostly useful for loading compressed texture format data into a texture. Passed data should be of required size to fill the whole texture according to its width, height, data format and mipmapCount ; otherwise a UnityException is thrown.

Mipmaps are laid out in memory starting from largest, with smaller mip level data immediately following. For example, a 16x8 texture of RGBA32 format with no mipmaps can be filled with a byte array 16x8x4. For runtime texture generation, it is also possible to directly write into texture data via GetRawTextureData that returns a Unity.

This can be faster since it avoids a memory copy that LoadRawTextureData would do. Call Apply after setting image data to actually upload it to the GPU. Is something described here not working as you expect it to? It might be a Known Issue.

Please check with the Issue Tracker at issuetracker. Version: Language English. Scripting API. Suggest a change. Submission failed For some reason your suggested change could not be submitted. Parameters data Raw data array to initialize texture pixels with. LoadRawTextureData pvrtcBytes ; tex. Publication Date: By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

unity texture to byte array

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

I am writing an Android app in Unity C which implements an sqlite database. The program tries to store pictures in the database with relevant info. At step 3, when trying to convert the byte array back into a 'texture2d', something goes wrong. A red question mark is displayed instead of the png. The variable 'img' is supposed to hold the texture converted back from the byte array, but trying to display 'img' just shows a big red question mark.

Any help would be greatly appreciated.

Learn more. Then display Ask Question. Asked 2 years, 4 months ago. Active 1 year, 3 months ago. Viewed 5k times. I am trying to solve the following steps to solve a piece of this: Display a png as 'texture2d' located in the Resources folder - achieved Convert the png 'texture2d' to a byte array to save to a db -???

MattBorg MattBorg 57 1 1 silver badge 7 7 bronze badges. But an extension is not needed. When i bring 'eeyore' in its saved in 'photo'. In the next to last line, if i use 'photo' instead of 'img', the image will display.

If i try to display 'photo' it shows up just fine. After trying to convert 'photo' to a byte[] array, then back it won't show. Active Oldest Votes. LoadImage vs ; icon. Apply. Luke Skywalker Luke Skywalker 1 1 1 bronze badge. SetUploadId downloadId. Robert Columbia 5, 9 9 gold badges 26 26 silver badges 35 35 bronze badges. What the hell?Thank you for helping us improve the quality of Unity Documentation.

Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

unity texture to byte array

For some reason your suggested change could not be submitted. And thank you for taking the time to help us improve the quality of Unity Documentation. The returned byte array is the PNG "file". You can write them to disk to get the PNG file, send them over the network, etc. This function works only on uncompressed, non-HDR texture formats. PNG data will not contain gamma correction or color profile information.

Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at issuetracker. Version: Language English. Scripting API. Suggest a change. Submission failed For some reason your suggested change could not be submitted. Parameters tex The texture to convert. Networking; using System. Collections; using System. ReadPixels new Rect 0, 0, width, height0, 0 ; tex.

unity texture to byte array

WriteAllBytes Application. AddField "frameCount", Time. ToString ; form. SendWebRequest ; if w. Log w. Publication Date: Select your preferred scripting language. All code snippets will be displayed in this language. Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Subscribe to RSS

This function replaces texture contents with new image data. After LoadImage, texture size and format might change. If the platform is not able to perform runtime compression then false will be returned. Texture will be uploaded to the GPU automatically; there's no need to call Apply. Legacy Documentation: Version 5.

Language English. C JS Script language. Scripting API. Suggest a change. Sumbission failed For some reason your suggested change could not be submitted. Parameters data The byte array containing the image data to load. Returns bool Returns true if the data can be loaded, false otherwise. LoadImage imageAsset. Publication 5.Search Unity.

Log in Create a Unity ID. Unity Forum. Forums Quick Links. Asset Store Spring Sale has begun! Unite Now has started! Come level up your Unity skills and knowledge. Come post your questions!

unity texture to byte array

Joined: Jan 2, Posts: 1. I know that there are other questions asking this, but I haven't been able to find one with an answer that works. The program I'm writing requires me to take a Unity RenderTexture object and convert it into an RGB A buffer, and be able to do that in as little time as possible at absolute most 15 ms, I'd love to go far below that.

I know that the fastest way to do that is going to be with a native plugin, but I can't for the life of me get a native plugin to read a rendertexture into a byte buffer with OpenGL.

Collections; using System. Generic; using UnityEngine; using UnityEngine. Rendering; using System. It's all zeroes. I don't really know what the right way to do this is.

I've tried four or five, all of them left the byte array black. If you have a better way, please let me know. This is someone else's code, that I found in a post from I can't find again. AlyssInWonderlandJul 31, Joined: Oct 11, Posts: 1, Wow, that is a lot of code for something like that. You can do that into a Texture2D using ReadPixels. This does take some time, but that's just how it is.

The only way to speed it up is to compress the RenderTexture on the GPU so the amount of data to transfer is less. You must log in or sign up to reply here. Show Ignored Content. Your name or email address: Password: Forgot your password?By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I have a. How can i convert IntPtr to Byte[]? I think that payload is actually Byte[]. If it's not Byte[] and it's something else would i lose some data?

If it's not byte[]the size parameter in of Marshal. Copy is the number of elements in the array, not the byte size. So, if you had an int[] array rather than a byte[] array, you would have to divide by 4 bytes per int to get the correct number of elements to copy, assuming your size parameter passed through the callback refers to of bytes.

According to this Stack Overflow questionyou can do the following:. You can use Marshal. It is faster as you won't need to allocate and copy to a new buffer and safer as you will not have to use directly the pointer.

It's included in. NET Core 2. Memory for. NET Framework 4. Learn more. Asked 9 years ago.