简体   繁体   English

从Texture2D创建UI图像

[英]Creating UI Image from Texture2D

I working on a live stream App that receives JPEG image as arrays of bytes and displays it on the screen with UI.Image . 我正在开发一个实时流应用程序,它接收JPEG图像作为字节数组,并使用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然后从Texture2D 创建一个Sprite ,然后将该Sprite分配给UI.Iamge以在屏幕上显示。

Texture2D camTexture;
Image screenDisplay;
public byte[] JPEG_VIDEO_STREAM;
bool updateScreen = false;

//Initializing //初始化

JPEG_VIDEO_STREAM = new byte[20000];
camTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);

//Main Code that runs in the Update function //在Update函数中运行的主代码

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;
}

The code above currently perform 3 steps just to display image to screen. 上面的代码目前只执行3个步骤来显示图像到屏幕。

byte array -> Texture2D -> Sprite -> UI.Image . byte array - > Texture2D - > Sprite - > UI.Image

but I want it to look like byte array -> Texture2D -> UI.Image . 但我希望它看起来像byte array - > Texture2D - > UI.Image

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); 我想直接将Texture2D写入UI.Image 而不创建新的Sprite,因为我相信Sprite.Create(camTexture, new Rect(0, 0, camTexture.width, camTexture.height), Vector2.zero, 0); allocates new memory each time Sprite.Create called. 每次调用Sprite.CreateSprite.Create 分配新的内存。 I looked at the Unity Documentation and couldn't find any other way to do this. 我查看了Unity文档,找不到任何其他方法来执行此操作。

My questions are: 我的问题是:

  1. How can I assign camTexture(Texture2D) to the screen screenDisplay(UI.Image) without converting camTexture(Texture2D) to Sprite first? 我该如何分配camTexture(Texture2D)屏幕screenDisplay(UI.Image)无需转换camTexture(Texture2D)Sprite第一?

  2. Does Sprite.Create allocate new memory when called? Sprite.Create在调用时是否分配新内存?

  3. If there is a solution to this, is that solution better than what I currently have in terms of performance and memory management ? 如果有解决方案 ,那么该解决方案在性能内存管理方面是否优于目前的解决方案?

Note: I have no plans on using OnGUI to draw Texture2D . 注意:我没有计划使用OnGUI绘制Texture2D I want to do this with the new Unity UI . 我想用新的Unity UI做到这一点。 Thanks. 谢谢。

Edit: With Joe's answer of RawImage, the final code looks like this: 编辑:使用Joe对RawImage的回答,最终代码如下所示:

RawImage screenDisplay;
if(updateScreen){
camTexture.LoadImage(JPEG_VIDEO_STREAM);
screenDisplay.texture = camTexture;
updateScreen = false;
}

No more Sprite needed. 不再需要Sprite。

I think that by specifically using a RawImage rather than Image , one can do this. 认为通过专门使用RawImage而不是Image ,可以做到这一点。

I use RawImage extensively, because, we have to "display PNGs" and it's easier. 我广泛使用RawImage ,因为我们必须“显示PNG”并且它更容易。

Consider the very handy trick: 考虑一个非常方便的技巧:

just start with a trivial gray PNG which you have imported .. and then modify that .. rather than try to build from scratch? 开始使用你导入的一个简单的灰色PNG .. 然后修改它 ...而不是尝试从头开始构建?

An interesting curiosity I found is: normally to mirror an image, you just simply scale of x or y to -1. 我发现一个有趣的好奇心是:通常镜像一个图像,你只需将x或y的比例缩放到-1。 Unless it's been fixed, Unity has a problem where this won't work for RawImage . 除非它已被修复,否则Unity会遇到一个问题,即这对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

Another interesting factor. 另一个有趣的因素 For this reason, many Unity projects still use (even 2017) the superlative 2dToolkit . 出于这个原因,许多Unity项目仍然使用(甚至2017年)最高级的2dToolkit It instantly solves issues such as this. 它立即解决了这样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM