遊戲端和合約端略過,寫一下 unity 和合約的交互部分。
1. 在 unity 專案 plugins 資料夾下,首先新建一個後綴.jslib 的檔案,檔案名自定義。
mergeInto(LibraryManager.library, {
StartTime: function(){ //StartTime是Unity調用的方法名字
BeginTime(); //BeginTime是調用前端BeginTime()方法
},
});
2. 在 unity 中,新建一個腳本,添加命名空間 using System.Runtime.InteropServices; 並添加以下 C# 程式碼:
[DllImport("__Internal")]
private static extern void StartTime();
3. 在 unity 中調用 StartTime () 方法,即調用 jslib 裡的 StartTime () 方法
function StartTime() {
}
4. 將 unity 用 webgl 打包,壓縮方式選擇不壓縮
也可以使用本專案使用第三方插件 react-unity-webgl 在 React 專案中加載由 Unity 專案打包得到的 WebGL 資源。
github:https://github.com/skka3134/plantsVsZombines/tree/main