[Cocos Creator] 加载远程代码

14 篇文章 1 订阅
13 篇文章 0 订阅
// 加载代码
let url = "http://远程域名或ip地址/路径/TestRemote.js";
cc.loader.load(url, (err, res) => {
    if (err) {
        console.error(err);
    } else {
        // 1. 普通函数可以直接调用
        Print("Hello World");

        // 2. 继承cc.Component的类
        let clsName = "TestRemote";// 自己定义,需要和TestRemote.js文件中类名一致
        let cls = cc.js.getClassByName(clsName);
        if (!cls) {
            let remoteCls: any = window[clsName];
            if (window.__modular) {
                // preview
                window.__modular.modules[clsName] = {
                    file: clsName
                };
                window.__modular.nameMap[clsName] = clsName;
            }
            if (window.__require) {
                // build
            }
            let o = function (r) {
                var u = {
                    exports: {}
                };
                remoteCls[0].call(u.exports, function (t) {
                    return o(remoteCls[1][t] || t)
                }, u, u.exports);
                return u.exports
            }
            o(clsName);
        }
        let node = new cc.Node();
        node.parent = this.node;
        node.setPosition(0, 100);
        let com = node.addComponent("TestRemote");
        let labelNode = new cc.Node();
        labelNode.parent = node;
        com.label = labelNode.addComponent(cc.Label);
        com.setLabel("Hahaha");

        console.log('done');
    }
});
// 远程代码: 下面3种任选一种
// 1.普通js代码
function Print(msg) {
    console.log('Print', msg);
}
// =====================================================
// 2.cocos creator 继承cc.Component的js类编译后版本:
// build版本之后获取到的,"37c8fTvEXFFTJXIilra/hLt"为随机生成的uuid,由编辑器生成
var TestRemote = [function (t, e, i) {
    "use strict";
    cc._RF.push(e, "37c8fTvEXFFTJXIilra/hLt", "TestRemote", "TestRemote");
    cc.Class({
        extends: cc.Component,
        properties: {
            label: cc.Label
        },
        start: function () { },
        setLabel: function (t) {
            this.label.string = t
        }
    });
    cc._RF.pop()
}, {}]
// =====================================================
// 2.cocos creator 继承cc.Component的ts类编译后版本:
// build版本之后获取到的,"19b791y98xIZLnToPi/DNyb"为随机生成的uuid,由编辑器生成
var TestRemote = [function(t, e, i) {
    "use strict";
    cc._RF.push(e, "19b791y98xIZLnToPi/DNyb", "TestRemote", "TestRemote");
    var n = this && this.__extends || function() {
        var t = function(e, i) {
            return (t = Object.setPrototypeOf || {
                __proto__: []
            }instanceof Array && function(t, e) {
                t.__proto__ = e
            }
            || function(t, e) {
                for (var i in e)
                    e.hasOwnProperty(i) && (t[i] = e[i])
            }
            )(e, i)
        };
        return function(e, i) {
            function n() {
                this.constructor = e
            }
            t(e, i),
            e.prototype = null === i ? Object.create(i) : (n.prototype = i.prototype,
            new n)
        }
    }()
      , o = this && this.__decorate || function(t, e, i, n) {
        var o, s = arguments.length, r = s < 3 ? e : null === n ? n = Object.getOwnPropertyDescriptor(e, i) : n;
        if ("object" == typeof Reflect && "function" == typeof Reflect.decorate)
            r = Reflect.decorate(t, e, i, n);
        else
            for (var a = t.length - 1; a >= 0; a--)
                (o = t[a]) && (r = (s < 3 ? o(r) : s > 3 ? o(e, i, r) : o(e, i)) || r);
        return s > 3 && r && Object.defineProperty(e, i, r),
        r
    }
    ;
    Object.defineProperty(i, "__esModule", {
        value: !0
    });
    var s = cc._decorator
      , r = s.ccclass
      , a = s.property
      , c = function(t) {
        function e() {
            var e = null !== t && t.apply(this, arguments) || this;
            return e.label = null,
            e
        }
        return n(e, t),
        e.prototype.onLoad = function() {}
        ,
        e.prototype.start = function() {}
        ,
        e.prototype.setLabel = function(t) {
            this.label.string = t
        }
        ,
        o([a(cc.Label)], e.prototype, "label", void 0),
        e = o([r], e)
    }(cc.Component);
    i.TestRemote = c,
    cc._RF.pop()
}
, {}]

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GrimRaider

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值