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

一、环境与应用信息

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

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

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

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

一、在个人项目 下选择你的开发项目,在构建下面,找到云函数服务,点击开通:算法

没有应用的话,须要先本身建立一个。json

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

三、建立Http触发器而且将"详细信息"中的"触发URL"的后缀保存,做为后续客户端请求时的触发器标识。ide

在这里插入图片描述

三、在Cocos Creator项目集成SDK

一、Cocos集成SDK:

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

在这里插入图片描述

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

在这里插入图片描述

三、 根据我的须要建立一个新游戏测试

四、 建立完成后,就能够回到Cocos Creator界面新建,刷新和选择进行应用的关联,ui

在这里插入图片描述

五、 而后就能够回到云函数界面,能够正式进行服务的开通了

在这里插入图片描述

二、下载Json

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

在这里插入图片描述

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

四、界面布局

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

在这里插入图片描述

五、云函数功能开发:

一、在star中初始化云函数

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

二、 带参数对接云函数

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",
        }
        );
    },

三、不带参数对接云函数

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)}`);
        });
    },

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

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);
    },

六、打包测试

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

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

在这里插入图片描述

七、总结

全部函数放在云端就好,只要在端侧用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

相关文章
相关标签/搜索