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 /
  • Help Room /
avatar image
0
Question by Shlemon · Feb 24, 2016 at 09:28 AM · rendering

Rendering a GameObject on the same frame it is created

For my project I need to create thumbnail images of various assets. I'm doing this on demand, instantiating the asset in front of a dedicated camera, rendering the camera to a texture, and then destroying the asset instance.

I would like to do this in a single script, but it seems that gameobjects do not actually appear in the scene until after the frame they're created. If I pause the editor at the point of instantiation, nothing has been added to the hierarchy. If I then move one frame forwards, it is added. This means I render a blank texture.

Is there any way to force a region of my hierarchy to update, so I can create, render and destroy the asset all in one frame?

If not, does anyone know of a different approach?

Comment
Add comment · Show 1
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 Shlemon · Feb 24, 2016 at 12:23 PM 0
Share

Update I managed to make it work. The issue is I didn't call camera.Render(), so the camera probably contained data for the previous frame - which was blank.

2 Replies

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

Answer by Statement · Feb 24, 2016 at 09:59 AM

Should work (at least it does so on my machine). This example loads the asset called "Model" from Resources, creates a render texture and renders the model to that texture. The texture is then fed to a RawImage, for display on screen.

To set it up, ensure you have a model called "Model" (or create a cube/drag it into Resources and rename it Model). You also need a disabled camera, a location for the thumb object and a RawImage UI component to see the result.

Press R to generate the thumb texture.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class RenderThumb : MonoBehaviour
 {
     public Transform thumbLoc;
     public Camera thumbCam;
     public RawImage thumbImage;
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.R))
         {
             Destroy(thumbImage.texture);
             thumbImage.texture = MakeThumb("Model", 64, 64);
         }
     }
 
     private Texture MakeThumb(string asset, int width, int height)
     {
         // Load
         var thumbAsset = Resources.Load<GameObject>(asset);
         var thumbObj = Instantiate(thumbAsset, thumbLoc.position, thumbLoc.rotation);
 
         // Render
         thumbCam.targetTexture = new RenderTexture(width, height, 24);
         thumbCam.Render();
 
         // Unload
         Destroy(thumbObj);
         Resources.UnloadUnusedAssets();
         return thumbCam.targetTexture;
     }
 }
Comment
Add comment · Show 1 · 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
avatar image foxor · Nov 09, 2021 at 12:02 AM 0
Share

In case any future visitors are having the same problem I had, the camera must exist for a frame before the rendering can happen. The inactive camera in the scene looks like an implementation detail, but it's a technical requirement for some reason.

If you need the position of the camera to be controlled by the newly spawned object, you can move the camera to the location of a transform in the prefab.

avatar image
0

Answer by Shlemon · Feb 24, 2016 at 12:20 PM

My code follow the same structure as yours, but it doesn't work.

     private static void CreatePatchThumbnail(PatchTemplate patch) {
         // Create asset and texture
         PatchView patchView = new PatchView (patch, contentsRoot.transform);
         Texture2D thumbnail = new Texture2D (128, 128);
 
         // Render asset to texture
         camera.aspect = 1.13f;
         RenderTexture.active = camera.targetTexture;
         thumbnail.ReadPixels (new Rect(0, 0, 128, 128), 0, 0);
         thumbnail.Apply ();
         RenderTexture.active = null;
 
         // Store texture and destroy asset
         patchThumbnails [patch] = thumbnail;
         patchView.Destroy ();
     }

I have a camera with a RenderTexture set as the source, and I then use ReadPixels to extract the data to a Texture2D. Also, my asset is not a single file in memory, but rather a procedurally generated compound of multiple files - so I have more complex instantiation code.

But otherwise, our programs are functionally the same. Mine does not work, though. I can render things that have existed for at least a frame, but in the same frame as the instantiation call there is nothing on screen (or in my scene hierarchy!). If I run the debugger and step through the program, nothing is instantiated after the instantiation call. Instead, it only appears once the frame is complete.

Is there an obvious mistake I've made somewhere?

EDIT The problem was I didn't call camera.Render(), like in your example. Thanks

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with excessive shake when animating 2D content 0 Answers

Shadow rendering distance HTML5 1 Answer

How to get the low level rendering plugin working on Android? 1 Answer

Drawing multiple materials from a defined array on a single object 0 Answers

Rendering Game in 2D LAGS!!!! 0 Answers


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