Making a Texture2D readable in Unity via code
Select texture in project, open inspector window, set texture type to "Advanced", toggle "Read and write enabled".
How to make Texture2D Readable via script
There are two ways to do this:
1.Use RenderTexture
(Recommended):
Use RenderTexture
. Put the source Texture2D
into RenderTexture
with Graphics.Blit
then use Texture2D.ReadPixels
to read the image from RenderTexture
into the new Texture2D
.
Texture2D duplicateTexture(Texture2D source)
{
RenderTexture renderTex = RenderTexture.GetTemporary(
source.width,
source.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(source, renderTex);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = renderTex;
Texture2D readableText = new Texture2D(source.width, source.height);
readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
readableText.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(renderTex);
return readableText;
}
Usage:
Texture2D copy = duplicateTexture(sourceTextFromPlugin);
This should work and should not throw any error.
2.Use Texture2D.GetRawTextureData()
+ Texture2D.LoadRawTextureData()
:
You can't use GetPixels32()
because the Texture2D
is not readable. You were so close about using GetRawTextureData()
.
You failed when you used Texture2D.LoadImage()
to load from GetRawTextureData()
.
Texture2D.LoadImage()
is only used to load PNG/JPG array bytes not Texture2D array byte.
If you read with Texture2D.GetRawTextureData()
, you must write with Texture2D.LoadRawTextureData()
not Texture2D.LoadImage()
.
Texture2D duplicateTexture(Texture2D source)
{
byte[] pix = source.GetRawTextureData();
Texture2D readableText = new Texture2D(source.width, source.height, source.format, false);
readableText.LoadRawTextureData(pix);
readableText.Apply();
return readableText;
}
There will be no error with the code above in the Editor but there should be an error in standalone build. Besides, it should still work even with the error in the standalone build. I think that error is more like a warning.
I recommend you use method #1 to do this as it will not throw any error.
Make texture2D readable in runtime/script Unity3D
If you have the files in your project, you can select the texture in the inspector, set the texture type to "Advanced," then set "Read and write enabled" to true.
If not, you can try using GetRawTextureData() on the texture you have, then create a new texture with the same width and height and call LoadImage() on the new texture with the data you got from the old one, making sure markNonReadable is false. Then you should be able to do what you want on the new texture and display that while the user is cropping the image.
http://docs.unity3d.com/ScriptReference/Texture2D.GetRawTextureData.html
Mark Texture as Read/Write through code
You need to select all the textures you want to use in your function and set their Read/Write to enabled in the editor.
Making a Texture2D readable in Unity when loading from server
You could store the data from UnityWebRequest. What you are dong i a bit of useless for that purpose. You turn the result into a texture to convert the texture back to byte [].
byte[] bytes = www.uploadHandler.data;
File.WriteAllBytes(Application.persistentDataPath + "/art" + tag, data);
I'd guess this should do. No need for the .png extension as you store a raw array of bytes. You will then get the array and create a texture out of it:
byte [] bytes = File.ReadAllBytes(Application.persistentDataPath + "/art" + tag);
Texture2D texture = new Texture2D(4,4,TextureFormat.RGBA32, false);
texture.LoadImage(bytes);
Unity: How to change quality of texture2d with script
texture.filterMode = FilterMode.Point
docs.unity3d.com/ScriptReference/FilterMode.html
Related Topics
How to Asynchronously Read the Standard Output Stream and Standard Error Stream at Once
Exceptions That Can't Be Caught by Try-Catch Block in Application Code
Itextsharp - How to Get the Position of Word on a Page
Anyone Know of a Set of C# Bindings for Ffmpeg
Reasons for Why a Winforms Label Does Not Want to Be Transparent
Log Off User from Win Xp Programmatically in C#
Is There a Synchronization Class That Guarantee Fifo Order in C#
C#: How to Create an Attribute on a Method Triggering an Event When It Is Invoked
Ora-01008: Not All Variables Bound. They Are Bound
How to Detect Which .Net Runtime Is Being Used (Ms VS. Mono)
Simulating Cross Context Joins--Linq/C#
Selenium: Drag and Drop from File System to Webdriver
When Using .Net MVC Radiobuttonfor(), How to Group So Only One Selection Can Be Made
Does Dispose Still Get Called When Exception Is Thrown Inside of a Using Statement
Share Session Between Two Web Sites Using ASP.NET and State Server