Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by drHogan · May 04, 2017 at 09:30 AM · uitexture2drendertexturecoroutines

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

http://www.empiresinruins.com/ScrollSample.png

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FortisVenaliter · May 04, 2017 at 08:25 PM 0
Share

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.

avatar image drHogan · May 05, 2017 at 05:51 AM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Indirect Coroutine Behaviors with UI Buttons, Animator, and SetActive 0 Answers

Copy non-rectangle part of one texture to another 2 Answers

ToTexture2D() method not working 2 Answers

Save image from camera with post processing. 0 Answers

Graphics.DrawTexture in OnPostprocessTexture 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges