- Home /
How can I build a RenderTexture to Texture2D hidden booth for UI elements?
Hello guys,
I am having a bit of mixed issue that involves RenderTexture, UIs and Coroutines.
Premise : I have a little panel that I need to display several times in a scrollview, each time initialized with different data. The panel because of lots of elements and text, has a quite high number of drawcalls (20-30) that I can't really minimize much. As I need to display up to 30-40 of those elements, displaying it right away into a scrollview is obviously a no go. The panels don't have interactive components beside being clickable, everything is mainly there for information (and picking the chosen element), so I have no need to keep their inner UI elements separated.
My idea : Setting up a sort of out of screen rendering booth where, for each available element (they are Officers from the game) :
I initialize the panel with the new data
I render it to render texture
I render it to render texture
I save the render texture to Texture2d
I assign the Texture2d to a RawImage element
I display that on the scrollview
Move on to the next one
Being for sure a resource consuming activity, I definitely need to resort on Coroutines, but that is causing me some issues in getting it working properly, as I can't use the classic Update loop and I need therefore to force the waiting times to be minimal but necessary for the panel to be filled up and the rendering to be done before saving the texture and then assigning it.
This is the code this far, where it fails is at the fact that for example testing with 3 elements, they all get rendered with the look of the last of them (I also tried checking if by some mistake they all get the same Texture2d, but they all seem to have different instances IDs, beside the fact of looking the same (because most probably all produced after the last element is filled up)
This is the "rendering function" :
public RenderTexture myRenderableTexture;
public OfficerPreviewElement targetOfficer;
public Texture2D toStore;
public IEnumerator GenerateOfficerTexture(Officer off, int displayMode, System.Action<Texture2D> result) {
toStore = null;
targetOfficer.Init(off, displayMode); //init the panel
yield return new WaitForEndOfFrame(); //wait for end of frame when UIs should have been rendered but not displayed (?)
RenderTexture.active = myRenderableTexture;
toStore = new Texture2D(myRenderableTexture.width, myRenderableTexture.height);
toStore.ReadPixels(new Rect(0, 0, toStore.width, toStore.height), 0, 0);
toStore.Apply();
result(toStore); // Pass retrieved result.
}
And this is where I call it (later when it works I will use pooling instead of instantiating the new element to display)
public IEnumerator UpdateOfficers() {
yield return null;
foreach (int off in GameDataManager._gm.GetPlayerOfficers()) {
StartCoroutine(AddOfficer(off));
}
StartCoroutine(UpdatePanel());
}
public IEnumerator AddOfficer(int off) {
Texture2D texture;
yield return StartCoroutine(RenderBooth._booth.GenerateOfficerTexture(GameDataManager._gm.GetOfficer(off), displayMode, value => texture = value));
GameObject newOff = Instantiate(officerPrefab);
newOff.transform.SetParent(contentPanel);
newOff.GetComponent<OfficerPreviewPlaceholder>().Init(off, texture, displayMode);
newOff.transform.localScale = new Vector3(1.1f, 1.1f, 1.1f);
}
public IEnumerator UpdatePanel() {
yield return new WaitForSeconds(0.1f);
hlg.enabled = false;
hlg.enabled = true;
}
Now, any idea on how I might solve that problem would be supremely appreciated. I think if I get it working, I found a pretty decent workaround to some heavy performance issues of Unity UI in my specific case. Cheers, H
Well, one major optimization: RenderTexture inherits from Texture. So, in most cases, you can use the RenderTexture directly ins$$anonymous$$d of copying it to a Texture2D. If you store these as an array of RenderTextures and apply them directly to the buttons (especially if the RenderTextures are small), you could even conceivably do it in real-time, since you wouldn't be allocating new graphics memory each time.
Hi @FortisVenaliter . Actually I managed to solve the main issue (it was just enough to add a yield return 0 in the main loop so to make sure it generated one element per frame only).
About your idea, i dig the skipping the passage of converting to Texture2d, but as I have only one camera that renders every frame a different thing to texture, by using only RenderTextures, wouldn't the visuals of each texture get lost if I detach them each frame from the camera?
Answer by drHogan · May 05, 2017 at 05:53 AM
Ok, in the end the solution was really close by, it was enough to modify this way the main loop
public IEnumerator UpdateOfficers() {
yield return null;
foreach (int off in GameDataManager._gm.GetPlayerOfficers()) {
StartCoroutine(AddOfficer(off));
yield return 0; //ADDED THIS ONE so to skip a frame after each generated officer.
}
StartCoroutine(UpdatePanel());
}
I think it is definitely prone to more optimizations, but it works fairly well. The pain in terms of drawcalls now is the peak when the scrollview gets redrawn and resized with each new officer added, I will be looking for a solution for that.