I need to put meshes on my UI, and I have it working using ‘screen space - camera’ on my Canvas. All my meshes are in the right place, but the meshes won’t respect masking for scrollable windows. I think I need to use a render texture to make that work perhaps.
So these game objects are made entirely with script. They are static and don’t animate. To get a texture from them, do I create a camera on the fly and call camera.Render()? Do I make the object a child of that camera? I’m not used to thinking about cameras as I usually just use the main camera and not think about it.
If someday I do want these objects to animate, would I then want a separate camera for each object? I’m having trouble conceptualizing what is needed.
It’s remarkably flexible.
You can use your current Camera, add a RenderTexture, take a .Render(), remove the RenderTexture and off you go.
Or you can AddComponent() a fresh one, use that, then Destroy() it…
You can new up a RenderTexture, or just create one on disk to use. Depends if you’re plumbing stuff into a scene in advance, or just doing it on the fly.
Think of it like a video camera in another room (eg, off-screen). Attached is an example of a RenderTexture taping a small stage of acting objects spinning around and presenting it both as a Sprite and as a piece of UI.
Mess with it, you’ll quickly get the feel for it.
7844700–994638–RenderTextureSetup.unitypackage (41.2 KB)
1 Like
Thanks. I have a new problem. I was able to set up a little Camera and RawImage looking at the same render texture. I can tell the camera is drawing on the texture because I can see the background color of the camera on the RawImage.
However, I can’t for the life of me see my game object with the new camera. There’s a million reasons why this could be, but I can still move the mesh around and see it with the main camera, but I can’t get it into my viewport with the sub camera. My mesh is set to a position of 0,0,0, so it should be visible by any camera right?
I tried to use layers to filter this object from each camera, but I was only able to make the mesh disappear in the main camera, I can’t get it to appear with the sub camera.
How can I eliminate the possible reasons for this problem?
Things to check:
- where is the sub camera
- what is the sub camera’s near / far clip
- did you select / clear any layers in the camera
If that doesn’t fix it, delete it all and start over.
1 Like
Ok, I got it working. The complications were as follows. Because my on the fly camera was deep in the main canvas heirarchy, this threw off the scaling and position in mysterious ways. Also, because my mesh collection object was a child of the camera, setting the z component of the camera position back a little as normal kept the object in sync with that and kept it hidden. After solving that, I had to scale my game object to 90. I also had to scale the z coordinate to 90 also in this case so that the depth buffer could see the different orthographic mesh layers on the object.
For some reason it makes sense to me to put these objects in the normal UI hierarchy, but it’s probably better to just have the RawImage in there and keep the rest top level.
1 Like
This can run into issues, as you found. The reason is that anything in the UI is in a magical new coordinate space dictated by the Canvas and CanvasScaler, and that coordinate space may be wildly different from your “normal” workaday Camera setup and the rest of your game.
Glad you’re operational again!