Debug Tools
KenseiECS ships editor windows under the KenseiECS menu. They are Editor-only (nothing of them reaches a player build), and all but one need the KENSEI_DEBUG define.
| Menu item | Needs KENSEI_DEBUG | Shows |
|---|---|---|
| KenseiECS -> Systems | no (timings only with it) | Runner tree with enable toggles |
| KenseiECS -> World Inspector | yes | Entities, filters, pools |
| KenseiECS -> Profiler | yes | Entity lifecycle events with call stacks |
| KenseiECS -> Debug Mode | — | Toggles the define |
The World Inspector and Profiler windows are not compiled without KENSEI_DEBUG, so their menu items appear only after Debug Mode is turned on.
The windows list every live world, nothing has to be wired by hand. In the editor each World registers itself when it is created and drops out when it is destroyed, and a root SystemsRunner (one not nested in another) registers itself when its Init completes. The list holds weak references: a world that nothing else references is still garbage-collected and disappears from it, destroyed or not. Worlds exposed by a MonoBehaviour implementing IEcsWorldProvider (runners: IEcsSystemsProvider) come first, labelled with the GameObject and the provider type, ordered by world id; EcsBootstrap implements both. Worlds created in code follow as World <id>, oldest first. The windows hold what they list, and the world they show, weakly as well. The list lives only in the editor (or under KENSEI_DEBUG): a release player has none of it.
Each window shows one world at a time and has a dropdown in its toolbar to switch: the World Inspector lists worlds, the Systems window lists root runners (World <id>: <provider>, or the runner type for one initialized in code), and the Profiler shows the world whose debug names label its entities (see Profiler). The dropdown is as wide as its longest label, up to the width of the window; a label that still does not fit is cut, and the tooltip has all of it. When nothing is picked, or the picked world is destroyed, the World Inspector and the Systems window bind the first entry of the list, re-checking at most once a second in play mode, and they drop the previous session's world when play mode starts or stops. A provider that still returns a destroyed world (or a runner of one) is skipped. Outside play mode nothing is bound automatically, but the World Inspector's dropdown still lists worlds created in code (an editor tool, an Edit Mode test). The EcsEntityView inspector does not depend on the list: it shows the world its view is bound to.
With Enter Play Mode Options set to skip the domain reload, static fields survive a play session, and so does a world that one of them still references and nothing destroyed. Such a world is listed last, as World <id> (previous session) (a runner: World <id>: <type> (previous session)), and is never bound automatically, so a window never opens on it instead of the session's own world; pick it in the dropdown to see what was left behind. A world created in edit mode (an editor tool) is not marked, and a world a provider of the current scene exposes is listed with the providers. Destroy the world, or clear the static field in a [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] method, to get rid of the entry.
Changed in 2.5.0
Up to 2.4.0 the windows kept a strong reference to every world and runner they had listed, so none of them was collected while the editor ran. Without a domain reload, a world left by the previous play session was listed first among the worlds created in code and was the one the windows bound when the scene had no provider.
Debug Mode
Menu: KenseiECS -> Debug Mode — toggles the KENSEI_DEBUG scripting define.
- Unity 6 with an active Build Profile: the define goes into that profile's Scripting Defines. Other profiles are untouched, so a release profile stays clean while a dev profile keeps Debug Mode. The profile is an asset, usually under version control and shared by the team: every toggle changes it for everyone who pulls the commit. The toggle logs one line with the profile's name and asset path whenever it writes to it, so the change does not slip into a commit unnoticed. Turning the mode off and on again restores the define, but Unity may leave other serialized fields changed (seen:
m_HasScriptingDefinesgoing from0to1), so the asset is not always byte-identical afterwards; revert it in version control if that matters. - No active Build Profile, or Unity before 6: the define goes into the Player Settings defines of the active build target only. Switching to another platform does not carry the mode over; turn it on there separately.
- The checkmark shows whether the define is in effect for the active profile/target, from either the profile or Player Settings. Turning it off removes it from both, so a define that versions before 2.2 wrote to every build target does not keep the mode on silently.
- Turning it off changes only the active profile and the active target. If the Player Settings of other targets still have
KENSEI_DEBUG(versions before 2.2 wrote it to every target), a warning in the Console lists them; the toggle does not remove it there, since another target may keep it on purpose. Remove it by hand in Player Settings -> Scripting Define Symbols of each listed target. Unity may also leave an empty define entry for a target inProjectSettings.assetafter the last define is removed; it is harmless but shows up in version control. - When enabled, profiler hooks, inspector editing, the World Inspector and the Profiler window are active, together with the runtime validation listed in Release vs. KENSEI_DEBUG. When disabled, all debug overhead is stripped from compilation.
Release build guard
A player build that is not a Development Build fails in the preprocess step if its defines contain KENSEI_DEBUG: validation, debug names and timings would otherwise ship in a release player. The defines checked are those of the active Build Profile (Unity 6) plus the Player Settings of the built target, and the error says where the define was found: the profile (name and asset path) and/or the Player Settings of the target. Turn Debug Mode off, tick Development Build, or — to ship validation on purpose — add KENSEI_ALLOW_DEBUG_RELEASE to the same defines; that also turns the check off.
The preprocess step reads only the Scripting Define Symbols. It does not see defines that reach the compile another way: BuildPlayerOptions.extraScriptingDefines, Player Settings -> Additional Compiler Arguments (-define:) or an Assets/csc.rsp. For those a second check runs right after the player scripts compile: a compile with KENSEI_DEBUG from any source leaves a marker type in KenseiECS.dll, and a non-development player build stops with an error that names the source. Development builds are told apart by the build options, as in the preprocess step, not by the DEVELOPMENT_BUILD define. KENSEI_ALLOW_DEBUG_RELEASE passed the same way satisfies that check, but not the preprocess step: when KENSEI_DEBUG is in the profile or Player Settings, put the allow define there too.
Content builds are not checked. Addressables, the Scriptable Build Pipeline and legacy AssetBundles compile the player scripts too, by default without DEVELOPMENT_BUILD, but their output contains no code, so building content with Debug Mode on works (2.2.0 stopped them with a #error, including the content step of a Development player build with Addressables' default settings). The check adds a short step to every player build.
The guard is a last line of defence, not a pre-flight check. It runs early (callbackOrder -1000), but throwing does not stop the build step: preprocessors ordered before it have already run, and Unity still calls the ones after it. Whatever those change in assets (render pipeline global settings, preloaded assets and the like) can stay behind after the failed build. In CI, check the defines in the build script before BuildPipeline.BuildPlayer, from the sources the guard reads plus the ones the script passes itself:
// Unity 6 build script: call before BuildPipeline.BuildPlayer, so no preprocessor runs at all.
static void ThrowIfDebugRelease(BuildProfile profile, NamedBuildTarget target, bool development,
string[] extraScriptingDefines = null) {
if (development) {
return;
}
// Activate the profile first, as BuildPlayer(BuildPlayerWithProfileOptions) will: a profile
// with its own Player Settings makes PlayerSettings read its copy only while it is active.
BuildProfile.SetActiveBuildProfile(profile);
var defines = new List<string>(profile.scriptingDefines);
PlayerSettings.GetScriptingDefineSymbols(target, out string[] symbols);
defines.AddRange(symbols);
// Defines the script passes to the build itself (BuildPlayerOptions.extraScriptingDefines).
if (extraScriptingDefines != null) {
defines.AddRange(extraScriptingDefines);
}
// Not read here: Additional Compiler Arguments (-define:) and an Assets/csc.rsp. A release player
// compiled with KENSEI_DEBUG from those still stops, at the check after the player scripts compile.
if (defines.Contains("KENSEI_DEBUG") && !defines.Contains("KENSEI_ALLOW_DEBUG_RELEASE")) {
throw new BuildFailedException("KENSEI_DEBUG is on for a release build of " + profile.name);
}
}A script build through BuildPipeline.BuildPlayer(BuildPlayerWithProfileOptions) makes that profile active before the build, so its defines are the ones checked (and it stays active afterwards). The snippet activates the profile itself for the same reason: without that, PlayerSettings would answer for whichever profile is active at that moment (see below), and a release build could fail on a dev profile's define or pass with its own.
Profiles with their own Player Settings
A Build Profile can override Player Settings (Customize player settings in the profile). While such a profile is active, Unity redirects every PlayerSettings read and write to the copy stored in the profile asset. For KenseiECS this means:
- Turning Debug Mode off removes
KENSEI_DEBUGfrom that copy, so the profile asset changes; the toggle logs this with the profile's name and path, like its other profile writes. - The build guard and the warning about other targets read that copy too. Their messages name it ("the Player Settings stored in Build Profile '…'"), so look for the define in the profile, not in Project Settings -> Player. The warning cannot see leftovers in the project's own Player Settings (
ProjectSettings.asset) while the profile is active, and says so. - If the profile's Player Settings were copied while 2.1.x was installed, they may still contain
KENSEI_DEBUGfor every target.
Whether a profile overrides Player Settings is read through an internal Unity property; if a Unity version does not have it, the messages name both possible places.
When enabled you get:
- KenseiECS -> World Inspector — Entities tab with editable components and debug names, Filters and Pools tabs with counts, capacities and memory
- KenseiECS -> Profiler — lifecycle events with call stacks
- EcsEntityView inspector with entity navigation
- Validation — see the Release vs. KENSEI_DEBUG table
- IDE debugger view — hovering a
Worldin Rider or Visual Studio lists the alive entities with all their components (aDebuggerTypeProxy, no runtime cost)
Systems window
KenseiECS -> Systems works in any build: the runner tree with enable toggles per system and phase, plus per-system timings under KENSEI_DEBUG. It shows content only in play mode.
- Toolbar: the runner dropdown, the world's current Tick and whether the root runner is Initialized. Under
KENSEI_DEBUGa Reset peaks button clears the timings of the whole tree. In a window too narrow for one row the dropdown takes the first row and the rest the second. A narrow window also takes room from the Type column and then from the names (a cut name or type has a tooltip); the timings stay in view. - Tree: the Root runner, then every system in order. Nested runners are shown in bold with their own systems indented below; phases (
AddPhase, such as thefixedandlatephases ofEcsBootstrap) are tagged phase, inline groups (AddGroupor an unnamedAdd(runner)) are not. Each row also shows the system's type name, generic arguments included. - Toggles: run systems and nested runners have a checkbox that enables or disables them at runtime (the same as
SetActiveon the runner, see SystemsRunner). Init-only and destroy-only systems have no toggle. Disabled rows are greyed out. - Under
KENSEI_DEBUG: Last ms and Peak ms columns for every run system.
World Inspector
KenseiECS -> World Inspector (KENSEI_DEBUG only) has three tabs:
Entities
- Toolbar with the entity and pool counts, a search field and Clear. Search matches the entity index, its debug name or any of its component type names. Generic component types are named with their arguments (
EventBuffer<Hit>,Listeners<IHealthView>) here and in the other tabs, so a search forHitfinds the buffer. - Alive entities, 100 per page (< Prev / Next >). Each row shows the entity, its debug name and component count.
- Expanding an entity shows an editable Name field (the debug name; clear it to remove the name) and every component. Field values, nested structs and lists are editable in play mode.
Filters — every registered filter with its Count, Dense cap and Memory, plus the total.
Pools — every component pool, sorted by memory, with Count, Sparse cap, Dense cap, component Size and Memory. The footer sums pools + filters. Size is what Marshal.SizeOf reports, which can differ from the size in memory (a bool field counts 4 bytes instead of 1); it is managed when the component holds references and cannot be measured, and Memory then leaves the component data out.
Profiler
KenseiECS -> Profiler (KENSEI_DEBUG only) shows the events recorded by the runtime EcsProfiler. It stays empty ("Profiler is not enabled") until you enable recording from code:
#if KENSEI_DEBUG
EcsProfiler.Enable(world); // record this world only; restarts if already on
EcsProfiler.CaptureStacks = true; // opt-in, expensive
#endifEcsProfiler is compiled only under KENSEI_DEBUG, so calls to it must be wrapped in #if KENSEI_DEBUG. It has a runtime cost — use it for debugging only.
The window shows:
- Toolbar: event count, type filters All / Created / Destroyed / +Comp / -Comp / Grow, the Names dropdown, a search field and Clear, which also clears the recorded events. The three groups share one row in a wide window and wrap onto a second and a third row as it narrows, so each of them stays in view (at 430 pt: three rows).
- Names is the world whose debug names label the entities: the world being recorded, the one passed to
EcsProfiler.Enable, whatever the World Inspector shows. Another world can be picked in the dropdown; the pick lasts until recording starts for a different world. AfterEcsProfiler.Disablethe last recorded world keeps naming the events while it lives. (Up to 2.4.0 the names came from the World Inspector's world: with another world shown there the events had no names, or the name of an unrelated entity whose index and generation happened to match.) A search of digits only is an entity index and lists the events of entities whose index contains those digits (growth events have none); any other text matches the component type or the growth description (dense, a type name). Changing the search or a type filter goes back to the first page. - Event list, newest first, 200 per page: Tick, Time (ms), Type, Entity, Component (the recorded
ComponentType, see below). In a narrow window the Entity and Component columns give way; a label cut there has a tooltip. Rows are colored by event type. Entity shows the index and, when known, the generation (E(5v2)): Created and Destroyed events record it, and a component event takes it from the Created or Destroyed event of its slot around it in the buffer; one without such an event shows the index alone (E(5)). The debug name is that of the entity in the slot now, so it is shown only when that is the event's entity: once an index is reused, older events of the slot show no name. - Clicking an entity narrows the list to the history of its index: every entity that used the slot, told apart by generation. < / > step back and forward through the indices you visited; Show All returns to the full list.
- Selecting an event shows its call stack in the bottom panel. Stacks are recorded only when
EcsProfiler.CaptureStacksis on. The selection stays on that event while recording goes on; once the ring buffer drops the event, nothing is selected. - When recording stops (the world was destroyed or
EcsProfiler.Disablewas called), the window keeps showing the recorded events with a note.
Runtime API
The profiler can also be read from code, without the window:
#if KENSEI_DEBUG
EcsProfiler.MaxEvents = 50_000; // ring buffer size, 10 000 by default
// ... gameplay ...
var all = EcsProfiler.GetEvents(); // chronological
var history = EcsProfiler.GetEntityHistory(entity.Index);
var destroyed = EcsProfiler.GetEventsByType(ProfileEventType.Destroyed);
var thisTick = EcsProfiler.GetEventsByTick(world.Tick);
foreach (var evt in history) {
Console.WriteLine($"tick {evt.Tick} {evt.TimestampMs:F1} ms: {evt.Type} {evt.ComponentType}");
}
EcsProfiler.Clear();
EcsProfiler.Disable();
#endif| Member | Description |
|---|---|
Enable(world) | Start recording for one world. Events from other worlds are ignored. Clears previous events. |
Disable() | Stop recording. Also happens automatically when the profiled world is destroyed. |
IsEnabled | Whether the profiler is recording. |
CaptureStacks | Capture a call stack per event. Off by default; expensive. |
MaxEvents | Ring buffer size (10 000 by default, must be positive). The oldest events are discarded; resizing keeps the newest ones. |
GetEvents() | All recorded events in chronological order. |
GetEntityHistory(index) / GetEventsByType(type) / GetEventsByTick(tick) | Filtered copies. |
Clear() | Drop the recorded events. |
Each ProfileEvent has Tick, Type (Created, Destroyed, ComponentAdded, ComponentRemoved, StorageGrown), EntityIndex, Generation (set for Created and Destroyed, 0 for the other types), ComponentType (component events: the type's Type.Name, so EventBuffer`1 for every EventBuffer<T>; for StorageGrown, which array grew past its reserve and its sizes, or (not reserved) for a pool or filter nothing reserved, with EntityIndex -1), TimestampMs (since Enable) and CallStack. Events raised while the world suppresses its own events (World.Warmup, for example) are not recorded. In Unity, entering play mode without a domain reload resets the profiler, its events and CaptureStacks.
EcsEntityView inspector
With Debug Mode on, selecting a GameObject with an EcsEntityView (or a subclass of it) shows the view's own serialized fields and, below them, the bound entity's components in the Inspector with editable fields, nested structs and lists. Entity fields are clickable and navigate to that entity, with < Back / Forward > history and Home to return to the view's own entity.
