Cocos平台 | 快速集成AGC云函数服务

1、环境与应用信息

版本名称 集成环境 测试设备
agconnect-storage:1.3.0.300 Cocos Creator 华为P40Pro

AGC地址:https://developer.huawei.com/consumer/cn/service/josp/agc/index.html
SDK集成方式:在Cocos Creator中开通集成

2、在AGC开通与配置云函数:

PS: 云函数服务目前还处于beta状态,使用前应该发邮件去申请开通:
https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-cloudfunction-apply

1、在我的项目 下选择你的开发项目,在构建下面,找到云函数服务,点击开通:

没有应用的话,需要先自己创建一个。

2、根据CodeLab中的描述,创建一个函数
https://developer.huawei.com/consumer/cn/codelab/CloudFunctions/index.html#4

3、创建Http触发器并且将"详细信息"中的"触发URL"的后缀保存,作为后续客户端请求时的触发器标识。

在这里插入图片描述

3、在Cocos Creator项目集成SDK

1、Cocos集成SDK:

官方文档:https://docs.cocos.com/creator/manual/zh/cocos-service/agc-applinking.html
1、 在 Cocos 服务面板接入所需的云函数服务。目前 SDK 仅支持 Android 平台,

在这里插入图片描述

2、 接入相关的服务前,需要先关联APP,在服务界面点击启用后,再点击 关联 按钮,点击创建后,会跳转到Cocos的管理台,

在这里插入图片描述

3、 根据个人需要创建一个新游戏

4、 创建完成后,就可以回到Cocos Creator界面新建,刷新和选择进行应用的关联,

在这里插入图片描述

5、 然后就可以回到云函数界面,可以正式进行服务的开通了

在这里插入图片描述

2、下载Json

1、 服务开通以后,回到项目设置界面,下载最新的json文件。

在这里插入图片描述

2、将刚下载好json文件,放到Cocos项目的settings目录下

4、界面布局

在Cocos Creator上设置几个按钮,通过点击按钮来实现功能:包括触发云函数,设置超时时间,获取超时时间。

在这里插入图片描述

5、云函数功能开发:

1、在star中初始化云函数

start () {  
        this._func = huawei.agc.func.funcService;
    },

2、 带参数对接云函数

callWithParam:function() {
        this._func.wrap("testcodelab-$latest").call((err, data) => {
            if (err !== null) {
                console.log("Cloud Function", `error: ${JSON.stringify(err)}`);
            }
            console.log("Cloud Function", `result: ${JSON.stringify(data)}`);
        }, {
            year: "2020",
        }
        );
    },

3、不带参数对接云函数

callWithoutParam:function() {
        this._func.wrap("testcodelab-$latest").call((err, data) => {
            if (err !== null) {
                console.log("Cloud Function", `error: ${JSON.stringify(err)}`);
            }
            console.log("Cloud Function", `result: ${JSON.stringify(data)}`);
        });
    },

4、 设置超时时间与获取超时时间

    setTime:function() {
        this._func.wrap("testcodelab-$latest").setTimeout(100,3);
        console.log("Cloud Function", `setTime: ` + 100);

    },

    getTime:function() {
        let currTime = this._func.wrap("testcodelab-$latest").getTimeout();
        console.log("Cloud Function", `currTime: ` + currTime);
    },

6、打包测试

用Cocos creator的build,打包一个Android文件,并且安装到设备上,验证功能点是否正确。分别在Android应用中点击如下按钮,

查看对应的JS日志,有如下日志,表示使用集成成功。

在这里插入图片描述

7、总结

所有函数放在云端就好,只要在端侧用wrap.call方法调用既可实现,后续算法相关的代码都可以放在云端啦,大幅减少APK中的代码和包体大小。

欲了解更多详情,请参见:

云函数服务开发指南:

https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-cloudfunction-introduction

Cocos关于云存储的文档:

https://docs.cocos.com/creator/manual/zh/cocos-service/agc-cloudfunc.html


原文链接:https://developer.huawei.com/consumer/cn/forum/topic/0201427521524950690?fid=0101271690375130218&pid=0301427521524950044

原作者:Mayism

猜你喜欢

转载自blog.51cto.com/14772288/2600637