UCefView 1.0.21 Install On Fab Fab logo
World's highest-performance WebView plugin for Unreal Engine. UCefView delivers GPU-accelerated rendering, seamless cross-platform integration and both C++ code and Blueprint development approaches.
Loading...
Searching...
No Matches
Interoperability

UCefView provides robust interoperability between the native Unreal Engine context (C++/Blueprint) and the web context (JavaScript). This allows you to call JavaScript from C++ and vice-versa, enabling the creation of powerful hybrid applications.

The UCefViewBridge Object

To facilitate communication, UCefView automatically injects a bridge object into the window object of the web context. This object provides the primary interface for web pages to interact with your Unreal application.

The name of this bridge object can be configured in your project's Cef Settings via the BridgeObjectName field. The default name is UCefViewBridge.

UCefViewBridge API

Method Description
addEventListener(eventName, listener) Adds a listener function to be called when a specific event is triggered from C++.
removeEventListener(eventName, listener) Removes a previously registered event listener.
invoke(methodName, ...args) Invokes a native C++/Blueprint handler with the specified name and arguments.

C++ to JavaScript

Executing JavaScript Directly

You can execute arbitrary JavaScript code within the context of the WebView from C++.

// Assume you have a pointer to a UCefView widget
UCefView* MyUCefView;
// Execute a simple script in the main frame of the browser.
MyUCefView->ExecuteJavascript(
UCefView::MainFrameID, // The ID of the frame to execute in. MainFrameID targets the top-level frame.
TEXT("alert('hello Unreal')"), // The JavaScript code to execute.
TEXT("") // An optional URL parameter, not typically needed.
);
UCefView is a UMG widget that embeds a Chromium Embedded Framework (CEF) browser view within Unreal E...
Definition CefView.h:43
bool ExecuteJavascript(const FCefFrameId &InFrameId, const FString &InJavascriptCode, const FString &InTargetUrl)
Executes JavaScript code in a specific frame of the CEF view.
static const FCefFrameId MainFrameID
The main frame identity, representing the primary document frame in the browser. Used for targeting m...
Definition CefView.h:51

Event Triggering

You can register an event from javascript code and then trigger the event from C++ code or Blueprint.

Register event handler in Javascript

function onLoad() {
// Add a event listener to handle the event named 'colorChange'
Window.UCefViewBridge.addEventListener(
// event name
"colorChange"
// event handler
function (color) {
// change the background color
document.getElementById("main").style.backgroundColor = color;
}
);
}

Trigger the event from C++ or Blueprint

UCefView* MyUCefView;
// create event
UCefViewEvent* ColorChangeEvent = NewObject<UCefViewEvent>();
// set event name
ColorChangeEvent->Name = TEXT("colorChange");
// set event args with JSON string
ColorChangeEvent->SetArguments(TEXT("[red]"));
// broadcast the event to all frames
MyUCefView->BroadcastEvent(ColorChangeEvent);
Represents an event dispatched from the C++ code in native context to the Javascript code in web cont...
Definition CefViewEvent.h:45
void SetArguments(const FString &InArguments)
Sets the arguments for this event using a JSON string.
FString Name
The name of the event as dispatched from the browser.
Definition CefViewEvent.h:63
bool BroadcastEvent(const UCefViewEvent *InCefViewEvent)
Broadcasts a custom event to all frames in the CEF view.

JavaScript -> C++

UCefViewBridge.invoke

You can register an invoke handler from C++/Blueprint and then invoke the handler from Javascript.

Register invoke handler in C++/Blueprint

UCLASS()
class UWebClient : public UObject
{
GENERATED_BODY()
public:
void InvokeHandler(const int64& InBrowserId,
const FCefFrameId& InFrameId,
const FString& InMethod,
const FString& InArguments)
{
if (InMethod == "HelloNative") {
// InMethod: "HelloNative"
// InArguments: "[1, "2", true]"
}
}
};
// test code
UCefView* MyUCefView = NewObject<UCefView>();
UWebClient* MyWebClient = NewObject<UWebClient>();
// bind invoke handler
MyUCefView->OnInvokeMethod.AddDynamic(MyWebClient, &UWebClient::InvokeHandler);
FOnInvokeMethodEvent OnInvokeMethod
Event triggered when a method is invoked from web content (JavaScript) via the bridge object....
Definition CefView.h:629
Type alias for CEF frame id, used to uniquely identify a browser frame within the Chromium Embedded F...
Definition CefViewTypes.h:43

Invoke native handler from Javascript

// invoke C++ code
window.UCefViewBridge.invoke("HelloNative", 1, "2", true);

window.cefViewQuery(query)

you can register CefViewQuery handler to process the CefViewQuery request and reply with your response.

Register CefViewQuery handler in C++/Blueprint

UCLASS()
class UWebClient : public UObject
{
GENERATED_BODY()
public:
void CefViewQueryHanlder(const int64& InBrowserId, //
const FCefFrameId& InFrameId, //
UCefViewQuery* InQuery)
{
// read request data
FString Request = InQuery->GetRequest();
// Request: "hello from CefViewQuery"
// reply
InQuery->ErrorCode = 0;
InQuery->Response = TEXT("ok");
InQuery->IsSuccess = true;
InQuery->Reply();
}
};
UCefView* MyUCefView = NewObject<UCefView>();
UWebClient* MyWebClient = NewObject<UWebClient>();
// bind CefViewQuery handler
MyUCefView->OnInvokeMethod.AddDynamic(MyWebClient, &UWebClient::CefViewQueryHanlder);
Represents a query sent from the CEF (Chromium Embedded Framework) browser to the Unreal Engine....
Definition CefViewQuery.h:58

Send CefViewQuery request from Javascript and process the response

var query = {
request: "hello from CefViewQuery",
onSuccess: function (response) {
alert("Success: " + response);
},
onFailure: function (error_code, error_message) {
alert("Failure: " + error_message + " (Error Code: " + error_code + ")");
},
};
// send the query
window.cefViewQuery(query);