Skip to main content

CursorManager

Important Attention Reminder

Coding Style wiki


CursorManager is the cursor manager of CursorSystem (a MonoBehaviour singleton) that manages the CursorState list configured in the Inspector and drives the frame animation of dynamic cursors in Update. Import the CursorManager Prefab via Samples and drop it into the boot scene, or let the Cursors facade create it automatically.

NamespaceOxGKit.CursorSystem
Type[DisallowMultipleComponent] public class CursorManager : MonoBehaviour
SourceCursorManager.cs
using OxGKit.CursorSystem;

Reminder The instance methods of CursorManager correspond one-to-one with the Cursors static facade (the singleton accessor is internal); route calls through Cursors — there is no need to hold an instance reference yourself.

Quick Start

using OxGKit.CursorSystem;

// The CursorManager is automatically found or created by the Cursors facade (singleton)
Cursors.InitInstance();

// After authoring Cursor States in the Inspector, switch by name
Cursors.SetCursorState("Loading");

General Rules

Singleton and Lifecycle

  1. The first access through Cursors automatically finds the CursorManager in the scene; if none exists, a [CursorManager] node is created automatically.
  2. In Awake, it parents itself under the OxGKit container node and persists with DontDestroyOnLoad, and initializes the current state to the first element in the list.
  3. In Start, it resets the rendering (applies the default state); in Update, it drives the current state's dynamic cursor animation according to the Ignore Time Scale setting.
  4. OnEnable resets back to the default state; OnDisable removes the cursor rendering (restores the system cursor).

Inspector Members

MemberDescription
Ignore Time ScaleWhether the dynamic cursor animation ignores Time.timeScale (default true, driven by Time.unscaledDeltaTime).
List Cursor StatesThe CursorState list; the first entry is the default state (a built-in Default state with frameRate 30).

Methods Overview

MethodDescription
SetIgnoreScaleSets whether dynamic cursor animation ignores Time.timeScale.
GetCurrentCursorLockStateGets the current cursor lock mode.
SetCursorLockStateSets the cursor lock mode.
IsCursorVisibleChecks whether the cursor is visible.
SetCursorVisibleSets the cursor visibility.
SetScaleToAllCursorsSets the scale for all cursor states and resets the rendering.
GetAllCursorStatesGets all cursor states.
GetCursorStateGets the cursor state with the given name.
GetCurrentCursorStateGets the current cursor state.
SetCursorStateSwitches the cursor state by name.
ResetCursorStateResets back to the default state (first element).
ResetRenderResets the rendering of the current cursor state.
RemoveCursorRenderRemoves the cursor rendering (restores the system cursor).

SetIgnoreScale

public void SetIgnoreScale(bool ignore)

Sets whether the dynamic cursor animation update ignores Time.timeScale (default true, driven by Time.unscaledDeltaTime).

GetCurrentCursorLockState

public CursorLockMode GetCurrentCursorLockState()

Gets the current cursor lock mode (i.e., Cursor.lockState).

SetCursorLockState

public void SetCursorLockState(CursorLockMode cursorLockMode)

Sets the cursor lock mode (None, Locked, Confined).

IsCursorVisible

public bool IsCursorVisible()

Checks whether the cursor is currently visible (i.e., Cursor.visible).

SetCursorVisible

public void SetCursorVisible(bool visible)

Sets the cursor visibility (i.e., Cursor.visible).

SetScaleToAllCursors

public void SetScaleToAllCursors(Vector2 scale)

Sets the scale for all cursor states and resets the rendering (scaling only takes effect with scalingEnabled + ForceSoftware).

GetAllCursorStates

public CursorState[] GetAllCursorStates()

Gets all configured cursor states (CursorState).

GetCursorState

public CursorState GetCursorState(string stateName)

Gets the cursor state with the given name; returns null if not found.

GetCurrentCursorState

public CursorState GetCurrentCursorState()

Gets the current cursor state.

SetCursorState

public bool SetCursorState(string stateName)

Switches the cursor state by name and resets the rendering; returns true on success, or false if the name is not found.

ResetCursorState

public void ResetCursorState()

Resets the cursor state to the default state (first element in the list) and resets the rendering.

ResetRender

public void ResetRender()

Resets the rendering of the current cursor state.

RemoveCursorRender

public void RemoveCursorRender()

Removes the cursor rendering (clears the current state and restores the system default cursor); to restore the custom cursor rendering, call ResetCursorState to re-initialize and reset the rendering.


CursorState

