Creating UI Image from Texture2D
<p>I working on a live stream App that receives <strong>JPEG</strong> image as <strong>arrays of bytes</strong> and <strong>displays</strong> it on the <strong>screen</strong> with <code>UI.Image</code>. It <strong>works</strong> fine but I am making <strong>optimization</strong> and have <strong>few questions</strong>. Currently, the code I have below converts <strong>arrays of bytes</strong> to <code>Texture2D</code> then <strong>creates</strong> a <code>Sprite</code> from the <code>Texture2D</code> then assign that <code>Sprite</code> to <code>UI.Iamge</code> to display on the screen.</p>

<pre><code>Texture2D camTexture;
Image screenDisplay;
public byte[] JPEG_VIDEO_STREAM;
bool updateScreen = false;
</code></pre>

<p>//Initializing</p>

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

<p>//Main Code that runs in the Update function</p>

<pre><code>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;
}
</code></pre>

<p>The code above currently perform 3 steps just to display image to screen.</p>

<p><code>byte array</code> <strong>-></strong> <code>Texture2D</code> <strong>-></strong> <code>Sprite</code> <strong>-></strong> <code>UI.Image</code>.</p>

<p>but I want it to look like <code>byte array</code> <strong>-></strong> <code>Texture2D</code><strong>-></strong> <code>UI.Image</code>.</p>

<p>I want to write <code>Texture2D</code> directly to <code>UI.Image</code> <strong>without</strong> creating <strong>new Sprite</strong> because I believe that <code>Sprite.Create(camTexture, new Rect(0, 0, camTexture.width, camTexture.height), Vector2.zero, 0);</code> <strong>allocates</strong> new memory each time <code>Sprite.Create</code> called. I looked at the Unity Documentation and couldn't find any other way to do this.</p>

<p>My questions are:</p>

<ol>
<li><p>How can I assign <code>camTexture(Texture2D)</code> to the screen <code>screenDisplay(UI.Image)</code> without converting <code>camTexture(Texture2D)</code> to <code>Sprite</code> first?</p></li>
<li><p>Does <code>Sprite.Create</code> allocate new memory when called?</p></li>
<li><p>If there is a <strong>solution</strong> to this, is that solution <strong>better</strong> than what I currently have in terms of <strong>performance</strong> and <strong>memory management</strong>?</p></li>
</ol>

<p>Note: I have <strong>no</strong> plans on using <code>OnGUI</code> to draw <code>Texture2D</code>. I want to do this with the new Unity <code>UI</code>. Thanks.</p>

<p><strong>Edit:</strong>
With Joe's answer of RawImage, the final code looks like this:</p>

<pre><code>RawImage screenDisplay;
if(updateScreen){
camTexture.LoadImage(JPEG_VIDEO_STREAM);
screenDisplay.texture = camTexture;
updateScreen = false;
}
</code></pre>

<p>No more Sprite needed.</p>