行业知识
前端与Ue4交互实战经验
2026-11-15 06:38:48
来源:互联网转载
本人是前端开发,我跟刚接触ue的小伙伴们一样也是一头雾水,但只要翻过这个坎,回头望去,其实并没有什么,事事如此。 废话不多说,直接上代码,简单暴力。
1.首先在public文件夹下入口文件 index.html 中引入以下代码
"object" != typeof ue && (ue = {}), ue4 = function (functoid) { "object" != typeof ue.interface && (delete ue.interface, ue.interface = {}); return function (key, json, cback, timeout) { if ("object" != typeof ue["$receive"] || "function" != typeof ue["$receive"]["asyn"]) { console.error("[ue.$receive.asyn] drop message .... key" + key + " json" + JSON.stringify(json)); return; } "string" == typeof key && ("function" == typeof json && (timeout = cback, cback = json, json = null)); var backid = functoid(cback, timeout); void "string" !== typeof json ? ue["$receive"].asyn(key, JSON.stringify(json), backid) : ue["$receive"].asyn(key, json, backid); }; }( function (callback, timeout) { if ("function" != typeof callback) return ""; var funcid = function () { return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, function (t) { return (t ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> t / 4).toString(16) }) }(); return ue.interface[funcid] = callback, setTimeout(function () { delete ue.interface[funcid] }, 1e3 * Math.max(2, parseInt(timeout) || 0)), funcid } ); //执行ready后,UE会以函数形式返回OK window.onload = function () { ue4("ready"); } 2.web调用Ue函数 (方法名web与Ue必须一致)
//例如以下是页面跳转交互逻辑 onHome() { // "click" 是跟ue约定好的方法名 ue4("click", { btnName: "btn_", //非必须 btnID: "ID", //非必须 btnCategory: "OnHomePage", //此处参数需要跟ue达成一致 data: {}, //非必须 }); this.$router.push("/home"); }, 3.Ue调用Web方法 (方法名web与Ue必须一致)
mounted() { //在ue中是存在this指向问题的,需要声明this var that = this; // ueEventMsg是约定好的方法名 ue.interface.ueEventMsg = function (val) { //如果ue返回的 val.eventName === "Persons" 的话做逻辑操作 switch (val.eventName) { case "Persons": that.PersonsId = val.eventContent.ID; break; default: // 默认情况下的操作 break; } }; },