TransWikia.com

How do I safely change properties on my skybox?

Game Development Asked on December 15, 2021

Usually in Unity, when your scripts make changes to something at runtime, those changes are not persisted back to design-time. This is generally considered a good thing. But for whatever bizarre technical reason, this does not apply to shader properties on materials. If you change something while the game is running, it stays changed!

Generally the way to get around this is with a MaterialPropertyBlock, which also helps avoid generating a lot of garbage. But MaterialPropertyBlocks work with Renderers, which leaves out one rather important material: the skybox.

Right now, I have code that says RenderSettings.skybox.SetFloat("_PropName", value);, and it’s incredibly aggravating because it always needs to be reset afterwards. RenderSettings doesn’t have a SkyboxRenderer property that I can use in conjunction with a MaterialPropertyBlock, so how can I change shader settings on my skybox and make them not persist after I exit play mode?

One Answer

Presumably you considered doing something like...

Material originalSkybox = RenderSettings.skybox;
Material runtimeSkybox = Instantiate(originalSkybox);
RenderSettings.skybox = runtimeSkybox;

runtimeSkybox.SetFloat(...

...and destroying runtimeSkybox when your game ends?

Answered by DMGregory on December 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP