前端HTML代码
html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>test web ui</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script type="text/javascript">
// WEB UI ~~ UE
"object"!=typeof ue||"object"!=typeof ue.interface?("object"!=typeof ue&&(ue={}),(ue.interface={}),(ue.interface.broadcast=function(e,t){if("string"==typeof e){var o=[e,""];void 0!==t&&(o[1]=t);var n=encodeURIComponent(JSON.stringify(o));
"object"==typeof history&&"function"==typeof history.pushState?(history.pushState({},"","#"+n),history.pushState({},"","#"+encodeURIComponent("[]"))):((document.location.hash=n),(document.location.hash=encodeURIComponent("[]")))}})):(function(e){(ue.interface={}),(ue.interface.broadcast=function(t,o){"string"==typeof t&&(void 0!==o?e.broadcast(t,JSON.stringify(o)):e.broadcast(t,""))})})(ue.interface),(window.ue4=ue.interface.broadcast);window.ue=ue;
</script>
</head>
<body>
<h1>测试WebUI与UE交互</h1>
<button id="btn">调用UE</button>
<script type="text/javascript">
// UE调用方法定义
ue.interface.ue2js = function(ueData){
alert(ueData)
}
$(function(){
$('#btn').on('click',function(){
// 调用UE方法
ue4('js2ue',{"name":"js to ue"})
})
})
</script>
</body>
</html>
UE4蓝图代码
点击页面的《调用UE》按钮后,UE的界面上会打印"js to ue",同样,在UE侧点击按钮后,页面上会弹出含有「 {"name":"jason"} 」的弹窗。