从Texture2D创建UI图像 [英] Creating UI Image from Texture2D
问题描述
我正在开发一个实时流应用,该应用接收 JPEG 图像作为字节数组,并显示它在屏幕上和UI.Image
. 工作很好,但是我正在进行优化,并且有几个问题.目前,我在下面的代码将字节数组转换为Texture2D
,然后从Texture2D
创建一个Sprite
,然后将Sprite
分配给UI.Iamge
在屏幕上显示.
I working on a live stream App that receives JPEG image as arrays of bytes and displays it on the screen with UI.Image
. It works fine but I am making optimization and have few questions. Currently, the code I have below converts arrays of bytes to Texture2D
then creates a Sprite
from the Texture2D
then assign that Sprite
to UI.Iamge
to display on the screen.
Texture2D camTexture;
Image screenDisplay;
public byte[] JPEG_VIDEO_STREAM;
bool updateScreen = false;
//正在初始化
JPEG_VIDEO_STREAM = new byte[20000];
camTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
//在更新功能中运行的主代码
//Main Code that runs in the Update function
if(updateScreen){
camTexture.LoadImage(JPEG_VIDEO_STREAM);
Sprite tempSprite = Sprite.Create(camTexture, new Rect(0, 0, camTexture.width, camTexture.height), Vector2.zero, 0);
screenDisplay.sprite = tempSprite;
updateScreen = false;
}
上面的代码当前仅执行3个步骤即可将图像显示到屏幕上.
The code above currently perform 3 steps just to display image to screen.
byte array
-> Texture2D
-> Sprite
-> UI.Image
.
但是我希望它看起来像byte array
-> Texture2D
-> UI.Image
.
but I want it to look like byte array
-> Texture2D
-> UI.Image
.
我想直接将Texture2D
写到UI.Image
而无需创建新Sprite ,因为我相信Sprite.Create(camTexture, new Rect(0, 0, camTexture.width, camTexture.height), Vector2.zero, 0);
分配新内存每次Sprite.Create
调用.我看了看Unity文档,找不到其他方法.
I want to write Texture2D
directly to UI.Image
without creating new Sprite because I believe that Sprite.Create(camTexture, new Rect(0, 0, camTexture.width, camTexture.height), Vector2.zero, 0);
allocates new memory each time Sprite.Create
called. I looked at the Unity Documentation and couldn't find any other way to do this.
我的问题是:
-
如何在不首先将
camTexture(Texture2D)
转换为Sprite
的情况下将camTexture(Texture2D)
分配到屏幕screenDisplay(UI.Image)
?
How can I assign
camTexture(Texture2D)
to the screenscreenDisplay(UI.Image)
without convertingcamTexture(Texture2D)
toSprite
first?
Sprite.Create
调用时是否分配新的内存?
Does Sprite.Create
allocate new memory when called?
如果对此有一个解决方案,那么该解决方案是否比我目前在性能和内存管理?
If there is a solution to this, is that solution better than what I currently have in terms of performance and memory management?
注意:我没有没有计划使用OnGUI
绘制Texture2D
.我想用新的Unity UI
来做到这一点.谢谢.
Note: I have no plans on using OnGUI
to draw Texture2D
. I want to do this with the new Unity UI
. Thanks.
修改: 有了Joe对RawImage的回答,最终代码如下:
With Joe's answer of RawImage, the final code looks like this:
RawImage screenDisplay;
if(updateScreen){
camTexture.LoadImage(JPEG_VIDEO_STREAM);
screenDisplay.texture = camTexture;
updateScreen = false;
}
不再需要Sprite.
No more Sprite needed.
推荐答案
我认为,通过专门使用RawImage
而不是Image
,可以做到这一点.
I think that by specifically using a RawImage
rather than Image
, one can do this.
我广泛使用RawImage
,因为我们必须显示PNG",而且更容易.
I use RawImage
extensively, because, we have to "display PNGs" and it's easier.
考虑非常方便的把戏:
仅 以 开头,您已经导入了一个普通的灰色PNG.. 然后对其进行修改 .而不是尝试从头开始构建?
just start with a trivial gray PNG which you have imported .. and then modify that .. rather than try to build from scratch?
我发现一个有趣的好奇心是:通常要镜像图像,您只需将x或y缩放为-1.除非已修复,否则Unity会出现一个问题,该问题不适用于RawImage
.
An interesting curiosity I found is: normally to mirror an image, you just simply scale of x or y to -1. Unless it's been fixed, Unity has a problem where this won't work for RawImage
.
// currently in Unity, the ONLY way to mirror a RAW image is by fooling with
// the uvRect. changing the scale is completely broken.
if ( shouldWeMirror )
rawImage.uvRect = new Rect(1,0,-1,1); // means mirror
else
rawImage.uvRect = new Rect(0,0,1,1); // means no flip
另一个有趣的因素.因此,许多Unity项目仍使用(甚至是2017年)最高级的 2dToolkit .它可以立即解决诸如此类的问题.
Another interesting factor. For this reason, many Unity projects still use (even 2017) the superlative 2dToolkit. It instantly solves issues such as this.
这篇关于从Texture2D创建UI图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!