티스토리 뷰
[해결책] UnityException: Texture '' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.
나일해 2021. 4. 7. 19:491.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.
참고
stackoverflow.com/questions/44733841/how-to-make-texture2d-readable-via-script
- Total
- Today
- Yesterday
- intellij
- 포큐후기
- POCU후기
- AWSSDK
- framework
- 운동
- running
- 참고
- C#강의
- Unity
- Settings
- 프로그래밍입문
- Code Conventions
- 포큐아카데미후기
- web
- 일일회고
- 씨샵강의
- json
- POCU아카데미
- adb
- NRC
- Browsers
- 포큐아카데미
- .NET
- POCU
- errors
- springboot
- 프로그래밍강의
- 단축키
- 환경변수설정
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |