Accessing global fog properties
All properties shown in the inspector can be set or modified at runtime using scripting.
First you need to get a reference to the main script which can be done easily using:
using DynamicFogAndMist2;
…
DynamicFog fog = dynamicFogVolume.GetComponent<DynamicFog>;
(where dynamicFogVolume is the gameobject of the fog volume)
Then, you can set any property like the fog color or wind speed/direction:
fog.profile.color = new Color(1,0,0);
fog.profile.windDirection = Vector3.right;
etc.
Creating fog volumes at runtime
Use this code to create a new fog volume using code:
GameObject fogVolume = DynamicFogManager.CreateFogVolume(“New Fog Volume”);
Fog of War methods
The following fog of war methods are available:
fogOfWarTexture
Property that returns or set the fog of war texture.
ResetFogOfWar();
General reset function which removes any previous change to fog of war transparency.
ResetFogOfWarAlpha(…)
Restores fog immediately at a given position with radius or bounds.
GetFogOfWarAlpha(…)
Returns the opacity of the fog at a given world position.
SetFogOfWarAlpha(…)
Changes the transparency of the fog in an area. This method is overloaded and can accept different paremeters and options:
-
A world position and a radius, or a bounds object or a world position and size.
-
Desired alpha and specify if this value replaces the existing transparency or if it blends with it (blendAlpha parameter).
-
The duration of the transition (pass 0 to set the alpha immediately).
-
The smoothness/harshness of the border (by default it uses the global setting in Fog Of War section which is 1f).
-
The restore delay and duration (by default it uses the global settings in Fog Of War section). Pass 0 to restore delay to make the change permanent.
In Unity’s Universal Render Pipeline (URP), to properly enable or disable the “Fog of War” at runtime, you must call the
UpdateMaterialProperties()
method after changing the value. This ensures the changes are reflected both in the Inspector and in the game.