Cursor state (serializable, nested in CursorManager) that defines a single cursor state's render type, textures, hotspot, and animation parameters. Author it in the Inspector, or get it via Cursors.GetCursorState and adjust it at runtime (e.g., assign textures loaded from an AssetBundle).

Type[Serializable] public class CursorState
SourceCursorManager.cs

Members

MemberDescription
public string stateNameThe state name (the key for runtime switching).
public RenderType renderTypeStatic or dynamic cursor type, default Static.
public CursorMode cursorModeCursor rendering mode (Auto / ForceSoftware), default Auto.
public bool scalingEnabledScaling toggle, default false (scaling is only supported in ForceSoftware rendering mode).
public Vector2 scaleScale factor, default (1, 1) (requires scalingEnabled to take effect).
public Vector2 hotspotThe cursor hotspot position, default (0, 0).
public Texture2D staticCursorTextureThe static cursor texture.
public List<Texture2D> dynamicCursorTexturesThe dynamic cursor frame sequence.
public bool isLoopWhether the dynamic cursor loops, default false.
public PlayMode playModeThe dynamic cursor play mode, default Normal.
public int frameRateThe dynamic cursor frame rate (FPS), field default 0 (must be greater than 0 for the animation to play).

RenderType

Defines the static or dynamic cursor type.

public enum RenderType
{
Static, // Static cursor
Dynamic // Dynamic cursor (frame animation)
}

PlayMode

Defines the animation play mode.

public enum PlayMode
{
Normal, // Forward
Reverse, // Backward
PingPong, // Forward -> backward
PingPongReverse // Backward -> forward
}

Attention With isLoop off, Normal / Reverse stop at the last frame; PingPong / PingPongReverse stop after one full round trip.

Methods Overview

MethodDescription
GetStateNameGets the state name.
SetStateNameSets the state name.
SetRenderTypeSets the render type (static/dynamic).
SetCursorModeSets the cursor rendering mode.
SetCursorScaleSets the scale factor.
SetCursorOffsetSets the cursor hotspot position.
SetStaticCursorSets the static cursor texture and applies the render immediately.
SetDynamicCursorReplaces the dynamic cursor frame sequence and resets the animation.
SetFrameRateSets the frame rate.
SetLoopSets the loop toggle.
ResetRenderResets the render according to the render type.
DriveUpdateDrives the dynamic cursor animation update (called automatically by CursorManager).

GetStateName

public string GetStateName()

Gets the state name.

SetStateName

public void SetStateName(string stateName)

Sets the state name.

SetRenderType

public void SetRenderType(RenderType renderType)

Sets the render type (RenderType static/dynamic).

SetCursorMode

public void SetCursorMode(CursorMode cursorMode)

Sets the cursor rendering mode (Auto / ForceSoftware).

SetCursorScale

public void SetCursorScale(Vector2 scale)

Sets the scale factor (requires scalingEnabled and ForceSoftware mode to take effect).

SetCursorOffset

public void SetCursorOffset(Vector2 offset)

Sets the cursor hotspot position.

SetStaticCursor

public void SetStaticCursor(Texture2D t2d = null)

Sets the static cursor texture and applies the render immediately; when t2d is null, the current staticCursorTexture is re-applied directly (in ForceSoftware mode with scaling enabled, the scaled texture is applied).

// Assign a texture at runtime (e.g., loaded from an AssetBundle)
var cursorState = Cursors.GetCursorState("Default");
cursorState.SetStaticCursor(tex);

SetDynamicCursor

public void SetDynamicCursor(Texture2D[] t2ds)

Replaces the dynamic cursor frame sequence and resets the animation parameters (plays from the beginning).

// Replace the frame sequence and adjust parameters at runtime
var cursorState = Cursors.GetCursorState("Loading");
cursorState.SetDynamicCursor(texture2ds);
cursorState.SetFrameRate(12);
cursorState.SetLoop(true);
cursorState.ResetRender();

Attention SetStaticCursor / SetDynamicCursor do not change renderType; to switch the state's render type, call SetRenderType and then ResetRender.

SetFrameRate

public void SetFrameRate(int frameRate)

Sets the dynamic cursor frame rate (FPS).

SetLoop

public void SetLoop(bool isLoop)

Sets the dynamic cursor loop toggle.

ResetRender

public void ResetRender()

Resets the render according to the render type (Static re-applies the static texture; Dynamic resets the animation parameters and replays).

DriveUpdate

public void DriveUpdate(float dt)

Drives the frame-animation update of the dynamic cursor; called automatically by CursorManager's Update (normally no need to call it yourself).