Class AlacrityBrowser
- Namespace
- Alacrity
- Assembly
- Alacrity.dll
This is the public interface for Alacrity. You'll want to add this on either a UI element with a RawImage component, or any 3D renderer component. You can pass a reference to the AlacrityBrowser to other components to call public methods on it so you can control it.
public class AlacrityBrowser : MonoBehaviour
- Inheritance
-
ObjectComponentBehaviourMonoBehaviourAlacrityBrowser
- Inherited Members
-
MonoBehaviour.IsInvoking()MonoBehaviour.CancelInvoke()MonoBehaviour.StopCoroutine(Coroutine)MonoBehaviour.StopAllCoroutines()MonoBehaviour.destroyCancellationTokenMonoBehaviour.useGUILayoutMonoBehaviour.runInEditModeBehaviour.enabledBehaviour.isActiveAndEnabledComponent.GetComponent<T>()Component.TryGetComponent<T>(out T)Component.GetComponentInChildren<T>()Component.GetComponentsInChildren<T>()Component.GetComponentInParent<T>()Component.GetComponentsInParent<T>()Component.GetComponents<T>()Component.transformComponent.gameObjectComponent.tagObject.GetInstanceID()Object.GetHashCode()Object.Instantiate(Object, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion, Transform)Object.Instantiate(Object)Object.Instantiate(Object, Transform)Object.Instantiate<T>(T)Object.Instantiate<T>(T, Vector3, Quaternion)Object.Instantiate<T>(T, Vector3, Quaternion, Transform)Object.Instantiate<T>(T, Transform)Object.Destroy(Object)Object.DestroyImmediate(Object)Object.DontDestroyOnLoad(Object)Object.DestroyObject(Object)Object.FindObjectsOfType<T>()Object.FindObjectsByType<T>(FindObjectsSortMode)Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)Object.FindObjectOfType<T>()Object.FindFirstObjectByType<T>()Object.FindAnyObjectByType<T>()Object.FindFirstObjectByType<T>(FindObjectsInactive)Object.FindAnyObjectByType<T>(FindObjectsInactive)Object.ToString()Object.nameObject.hideFlags
Fields
bufferSize
This is the size of the buffer (in bytes) that is used to pass data back and forth between Unity and Javscript. This cannot be changed at runtime, so you must pick a value equal to the highest size you expect to send or receive.
[Tooltip("This is the size of the buffer (in bytes) that is used to pass data back and forth between Unity and Javscript")]
[Min(256)]
public int bufferSize
Field Value
chromiumSwitches
[Header("Chromium Command-Line Switches")]
[Tooltip("Allows you to specify command line switches to forward to the Chromium browser. See https://www.chromium.org/developers/how-tos/run-chromium-with-flags/ for information about Chromium command-line switches. Note that not all Chromium switches will always work. Do not include \"--\" or \"-\" in the name of the switch (e.g. for \"--my-switch\", use the name \"my-switch\"). Providing a value is optional. This is an advanced feature, it is unlikely you'll need to use this unless you know what you are doing.")]
public List<CommandLineSwitch> chromiumSwitches
Field Value
- List<CommandLineSwitch>
forwardKeyboardEvents
Indicates whether or not keyboard events should automatically be forwarded to the browser. If you set this to false, you can call AlacrityBrowser#GetEventSender to manually forward whatever keyboard events you would like.
[Tooltip("Indicates whether or not keyboard events should automatically be forwarded to the browser. If you set this to false, you can call AlacrityBrowser#GetEventSender to manually forward whatever keyboard events you would like.")]
public bool forwardKeyboardEvents
Field Value
forwardMouseEvents
Indicates whether or not mouse events (clicks, moves, scrolls) should automatically be forwarded to the browser. If you set this to false, you can call AlacrityBrowser#GetEventSender to manually forward whatever mouse events you would like.
[Tooltip("Indicates whether or not mouse events (clicks, moves, scrolls) should automatically be forwarded to the browser. If you set this to false, you can call AlacrityBrowser#GetEventSender to manually forward whatever mouse events you would like.")]
public bool forwardMouseEvents
Field Value
forwardResizeEvents
[Header("Event Forwarding")]
[Tooltip("Indicates whether or not window resizes should automatically be forwarded to the browser. If you set this to false, you can call AlacrityBrowser#GetEventSender to manually forward whatever resize events you would like.")]
public bool forwardResizeEvents
Field Value
framerateOnLoad
This is the initial framerate the browser will be loaded at. You can set the framerate at runtime using SetFramerate.
[Tooltip("This is the initial framerate the browser will be loaded at. You can set the framerate at runtime using SetFramerate")]
public int framerateOnLoad
Field Value
remoteDebuggingPort
The port at which remote debugging should be enabled. 0 indicates that remote debugging should be disabled. If you set this port, you can use chrome://inspect/ in a remote chromium browser to find the webpage being hosted by Alacrity and remotely inspect and debug it.
[Tooltip("The port at which remote debugging should be enabled. 0 indicates that remote debugging should be disabled. If you set this port, you can use chrome://inspect/ in a remote chromium browser to find the webpage being hosted by Alacrity and remotely inspect and debug it.")]
public int remoteDebuggingPort
Field Value
urlOnLoad
This is the initial URL the browser will be loaded with. You can load a new URL runtime using LoadUrl.
[Tooltip("This is the initial URL the browser will be loaded with. You can load a new URL runtime using LoadUrl")]
public string urlOnLoad
Field Value
Methods
AddJavascriptEventListener(Action<byte[], int>)
Add a new event listener for Javascript events. The action provided to this method will be invoked whenever
the Javascript on the currently rendered page calls sendToUnity
with an ArrayBuffer. The byte[] will be a
byte buffer containing the data sent from Javascript, and the int will be the length. Any data in the byte[]
buffer past the given length will be undefined and should not be consumed.
public void AddJavascriptEventListener(Action<byte[], int> action)
Parameters
Examples
browser.AddJavascriptEventListener((data, len) => {
Debug.Log($"Received binary data from javascript: {data[0]} {data[1]}");
});
AddJavascriptEventListener(Action<string>)
Add a new event listener for Javascript events. The action provided to this method will be invoked whenever
the Javascript on the currently rendered page calls sendToUnity
with a string, and the string message will
be propagated through IPC. Note that the bufferSize will truncate any string received if it is too small.
public void AddJavascriptEventListener(Action<string> action)
Parameters
Examples
browser.AddJavascriptEventListener(data => {
Debug.Log($"Received string data from javascript: {data}");
});
GetEventSender()
Returns an object which allows you to manually forward input events to the browser.
public UnityIPCEventSender GetEventSender()
Returns
IsUIFocused()
Returns whether or not a web UI element is focused. For example, if a textarea element is being typed into, this will return true.
public bool IsUIFocused()
Returns
Examples
if (Input.GetKey(KeyCode.Space) && !browser.IsUIFocused()) {
Debug.Log($"Spacebar clicked on Unity, browser is not focused.");
}
IsUIHovered()
Returns whether or not a web UI element is currently being hovered by the cursor. For example, if a the
cursor is on top of a line of text, this will return true. You can use the css pointer-events: none;
to
ensure that this returns false for certain elements like text if you want.
It is recommended to set pointer-events: none
on the css selector *
and then set pointer-events: auto
on the elements that should be able to be interacted with.
public bool IsUIHovered()
Returns
Examples
if (Input.GetKey(KeyCode.Space) && !browser.IsUIHovered()) {
Debug.Log($"Spacebar clicked on Unity, cursor is not hovering a browser UI element.");
}
LoadUrl(string)
Change the currently loaded page. A valid URL might look like:
- https://google.com
- file:///path/to/file.html
public void LoadUrl(string url)
Parameters
url
string
RemoveJavascriptEventListener(Action<byte[], int>)
Remove a previously registered event listener. If the listener was not registered, this method does nothing.
public void RemoveJavascriptEventListener(Action<byte[], int> action)
Parameters
RemoveJavascriptEventListener(Action<string>)
Remove a previously registered event listener. If the listener was not registered, this method does nothing.
public void RemoveJavascriptEventListener(Action<string> action)
Parameters
SendDataToJavascript(byte[])
Send data to the currently running Javascript on the rendered page. This data can be recieved in Javascript
by running window.AddEventListener("unitydata", (event) => alert(event.detail));
.
Sends all bytes specified in the data buffer.
public void SendDataToJavascript(byte[] data)
Parameters
data
byte[]
Examples
window.addEventListener("unitydata", (event) => {
if (event.detail instanceof ArrayBuffer) { // event.detail Will be an ArrayBuffer
const view = new DataView(event.detail);
console.log(view.getInt32(0));
}
});
SendDataToJavascript(byte[], int)
Send data to the currently running Javascript on the rendered page. This data can be recieved in Javascript
by running window.AddEventListener("unitydata", (event) => alert(event.detail));
.
Sends all bytes specified in the data buffer starting from the specified offset until the end of the array.
public void SendDataToJavascript(byte[] data, int offset)
Parameters
Examples
window.addEventListener("unitydata", (event) => {
if (event.detail instanceof ArrayBuffer) { // event.detail Will be an ArrayBuffer
const view = new DataView(event.detail);
console.log(view.getInt32(0));
}
});
SendDataToJavascript(byte[], int, int)
Send data to the currently running Javascript on the rendered page. This data can be recieved in Javascript
by running window.AddEventListener("unitydata", (event) => alert(event.detail));
.
Sends length bytes specified in the data buffer starting from the specified offset.
public void SendDataToJavascript(byte[] data, int offset, int length)
Parameters
Examples
window.addEventListener("unitydata", (event) => {
if (event.detail instanceof ArrayBuffer) { // event.detail Will be an ArrayBuffer
const view = new DataView(event.detail);
console.log(view.getInt32(0));
}
});
SendDataToJavascript(string)
Send data to the currently running Javascript on the rendered page. This data can be recieved in Javascript
by running window.AddEventListener("unitydata", (event) => alert(event.detail));
.
window.addEventListener("unitydata", (event) => {
console.log(event.detail); // event.detail Will be a string
});
public void SendDataToJavascript(string data)
Parameters
data
string
SetFramerate(int)
Updates the target framerate for the Alacrity Browser at runtime.
public void SetFramerate(int newFramerate)
Parameters
newFramerate
int
Events
OnClientConnect
Called whenever a client connects to the WebSocket server (which is used to send data between Javascript and C#/Unity). You can use this to ensure you don't send data before the client has connected.
public event WebsocketServer.ClientConnectOrDisconnectHandler OnClientConnect
Event Type
Examples
browser.LoadUrl(...);
browser.OnClientConnect += (_) => {
browser.SendDataToJavascript(...);
}
OnClientDisconnect
Called whenever a client disconnects from the WebSocket server (which is used to send data between Javascript and C#/Unity).
public event WebsocketServer.ClientConnectOrDisconnectHandler OnClientDisconnect
Event Type
Examples
browser.OnClientDisconnect += (_) => {
Debug.Log("A client disconnected.");
}