- Home /
Make GameObjects dynamically visible/invisible for certain cameras
I have multiple cameras (no static amount, new cameras can be created, but usually it should be less than 20), and have certain GameObjects (no fixed amount, could be 10, could be 40), I'll just call them "Toggleable GOs" here. I want to be able to make them invisible and visible dynamically (at runtime) for a certain camera without turning them visible/invisible for all cameras.
Obviously Culling Masks and Layers should come in handy, but they don't, here's the Plot Twist: You can only assign one layer to a Game Object. I can't make a game object visible on multiple layers. So I can't just have cameras that render only the default layer plus a specific layer assigned to them and then modify the Layer Mask of the "Toggleable GOs" every time I want to make them (in)visible, because in Unity there's no Layer Mask for GOs, there's only a single Layer property. Doing it the other way around (assigning "Toggleable GOs" to specific layers, and modifying the culling masks of the cams) doesn't work because of the layer limit. Keep in mind I might have more than 32 "Toggleable GOs".
maybe you could use Draw$$anonymous$$esh and draw the meshFilters shared$$anonymous$$esh. https://docs.unity3d.com/ScriptReference/Graphics.Draw$$anonymous$$esh.html
You could use replacement shaders on your cameras, and simply have a shader that doesn't render.
https://docs.unity3d.com/$$anonymous$$anual/SL-ShaderReplacement.html
Answer by YinXiaozhou · Sep 14, 2016 at 09:35 AM
Then just turning them visible/invisile for each camera. https://docs.unity3d.com/ScriptReference/Camera.OnPreRender.html
Thank you, that was exactly what I needed. It also has no mesurable effect on my framerate; Performance is still excellent, and I'm not even limited by the layer count. Btw OnPreRender actually was not working exactly as I expected, I needed OnPreCull in order for it to run correctly.
Your answer
Follow this Question
Related Questions
Same flare appearing in two diferent cameras 0 Answers
SplitScreen shooter with Viewmodels and PlayerModels without so many layers? 1 Answer
Unity - blur on one camera effects the other as well 0 Answers
How to save multiple camera views into different video files? 0 Answers
materials visible if seen by (1) all or (2) any camera(s) 0 Answers