版本2.4.4
參考:
一 使用第三方庫
二 腳本加載順序引起的問題
一 使用第三方庫
這里使用puremvc框架作為測試,如何在cocos中加入puremvc第三方庫並使用
在項目assets下新建libs文件夾,並將文件放入libs中
返回cocos,彈出提示框,選擇是
選擇puremvc.min.js,右側勾選允許編輯器加載,不勾選cocos會報錯 "puremvc is not defined"。
代碼里使用puremvc注冊和派發消息
const { ccclass, property } = cc._decorator; @ccclass export default class Helloworld extends cc.Component { start() { //注冊Command puremvc.Facade.getInstance().registerCommand("abc", TestCommond); //派發Command puremvc.Facade.getInstance().sendNotification("abc", 123); } } class TestCommond extends puremvc.SimpleCommand{ public execute(notification: puremvc.Notification){ console.log(notification); //輸出消息 } }
運行后輸出,到此purembc做為第三方庫在cocos中可以正常使用。
二 腳本加載順序引起的問題
在使用protobuf第三方庫時,作為插件使用,但是保存后運行,有報錯提示。
原因是第三方庫有依賴關系,A依賴B,但是A先加載進來,B后加載進來,導致A加載完成時找B時找不到。
查看官方教程插件腳本,插件腳本是根據字母順序加載的
默認的文件加載順序如下:
protobuf-bundles.min.js
protobuf-library.min.js
我希望的加載順序是:
protobuf-library.min.js
protobuf-bundles.min.js
所以只能修改文件的名字了。將protobuf-library.min.js前面加上一個a,讓其先加載進來。