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++.
TEXT("alert('hello Unreal')"),
TEXT("")
);
UCefView is a UMG widget that embeds a Chromium Embedded Framework (CEF) browser view within Unreal E...
Definition CefView.h:45
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:53
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
ColorChangeEvent->
Name = TEXT(
"colorChange");
Represents an event dispatched from the C++ code in native context to the Javascript code in web cont...
Definition CefViewEvent.h:49
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 FString& InMethod,
const FString& InArguments)
{
if (InMethod == "HelloNative") {
}
}
};
UCefView* MyUCefView = NewObject<UCefView>();
UWebClient* MyWebClient = NewObject<UWebClient>();
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:646
Type alias for CEF frame id, used to uniquely identify a browser frame within the Chromium Embedded F...
Definition CefViewTypes.h:45
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,
{
FString Request = InQuery->GetRequest();
InQuery->ErrorCode = 0;
InQuery->Response = TEXT("ok");
InQuery->IsSuccess = true;
InQuery->Reply();
}
};
UCefView* MyUCefView = NewObject<UCefView>();
UWebClient* MyWebClient = NewObject<UWebClient>();
MyUCefView->
OnInvokeMethod.AddDynamic(MyWebClient, &UWebClient::CefViewQueryHanlder);
Represents a query sent from the CEF (Chromium Embedded Framework) browser to the Unreal Engine....
Definition CefViewQuery.h:62
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);