Initial Cocos Creator port of main-site Unity WebGL game.
Includes core gameplay, 600 exported levels, visual assets, web bridge, and bootstrap scene. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
43
web-template/cocos-bridge.js
Normal file
43
web-template/cocos-bridge.js
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* 主站页面对接层:在 Cocos 启动后提供与 Unity WebGL 相同的 unityInstance.SendMessage API
|
||||
* 用法:构建产物 index.html 末尾引入本脚本,或在主站模板中引入
|
||||
*/
|
||||
(function (global) {
|
||||
function processData(json) { global.__tfrhOnProcessData?.(json); console.log('[processData]', JSON.parse(json)); }
|
||||
function processVehicleData(json) { global.__tfrhOnProcessVehicleData?.(json); console.log('[processVehicleData]', JSON.parse(json)); }
|
||||
function externalResult(json) { global.__tfrhOnExternalResult?.(json); console.log('[externalResult]', JSON.parse(json)); }
|
||||
function externalLevelInfo(json) { global.__tfrhOnExternalLevelInfo?.(json); console.log('[externalLevelInfo]', JSON.parse(json)); }
|
||||
function coinsData(json) { global.__tfrhOnCoinsData?.(json); console.log('[coinsData]', JSON.parse(json)); }
|
||||
|
||||
global.processData = processData;
|
||||
global.processVehicleData = processVehicleData;
|
||||
global.externalResult = externalResult;
|
||||
global.externalLevelInfo = externalLevelInfo;
|
||||
global.coinsData = coinsData;
|
||||
|
||||
// 动态 processPlayerA1 等
|
||||
['A1','A2','A3','B1','B2','B3'].forEach(function (id) {
|
||||
global['processPlayer' + id] = function (json) { console.log('[processPlayer' + id + ']', JSON.parse(json)); };
|
||||
global['processVehicle' + id] = function (json) { console.log('[processVehicle' + id + ']', JSON.parse(json)); };
|
||||
});
|
||||
|
||||
function waitInstance(maxMs) {
|
||||
maxMs = maxMs || 30000;
|
||||
return new Promise(function (resolve, reject) {
|
||||
var t0 = Date.now();
|
||||
(function tick() {
|
||||
if (global.cocosIns || global.unityInstance) return resolve(global.cocosIns || global.unityInstance);
|
||||
if (Date.now() - t0 > maxMs) return reject(new Error('Cocos instance timeout'));
|
||||
requestAnimationFrame(tick);
|
||||
})();
|
||||
});
|
||||
}
|
||||
|
||||
global.tfrhReady = waitInstance;
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
waitInstance().then(function () {
|
||||
console.log('[tfrh] Cocos bridge ready, use unityInstance.SendMessage(...)');
|
||||
}).catch(function (e) { console.warn('[tfrh]', e.message); });
|
||||
});
|
||||
})(typeof window !== 'undefined' ? window : globalThis);
|
||||
Reference in New Issue
Block a user