Game.UI.Tooltip.TooltipSystemBase
Assembly:
Assembly-CSharp
Namespace:
Game.UI.Tooltip
Type:
abstract class
Base:
GameSystemBase
Summary:
TooltipSystemBase is an abstract base system used to register and manage tooltip groups and mouse tooltips with the game's TooltipUISystem. It acquires a reference to the global TooltipUISystem during creation and provides helper methods for adding tooltip groups and mouse-only tooltips while guarding against duplicate tooltip path registrations. It also contains a utility to convert world-space positions to UI tooltip screen coordinates (and determine whether the position is on-screen). The class uses Unity's Camera.main and Screen dimensions and relies on Unity.Mathematics for 2D vector types. The class methods are intended to be used/overridden by derived systems that register and manage tooltips.
Fields
private TooltipUISystem m_TooltipUISystem
Holds a reference to the game's TooltipUISystem retrieved from the World in OnCreate. This reference is used by AddGroup and AddMouseTooltip to register tooltip groups and mouse tooltips. It is private and managed by the base system.
Properties
- (none)
Constructors
public TooltipSystemBase()
Default protected constructor (marked with [Preserve] in source). Exists to allow subclassing and to prevent stripping by the Unity build process when the Preserve attribute is present on the class constructor.
Methods
protected override void OnCreate() : System.Void
Called by the game/system framework when this system is created. The base implementation:- Calls base.OnCreate().
- Retrieves or creates the TooltipUISystem instance via
base.World.GetOrCreateSystemManaged<TooltipUISystem>()
and stores it inm_TooltipUISystem
. -
The method is marked with [Preserve] in the source to avoid code stripping.
-
protected void AddGroup(TooltipGroup group) : System.Void
Registers a TooltipGroup with the TooltipUISystem. Behavior: - If
group.path
equalsPathSegment.Empty
, the group is still added (no duplicate-path check for empty path beyond the equality check used). - If another group already has the same
path
, logs an error viaUnityEngine.Debug.LogError
and does not add the duplicate. - Otherwise adds the group to
m_TooltipUISystem.groups
. -
Useful for organizing tooltips into named groups for the UI.
-
protected void AddMouseTooltip(IWidget tooltip) : System.Void
Registers a mouse-only tooltip (widget) to the TooltipUISystem's mouse group. Behavior: - If
tooltip.path
equalsPathSegment.Empty
, it is treated the same as any other path for the duplicate check logic. - If the mouse group's children already contain a widget with the same
path
, logs an error and does not add the duplicate. - Otherwise adds the tooltip to
m_TooltipUISystem.mouseGroup.children
. -
Intended for tooltips that should follow the mouse cursor.
-
protected static float2 WorldToTooltipPos(UnityEngine.Vector3 worldPos, out bool onScreen) : Unity.Mathematics.float2
Utility that converts a 3D world position into UI tooltip coordinates: - Uses
Camera.main.WorldToScreenPoint(worldPos)
to get screen space coordinates. - Converts the resulting Vector3 to float2 (.xy) and flips the Y coordinate to match UI coordinate space by
xy.y = Screen.height - xy.y
. - Sets
onScreen
to true if the computed XY is within [0, Screen.width] x [0, Screen.height], otherwise false. - Returns the computed UI coordinate as a
float2
. - Note: relies on
Camera.main
being valid and on the current Screen.width/height; callers should handle possible null Camera.main.
Usage Example
using UnityEngine;
using UnityEngine.Scripting;
using Unity.Mathematics;
public class MyTooltipRegistrar : TooltipSystemBase
{
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Example: create and register a tooltip group (pseudo code)
var myGroup = new TooltipGroup
{
// set path, label, etc.
};
AddGroup(myGroup);
// Example: create and register a mouse-following widget (pseudo code)
IWidget mouseWidget = /* create widget */;
AddMouseTooltip(mouseWidget);
}
private void UpdateTooltipPosition(Vector3 worldPos)
{
bool onScreen;
float2 uiPos = WorldToTooltipPos(worldPos, out onScreen);
if (onScreen)
{
// Position your tooltip UI element at uiPos
}
else
{
// Hide or reposition tooltip
}
}
}
Notes and tips: - The class uses [Preserve] on members to reduce the chance of Unity's code stripping removing constructors or methods used via reflection or by the system framework. - Duplicate path detection relies on equality of PathSegment; ensure tooltip/widget PathSegment values are unique where required. - WorldToTooltipPos flips the Y coordinate to match typical UI coordinate space; this is necessary when converting from Unity's screen coordinates to a UI system that uses top-left origin. Make sure your UI coordinate system matches this convention.