- Home /
How do I blit a Texture to a specified display?
I'm working on something where, for the time being, I'm having to use a Canvas, RawImage and RenderTexture to effectively blit an input Texture to a specific display. The script that drives this looks like the following;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Canvas))]
[RequireComponent(typeof(RawImage))]
public sealed class BlittableCanvas : MonoBehaviour
{
private Canvas canvasComponent = null;
private RawImage rawImageComponent = null;
private RenderTexture renderTexture = null;
public void Setup(int displayIndex)
{
this.rawImageComponent.texture = null;
if (this.renderTexture != null)
{
this.renderTexture.Release();
this.renderTexture = null;
}
Display display = DisplayUtils.GetDisplay(displayIndex, true);
if (display == null)
{
this.canvasComponent.targetDisplay = 0;
this.rawImageComponent.color = new Color(0.0f, 0.0f, 0.0f, 0.0f);
}
else
{
this.canvasComponent.targetDisplay = displayIndex;
this.renderTexture = new RenderTexture(
display.renderingWidth, display.renderingHeight, 0, RenderTextureFormat.Default);
this.rawImageComponent.texture = this.renderTexture;
this.rawImageComponent.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
}
}
public void Blit(Texture texture, Material material)
{
if (this.renderTexture == null) return;
if (texture == null) return;
if (material == null) return;
Graphics.Blit(texture, this.renderTexture, material);
}
private void Awake()
{
this.canvasComponent = this.GetComponent<Canvas>();
this.rawImageComponent = this.GetComponent<RawImage>();
}
private void OnDestroy()
{
this.rawImageComponent.texture = null;
if (this.renderTexture != null)
{
this.renderTexture.Release();
}
}
}
Note that the line "Display display DisplayUtils.GetDisplay(displayIndex, true);" uses a helper method that I wrote that returns the Display with the given index, and "true" activates it if it hasn't been already. So I attach this script to a GameObject containing a Canvas and RawImage, and then call "Setup(displayIndex)" to set it up to output to the specified display. I can then use the "Blit(texture, material)" method in order to blit a texture to the display (fullscreen).
Of course, at the moment I need a GameObject with this script attached (which I have stored as a prefab) for each display in order for this to work. What I ideally want to do is to replace this prefab and script with a simple method as follows;
public void Blit(Texture texture, int displayIndex, Material material)
{
// Implementation?
}
I want to be able to do this without having to instantiate any prefabs. Does anyone have any ideas on how I could possibly do this? Note that it's mandatory that I can choose the display that I wish to render to (using "displayIndex"), as my system uses multiple displays.
Your answer
Follow this Question
Related Questions
Fullscreen on a second (non primary) monitor 1 Answer
Multidisplay: 1 camera output over 3 fixed displays 2 Answers
Possible to Output Render Texture or Camera Output to Multiple Displays? 0 Answers
Change the camera RenderTexture before rendering it to screen? 1 Answer
Where to Graphics.Blit()? 1 Answer