Skip to content

Game.Tools.ToolRaycastSystem

Assembly: Assembly-CSharp
Namespace: Game.Tools

Type: class

Base: GameSystemBase

Summary:
ToolRaycastSystem is a game system that prepares and submits raycast inputs each frame for the currently active tool and camera viewer. It collects state from ToolSystem, CameraUpdateSystem and InputManager, composes a RaycastInput (line, offsets, masks and flags) and enqueues it into RaycastSystem. It also exposes a helper to read back the first raycast result produced for this system. This system is intended for use by tools that need world-raycasting (mouse picking, UI/world toggles, tool-specific filtering) in Cities: Skylines 2 modding.


Fields

  • private ToolSystem m_ToolSystem
    Holds a reference to the global ToolSystem. Used to query the active tool and whether a full update is required.

  • private RaycastSystem m_RaycastSystem
    Reference to the shared RaycastSystem where RaycastInput values are submitted and from which results are later read.

  • private CameraUpdateSystem m_CameraUpdateSystem
    Reference to the CameraUpdateSystem used to obtain the current viewer (camera) for generating the raycast line.

Properties

  • public RaycastFlags raycastFlags { get; set; }
    Flags controlling raycast behavior (example: whether UI is ignored, tool-disabling, etc.). These flags are combined/modified each frame before building the RaycastInput.

  • public TypeMask typeMask { get; set; }
    Mask that restricts which object types the raycast should consider (building, prop, tree, etc.).

  • public CollisionMask collisionMask { get; set; }
    Mask that restricts collision layers used by the raycast.

  • public Layer netLayerMask { get; set; }
    Network layers (roads, rails, etc.) filter for the raycast.

  • public AreaTypeMask areaTypeMask { get; set; }
    Filter for area types (zoning/area-based filters) for the raycast.

  • public RouteType routeType { get; set; }
    Route type filter (used when raycasting routes/paths).

  • public TransportType transportType { get; set; }
    Transport type filter for the raycast.

  • public IconLayerMask iconLayerMask { get; set; }
    Filter for icon layers when raycasting UI/overlay icons.

  • public UtilityTypes utilityTypeMask { get; set; }
    Filter for utility types in raycasts.

  • public float3 rayOffset { get; set; }
    Offset applied to the raycast line start/end (world-space offset applied to the computed line).

Constructors

  • public ToolRaycastSystem()
    Default constructor (kept for ECS/system registration). No custom initialization logic here; initialization happens in OnCreate.

Methods

  • protected override void OnCreate()
    Called when the system is created. Retrieves/initializes references to the ToolSystem, RaycastSystem and CameraUpdateSystem via World.GetOrCreateSystemManaged(). No other state is set here.

  • public bool GetRaycastResult(out RaycastResult result)
    Fetches raycast results previously produced by RaycastSystem for this owning system. Internally calls m_RaycastSystem.GetResult(this) which returns a NativeArray. If the array is non-empty, it returns the first result and returns true if result.m_Owner is not Entity.Null (indicating a valid hit owned by some entity). If no results are available, returns false and sets result to default.

  • public static Line3.Segment CalculateRaycastLine(Camera mainCamera)
    Utility to compute a ray segment from the current mouse position using the provided camera. It:

  • Converts InputManager.instance.mousePosition to a Ray via Camera.ScreenPointToRay.
  • Uses the ray.direction and the camera.forward vector to compute a clamped projection distance to the camera far plane. The dot product between the ray direction and camera forward is clamped to [0.25, 1.0] to avoid extremely shallow rays extending too far.
  • Returns a Line3.Segment with a set to the ray origin and b to the computed end point on the far plane.

  • protected override void OnUpdate()
    Per-frame logic:

  • If ToolSystem.activeTool != null, calls activeTool.InitializeRaycast() to let the tool prepare any tool-specific raycast state.
  • Attempts to get the current viewer from CameraUpdateSystem.TryGetViewer(out viewer). If a viewer is available:
    • Modifies raycastFlags: if ToolSystem.fullUpdateRequired is true it sets RaycastFlags.ToolDisable; otherwise clears it. It also sets or clears RaycastFlags.UIDisable depending on InputManager.instance.controlOverWorld (if the player is controlling the world, UI is enabled/disabled appropriately).
    • Builds a RaycastInput populated with: the line from CalculateRaycastLine(viewer.camera), rayOffset, the various masks and flags currently set on this system (typeMask, collisionMask, netLayerMask, areaTypeMask, routeType, transportType, iconLayerMask, utilityTypeMask).
    • Submits the input to the RaycastSystem with m_RaycastSystem.AddInput(this, input). The system instance is used as the owner so results can later be fetched by GetRaycastResult.

Notes: - This system does not itself consume the RaycastResult beyond providing GetRaycastResult; tools or other systems should call GetRaycastResult to obtain hits. - Raycast flags and masks should be configured by tools or initialization code to suit the tool's picking needs.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_Stopwatch = new Stopwatch();
}

(Example above shows how systems generally use OnCreate; for ToolRaycastSystem you typically let the system run and set properties such as typeMask and rayOffset from your tool, then call GetRaycastResult each frame or in a later stage to consume results.)