Skip to content

Game.UI.GameUIResourceHandler

Assembly: Assembly-CSharp.dll
Namespace: Game.UI

Type: public class

Base: DefaultResourceHandler

Summary:
GameUIResourceHandler is a resource handler used by the game's UI system to serve dynamic/cohtml resources such as screenshots, thumbnails, and user avatars. It extends DefaultResourceHandler and uses Unity coroutines to handle requests asynchronously. The handler supports special URI schemes (screencapture://, thumbnail://, useravatar://) and integrates with the game's UserImagesManager, ThumbnailCache, PrefabSystem and platform avatar APIs to produce or fetch textures and render targets required by the UI.


Fields

  • private const string kScreencaptureScheme = "screencapture"
    Constant scheme name used to identify screencapture requests.

  • public const string kScreencaptureProtocol = "screencapture://"
    Constant protocol prefix for screencapture URIs.

  • public const string kScreenshotOpString = "Screenshot"
    Operation string used to indicate a capture operation (as opposed to simply providing a render target).

  • private const string kThumbnailScheme = "thumbnail"
    Constant scheme name used to identify thumbnail requests.

  • public const string kThumbnailProtocol = "thumbnail://"
    Constant protocol prefix for thumbnail URIs.

  • private const string kUserAvatarScheme = "useravatar"
    Constant scheme name used to identify user avatar requests.

  • public const string kUserAvatarProtocol = "useravatar://"
    Constant protocol prefix for user avatar URIs.

  • private Dictionary<string, Camera> m_HostCameraCache
    A cache mapping host identifiers (string) to Camera instances to speed up repeated camera lookups for screenshots/thumbnails. Cameras are looked up by tag (case-insensitive via ToLowerInvariant()) across all scenes if not cached.

  • protected class GameResourceRequestData : ResourceRequestData
    Nested protected helper class that extends ResourceRequestData to add convenient properties for the supported schemes:

  • IsThumbnailRequest — true if the request's scheme is "thumbnail".
  • IsScreenshotRequest — true if the request's scheme is "screencapture".
  • IsUserAvatarRequest — true if the request's scheme is "useravatar". It also exposes the standard IResourceRequest/IResourceResponse via its base.

Properties

  • (No public properties declared on this class beyond inherited ones)
    Note: The handler uses the inherited base.coroutineHost to run coroutines.

Constructors

  • public GameUIResourceHandler(MonoBehaviour coroutineHost)
    Create a new GameUIResourceHandler. The provided MonoBehaviour instance is stored as the coroutine host (base.coroutineHost) and is required for starting coroutines used to service resource requests.

Methods

  • public override void OnResourceRequest(IResourceRequest request, IResourceResponse response)
    Entry point called by the cohtml/renderer when a resource request arrives. Wraps the request/response into a GameResourceRequestData and starts the coroutine TryGetResourceRequestAsync to handle it. Exceptions are caught and result in a failure response.

  • private Camera GetCameraFromHost(string host)
    Lookup helper: returns a Camera instance whose tag (lowercased) matches the provided host string. Uses m_HostCameraCache for previously found cameras; otherwise searches all scenes and root GameObjects (includeInactive: true) and caches the result. Returns null if none found.

  • private RenderTexture SetupCameraTarget(string name, Camera camera, int width, int height)
    Ensures the camera has a RenderTexture of the requested size and name available. Releases and resizes the existing target texture and reassigns it to the camera. Returns the camera's render target after setup.

  • private IEnumerator RequestScreenshot(GameResourceRequestData requestData)
    Coroutine that services screencapture:// requests. It:

  • Adds the request to the pending list.
  • Waits until end of frame to capture (so the rendered frame is available).
  • Reads query parameters: width, height, op (operation), alloc (resource allocation type), liveView, and save preview settings.
  • Looks up the camera from the host portion of the URI.
  • Either captures an actual screenshot (if op == "Screenshot") into a user image target or sets up a render target for the camera and returns that target.
  • Sends image data back to the request (userImagesManager.GetUserImageData) and responds with success, or sets an error and marks the request failed if anything goes wrong.
  • Handles aborted requests and exceptions gracefully.

  • private IEnumerator TryGetResourceRequestAsync(GameResourceRequestData requestData)
    Main coroutine router for requests. Determines the request type and dispatches to the appropriate handler:

  • Data requests: immediate success response.
  • Screencapture requests: yield RequestScreenshot.
  • Thumbnail requests: yield TryThumbnailRequestAsync.
  • User avatar requests: yield RequestUserAvatarAsync.
  • Otherwise: yield TryPreloadedResourceRequestAsync (inherited behavior). Also logs the requested URI for debugging.

  • private bool UpdateTexture(ref Texture target, string name, (int width, int height, byte[] data) p)
    Helper for user avatars: given raw pixel data, either creates a new Texture2D (GraphicsFormat.R8G8B8A8_UNorm) or updates an existing Texture2D by loading the raw data and applying it. Returns true if the texture was created/updated successfully; false if the existing texture is not a Texture2D.

  • private IEnumerator RequestUserAvatarAsync(GameResourceRequestData requestData)
    Coroutine to fetch the platform/user avatar for the local user. Steps:

  • Adds the request to pending list.
  • Parses AvatarSize from the URI query string.
  • Calls PlatformManager.instance.GetAvatar(size) which returns a Task<(width, height, byte[])>.
  • Waits until the task completes.
  • On success, loads the raw avatar bytes into a user image target (via userImagesManager) or creates/updates a texture and responds with success.
  • On failure or if data is null, sets an error and marks the request failed.

  • private IEnumerator TryThumbnailRequestAsync(GameResourceRequestData requestData)
    Coroutine to service thumbnail:// requests. Steps:

  • Uses GameManager.instance?.thumbnailCache to obtain a ThumbnailCache.
  • Extracts width/height and the requested prefab type/name from the URI path (expected format: "/").
  • Uses the PrefabSystem to find the prefab and requests a thumbnail from the cache (thumbnailCache.GetThumbnail).
  • If the thumbnail is pending, waits until it completes (polling each frame).
  • If a thumbnail is available, extracts the texture and region and returns it through userImagesManager.GetUserImageData as an unmanaged, dynamic resource (specifying alpha premultiplication mode non-premultiplied).
  • Handles cases where thumbnails are not available, the path is invalid, or exceptions occur, by setting requestData.Error and marking the request as failed.

Notes about behavior and integration: - The handler relies on several game systems: - base.userImagesManager — to allocate/return image targets and user image data. - GameManager.instance.thumbnailCache — to obtain and wait for generated thumbnails. - PlatformManager.instance — to fetch user avatar bytes asynchronously. - World.DefaultGameObjectInjectionWorld.GetOrCreateSystemManaged() — to lookup prefabs for thumbnail generation. - The handler uses RequestData.AddPendingRequest / RemovePendingRequest / CheckForFailedRequest / RespondWithSuccess patterns (inherited) to manage request lifecycle. - Coroutines check requestData.Aborted to early-exit if the request was cancelled.

Usage Example

// Example MonoBehaviour registering/creating the handler (registration mechanism depends on host UI framework).
public class MyUIInitializer : MonoBehaviour
{
    private GameUIResourceHandler resourceHandler;

    void Awake()
    {
        // 'this' is passed so the handler can start coroutines.
        resourceHandler = new GameUIResourceHandler(this);

        // Register the handler with the UI/cohtml system as appropriate.
        // The exact registration call depends on how cohtml integration is exposed in your mod:
        // e.g. CohtmlView.RegisterResourceHandler(resourceHandler) or similar.
    }
}