A grid-based Fog of War package for Unity, implemented with the Shadowcasting algorithm.
- Unity: 2022.3 or later
- Namespace:
EunoLab.FogOfWar
Unity Package Manager → + → Install package from git URL → enter https://github.com/eunho5751/FogOfWar.git#1.0.1 → Install
Add a MainFogOfWar tag in Unity's Tags & Layers settings.
A FogOfWar instance with this tag can be accessed at runtime via FogOfWar.Main.
※ The AutoRegisterToMain option on the FogOfWarUnit component accesses the global FogOfWar instance through FogOfWar.Main, so the tag is required whenever that option is enabled.
- Create an empty GameObject and set its Tag to
MainFogOfWar. - Add the
FogOfWarcomponent to it. - The GameObject's position becomes the center of the grid. Place it to match your map. (Enable the gizmos to visualize the grid area.)
- Assign the layer of your vision-blocking colliders (walls, etc.) to Obstacle Scan Mask.
For objects that should provide vision or be hidden by the fog (players, enemies, structures, etc.):
- Add a
FogOfWarUnitcomponent (attach it to the root or topmost GameObject). - If you need show/hide behavior, add a
FogOfWarRendererSwitcherorFogOfWarGraphicSwitchercomponent.
If the Grid Data Asset field on the FogOfWar component is not assigned, obstacles are scanned automatically during initialization.
To skip the runtime obstacle scan:
- Click Save Grid in the
FogOfWarcomponent's context menu → a.bytesfile is generated. - Assign the generated
.bytesfile to theFogOfWar's Grid Data Asset field.
The quality of the fog of war depends heavily on the FogOfWar component's settings, so here are a few essential options worth understanding.
Grid Dimensions : The number of tiles. Higher values increase the resolution of the fog texture and allow the grid to cover a larger area at the same tile size.
Grid Unit Scale : The world-space size of each tile. Larger values make each tile bigger, letting the grid cover a larger area at the same fog texture resolution.
As shown above, both Grid Dimensions and Grid Unit Scale can be used to control the area covered by the grid in the world, but adjusting only one of them can cause problems.
(❌) Covering a large area by only increasing Grid Dimensions raises the fog texture resolution, which increases CPU/GPU processing cost.
(❌) Covering a large area by only increasing Grid Unit Scale means many fog texture pixels have to be rendered per single tile, which lowers the visual quality of the fog.
(✅) Adjust both settings together to find the right balance of grid size and tile size for your project. (Use the gizmos to help.)
Fog Lerp Speed : Fog interpolation speed. Higher values make the fog texture react to changes more quickly.
Fog Blur Iterations/Radius/Sigma : Settings used for the Gaussian blur, applied to soften the vision boundaries.
The main controller of the fog of war system. Handles grid generation, visibility updates, and fog texture rendering.
Main API
Activate(int? teamMask = null)/Deactivate()— turn the fog of war system ON / OFFScanGrid()— rescan obstacles in the current scene and refresh the gridAddUnit(FogOfWarUnit)/RemoveUnit(FogOfWarUnit)/ContainsUnit(FogOfWarUnit)IsVisible(Vector3 worldPos, int? teamMask = null)— whether a given world position is currently visiblestatic FogOfWar.Main— returns the instance tagged withMainFogOfWarIsActivated— current activation stateVisibilityUpdateRate— visibility refresh frequencyTeamMask— the team layer mask whose vision is rendered
Attach to objects that should provide vision or be hidden by the fog.
Main API / Events
event Action<bool> VisibilityChanged— raised when the unit's visibility changesbool IsVisible— current visibilitybool IsTeammate(int teamMask)— whether the unit belongs to the given mask
An abstract class that subscribes to a FogOfWarUnit's visibility-change event.
Overridable methods
OnAwake(),OnEnabled(),OnDisabled()OnVisibilityChanged(bool isVisible)— called when visibility changes
Example: inherit from this class to add custom behavior such as muting audio or switching AI states when a unit is hidden by fog.
(The FogOfWarRendererSwitcher and FogOfWarGraphicSwitcher components are implemented by inheriting from this class.)
Turns the Renderer component on the same GameObject ON/OFF based on visibility.
Turns a UI Graphic component (Image, Text, etc.) on the same GameObject ON/OFF based on visibility.
When applied to an int field, lets you edit teams 0–31 in the inspector as a checkbox mask.
[SerializeField, TeamMask] private int _allowedTeams;- Teams are identified by layer numbers from 0 to 31 (separate from Unity Layers).
FogOfWar.TeamMaskis a bitmask — multiple teams' vision can be displayed at the same time.FogOfWarUnit.TeamLayeris a single layer number — that team's vision clears the surrounding fog.
- Import via Unity Package Manager → Samples → Import.
- The demo is playable out of the box without additional setup. However, if you assign an obstacle layer to the
Obstaclesobjects in the demo scene and include that layer in theFogOfWarcomponent'sObstacle Scan Mask, you can see vision being blocked by the obstacles.