API Reference
Namespaces
| Namespace | Contents |
|---|---|
Hexora.LightweightBrowser | Core API — IWebView, LightweightBrowser, WebViewFactory, WebViewOptions, ScreenResizeHandler |
Hexora.LightweightBrowser.Bridge | Bridge system — BridgeEventBus, JavaScriptBridge, BridgeEnvelope, BridgeMessage, BridgeMessageFilter, BridgeSerializer, NativeMessageReceiver, MessageDispatcher |
Hexora.LightweightBrowser.DebugOverlay | BridgeDebugOverlay |
IWebView Interface
The primary interface for interacting with the native webview. Obtain via LightweightBrowser.WebView.
namespace Hexora.LightweightBrowser
{
public interface IWebView
{
// Properties
string CurrentUrl { get; }
string Title { get; }
bool CanGoBack { get; }
bool CanGoForward { get; }
bool IsLoading { get; }
Texture2D Texture { get; }
// Navigation
void LoadUrl(string url);
void GoBack();
void GoForward();
void Reload();
void GoHome();
// Display
void Show(Rect screenRect);
void Hide();
// JavaScript
void EvaluateJavaScript(string script, Action<string> resultCallback = null);
// Configuration
void SetDebuggingEnabled(bool enabled);
void SetZoomEnabled(bool enabled);
void SetMessageCallback(Action<string> callback);
// Input (desktop only)
void SendMouseEvent(int x, int y, int button, int type);
void SendMouseWheel(int x, int y, int delta);
void SendKeyEvent(int vkCode, int type);
void SendCharEvent(char c);
// Developer tools
void OpenDevTools();
// Lifecycle
void Tick();
void Destroy();
// Events
event Action<string> UrlChanged;
event Action<string> PageStarted;
event Action<string> PageFinished;
event Action<string> TitleChanged;
event Action<string, int> ErrorOccurred;
}
}
Event Descriptions
| Event | Parameter | When |
|---|---|---|
UrlChanged | New URL | URL changes for any reason (navigation, redirect, pushState) |
PageStarted | URL | A page begins loading |
PageFinished | URL | A page finishes loading (DOM ready) |
TitleChanged | New title | The document title changes |
ErrorOccurred | Error message, error code | A navigation or loading error occurs |
Input Parameters
SendMouseEvent(x, y, button, type)
| Parameter | Values |
|---|---|
x, y | Pixel coordinates relative to webview top-left |
button | 0 = left, 1 = right, 2 = middle |
type | 0 = move, 1 = down, 2 = up |
SendMouseWheel(x, y, delta)
| Parameter | Values |
|---|---|
delta | Positive = scroll up, negative = scroll down. Typically ±120 per tick. |
SendKeyEvent(vkCode, type)
| Parameter | Values |
|---|---|
vkCode | Windows virtual key code (e.g. 0x0D = Enter, 0x08 = Backspace) |
type | 0 = key down, 1 = key up |
LightweightBrowser (MonoBehaviour)
The main Unity component. Add via Add Component → Hexora → Lightweight Browser.
Public Properties
public IWebView WebView { get; } // The native webview
public BridgeEventBus Bridge { get; } // Event bus (null if bridge disabled)
public JavaScriptBridge JavaScript { get; } // JS bridge (null if bridge disabled)
public string HomeUrl { get; set; }
public Rect InitialRect { get; } // Current pixel rect
public Rect NormalizedRect { get; } // Original config rect
Public Methods
public void LoadUrl(string url); // Navigate to URL
public void GoBack(); // History back
public void GoForward(); // History forward
public void Reload(); // Reload page
public void GoHome(); // Navigate to HomeUrl
public void OpenDevTools(); // Open developer tools
public void Resize(); // Recalculate rect for current screen size
WebViewOptions
Configuration passed to WebViewFactory.Create().
[Serializable]
public class WebViewOptions
{
public string HomeUrl { get; set; } // Default: "https://www.google.com"
public bool DebuggingEnabled { get; set; } // Default: false
public bool ZoomEnabled { get; set; } // Default: false
public Rect InitialRect { get; set; } // Default: auto-calculated
}
WebViewFactory
Static factory that creates the correct platform IWebView.
public static class WebViewFactory
{
public static IWebView Create(WebViewOptions options, BridgeEventBus bridgeEventBus = null);
}
When bridgeEventBus is provided, the factory wraps the platform webview in a BridgedWebView decorator that intercepts events and publishes them through the bus.
BridgeEventBus
Central publish/subscribe hub for bridge messages.
Events
public event Action<BridgeMessage> MessageReceived;
Methods
// Subscribe with optional filter
public IDisposable Subscribe(Action<BridgeMessage> handler, BridgeMessageFilter filter = null);
// Subscribe to named message with auto-deserialized payload
public IDisposable Subscribe<TPayload>(string messageName, Action<TPayload> handler);
// Publish a message (thread-safe; marshals to main thread)
public void Publish(BridgeMessage message);
// Clear all subscriptions
public void Dispose();
Example
// Listen only to events from the webview
var sub = browser.Bridge.Subscribe(
msg => Debug.Log($"Event: {msg.Envelope.Name}"),
BridgeMessageFilter.Create().FromWebView().EventsOnly().Build()
);
// Later: unsubscribe
sub.Dispose();
JavaScriptBridge
Bidirectional C# ↔ JavaScript communication.
Methods
// Send a structured envelope to the webview
public void SendToWebView(BridgeEnvelope envelope);
// Execute raw JavaScript
public void ExecuteScript(string script, Action<string> resultCallback = null);
// Push an event to JS listeners (window.unityBridge.on)
public void PushEvent(string eventName, string data = null);
// Register a C# handler callable from JS (window.unityBridge.call)
public void RegisterHandler(string handlerName, Func<string, string> handler);
// Unregister a handler
public void UnregisterHandler(string handlerName);
Example
// Register a C# handler
browser.JavaScript.RegisterHandler("getPlayerName", data =>
{
return JsonUtility.ToJson(new { name = "Player1" });
});
// Push an event to JS
browser.JavaScript.PushEvent("score-updated", "{\"score\":42}");
BridgeEnvelope
The wire-format message structure. Immutable readonly struct.
public readonly struct BridgeEnvelope : IEquatable<BridgeEnvelope>
{
public readonly string Id; // UUID
public readonly EnvelopeType Type; // Command, Event, Call, Response, Error
public readonly string Name; // kebab-case identifier
public readonly string Payload; // JSON string or null
public readonly string CallbackId; // Correlation ID or null
}
Static Factories
BridgeEnvelope.CreateCommand(name, payload?)
BridgeEnvelope.CreateEvent(name, payload?)
BridgeEnvelope.CreateCall(handler, payload?, callbackId?)
BridgeEnvelope.CreateResponse(callbackId, payload?)
BridgeEnvelope.CreateError(name, payload)
EnvelopeType Enum
| Value | Meaning |
|---|---|
Command (0) | Instruction from C# to webview |
Event (1) | Notification from webview to C# |
Call (2) | Request from JS expecting a response |
Response (3) | Reply to a Call |
Error (4) | Error notification |
BridgeMessage
Enriched wrapper around BridgeEnvelope with metadata.
public readonly struct BridgeMessage
{
public readonly DateTime Timestamp; // UTC
public readonly MessageDirection Direction;
public readonly BridgeEnvelope Envelope;
public readonly TimeSpan Duration; // For paired request-response
}
MessageDirection Enum
| Value | Meaning |
|---|---|
UnityToWebView | C# command → native webview |
WebViewToUnity | Native webview → C# event |
UnityToJavaScript | C# → injected JS bridge |
JavaScriptToUnity | Injected JS bridge → C# |
BridgeMessageFilter
Fluent builder for filtering subscriptions.
var filter = BridgeMessageFilter.Create()
.FromWebView()
.EventsOnly()
.WithNamePattern("page-*")
.Build();
Filter Methods
| Method | Description |
|---|---|
.WithDirection(MessageDirection) | Restrict to direction |
.FromWebView() | Shorthand for WebViewToUnity |
.ToWebView() | Shorthand for UnityToWebView |
.FromJavaScript() | Shorthand for JavaScriptToUnity |
.ToJavaScript() | Shorthand for UnityToJavaScript |
.WithType(EnvelopeType) | Restrict to envelope type |
.EventsOnly() | Shorthand for Event type |
.CommandsOnly() | Shorthand for Command type |
.ErrorsOnly() | Shorthand for Error type |
.WithNamePattern(string) | Match name (supports trailing * wildcard) |
.Build() | Terminates the chain (optional) |
BridgeSerializer
Static JSON serializer for envelopes.
public static class BridgeSerializer
{
public static string Serialize(BridgeEnvelope envelope);
public static BridgeEnvelope Deserialize(string json);
}
Uses JsonUtility with camelCase property names matching the JSON wire format.
ScreenResizeHandler
Auto-detects screen resolution changes and resizes the browser.
[AddComponentMenu("Hexora/Screen Resize Handler")]
public class ScreenResizeHandler : MonoBehaviour
{
public LightweightBrowser Browser { get; set; } // Auto-detected
public float PollInterval; // Default: 0.15s
}
BridgeDebugOverlay
IMGUI-based debug panel for inspecting bridge traffic.
public class BridgeDebugOverlay : MonoBehaviour
{
public KeyCode ToggleKey; // Default: F12
public int MaxVisibleEntries; // Default: 100
public float PanelHeightRatio; // Default: 0.4
public void Initialize(BridgeEventBus eventBus);
}
- Desktop: press
F12to toggle - Mobile: triple-tap to toggle