文章目录新建数据库 初始化环境创建云函数Unity 里面准备工作测试从微信云数据库里面获取数据测试从微信云数据库里面删除数据测试解决每次出包之后想要云函数列表会消失新建数据库 初始化环境初始化wx.cloud.init({evn:你的环境Id})环境id在下图位置最好直接写到Unity wx插件的game.js里面这样不用每次导出都写一遍直接打开这个文件 在最后加上这句话即可将下述配置写入到project.config.json文件中cloudfunctionRoot:cloudfunctions/,cloudfunctionTemplateRoot:cloudfunctionTemplate/最写到Unity WX插件里面创建云函数写完代码之后再新建 cloudfunctions 文件夹选择新建云函数 脚本名字 UpLoadData找到这个文件写入如下代码// 云函数入口文件constcloudrequire(wx-server-sdk)cloud.init({env:cloud.DYNAMIC_CURRENT_ENV})// 使用当前云环境//获取数据库的引用constdbcloud.database();// 云函数入口函数// 上传用户存档数据exports.mainasync(event,context){constwxContextcloud.getWXContext()// 通过自定义传递过来的数据库名字 可以访问不同的数据库constdbdatadb.collection(event.dbName);// 这一行的作用是删除掉 微信官方往数据库里面插入的 UnserInfo和tcbContext 标签数据const{userInfo,tcbContext,...clenedEventData}event.data;// 这里有可能是环境共享所以需要处理一下 如果是共享环境调用的那么unionid就是空的letunionidevent.unionid?event.unionid:wxContext.FROM_UNIONID;if(unionidundefined||unionid){unionidwxContext.UNIONID}letgamedataawaitdbdata.where({unionid:unionid}).get();// 如果没有存过数据 则首次保存if(gamedata.data.length0){gamedata{openid:wxContext.OPENID,unionid:unionid,gamedata:clenedEventData,}letaddDataawaitdbdata.add({data:gamedata})return{code:0,res:addData,data:gamedata,};}// 如果有数据 则更新else{returnawaitdbdata.where({unionid:unionid}).update({data:{openid:wxContext.OPENID,unionid:unionid,gamedata:clenedEventData,}})}}然后将云函数上传到云端在上传一遍 并选择 云端安装依赖 不然无法调用会报错Unity 里面准备工作Unity 里面新建一个脚本 WXCloudFuncTest.csusingUnityEngine;usingWeChatWASM;usingNewtonsoft.Json;[System.Serializable]publicclassGameData{publicintscore0;publicintscore1;publicintweekNumber;publicstringnickName;publicstringavatarUrl;publicstringprovince;publicstringdbName;// 这个就是自定义访问的具体哪个数据库}publicclassWXCloudFuncTest:MonoBehaviour{privatevoidCallSetUserData(GameDatagameData){stringjsonDataJsonConvert.SerializeObject(gameData);Debug.LogError($CallSetUserData gameData:{jsonData});WX.cloud.CallFunction(newCallFunctionParam(){nameUpLoadData,datagameData,success(res){Debug.LogError($Call UpLoadData Success:{res.result});},fail(res){Debug.LogError($Call UpLoadData Fail:{res.errMsg});},complete(res){Debug.LogError($Call UpLoadData Complete);}});}privatevoidSendScoreToServer(){GameDatagameDatanewGameData(){score0Random.Range(1,10),score1Random.Range(11,20),nickNameZyt,avatarUrl,weekNumberRandom.Range(0,7),province北京市,dbNameTest,// 我们创建了一个Test的数据库 所以这里直接填写Test数据库名字即可};CallSetUserData(gameData);}publicvoidOnClickSendScoreButton(){SendScoreToServer();}}在这里新建一个Button 然后绑定发送数据的函数测试直接转换小游戏 然后运行 点击按钮 打印SCall UpLoadData Success: 就表示调用成功了然后打开云开发 找到数据库 可以看到数据已经存储成功从微信云数据库里面获取数据新建一个GetUserData的云函数 还是上传并同步到云端代码如下// 云函数入口文件constcloudrequire(wx-server-sdk)// 使用当前云环境cloud.init({env:cloud.DYNAMIC_CURRENT_ENV})//获取数据库的引用constdbcloud.database();exports.mainasync(event,context){constwxContextcloud.getWXContext()// 通过自定义传递过来的数据库名字 可以访问不同的数据库constdbdatadb.collection(event.dbName);// 这里有可能是环境共享所以需要处理一下 如果是共享环境调用的那么unionid就是空的letunionidevent.unionid?event.unionid:wxContext.FROM_UNIONID;if(unionidundefined||unionid){unionidwxContext.UNIONID}letgamedataawaitdbdata.where({unionid:unionid}).get();if(gamedata.data.length0){returnnull;}else{returngamedata;}}Unity 里面代码 还是哪个.cs脚本 直接更新usingUnityEngine;usingWeChatWASM;usingNewtonsoft.Json;[System.Serializable]publicclassResponseResultData{publicUserData[]data;publicstringerrMsg;}[System.Serializable]publicclassUserData{publicstring_id;publicstringopenid;publicGameDatagamedata;}[System.Serializable]publicclassGameData{publicstringavatarUrl;publicstringnickName;publicstringprovince;publicintscore0;publicintscore1;publicintweekNumber;publicstringdbName;// 这个就是自定义访问的具体哪个数据库}publicclassWXCloudFuncTest:MonoBehaviour{privatevoidCallSetUserData(GameDatagameData,stringfuncName,System.Actionstringcallbacknull){stringjsonDataJsonConvert.SerializeObject(gameData);Debug.LogError($CallSetUserData testData:{jsonData});WX.cloud.CallFunction(newCallFunctionParam(){namefuncName,datagameData,success(res){Debug.LogError($Call UpLoadData Success:{res.result});callback?.Invoke(res.result);},fail(res){Debug.LogError($Call UpLoadData Fail:{res.errMsg});},complete(res){Debug.LogError($Call UpLoadData Complete);}});}privatevoidSendScoreToServer(){GameDatagameDatanewGameData(){score0Random.Range(1,10),score1Random.Range(11,20),nickNameZyt,avatarUrl,weekNumberRandom.Range(0,7),province北京市,dbNameTest,// 我们创建了一个Test的数据库 所以这里直接填写Test数据库名字即可};CallSetUserData(gameData,UpLoadData);}publicvoidOnClickSendScoreButton(){SendScoreToServer();}publicvoidOnClickGetUserDataButton(){GameDatagameDatanewGameData();gameData.dbNameTest;CallSetUserData(gameData,GetUserData,(jsonData){if(jsonData){Debug.LogError(未查询到数据!!!);return;}ResponseResultDatadataJsonConvert.DeserializeObjectResponseResultData(jsonData);Debug.LogError($CallSetUserData callback :{data.data.Length}{data.errMsg}{data.data[0]._id}{data.data[0].openid}{data.data[0].gamedata.province});});}}按钮也绑定一下事件测试直接重新出包 直接编译 然后运行后点击 GetUserDataButton 然后就可以获取到如下数据了从微信云数据库里面删除数据新建一个ClearUserData的云函数 还是上传并同步到云端代码如下// 云函数入口文件constcloudrequire(wx-server-sdk)cloud.init({env:cloud.DYNAMIC_CURRENT_ENV})// 使用当前云环境//获取数据库的引用constdbcloud.database();// 云函数入口函数exports.mainasync(event,context){constwxContextcloud.getWXContext()// 通过自定义传递过来的数据库名字 可以访问不同的数据库constdbdatadb.collection(event.dbName);// 这里有可能是环境共享所以需要处理一下 如果是共享环境调用的那么unionid就是空的letunionidwxContext.FROM_UNIONIDif(unionidundefined||unionid){unionidwxContext.UNIONID}// 查询用户是否已经保存过数据letgamedataawaitdbdata.where({// openid:wxContext.OPENIDunionid:unionid}).get();// 如果没有存过数据 则直接返回nullif(gamedata.data.length0){// 这里做一个假的返回return{errMsg:collection.remove:fail,stats:{removed:0,}};}// 如果有数据 则返回玩家数据else{// 通过玩家的唯一id 查找对应的数据returnawaitgamedata.where({// openid:wxContext.OPENIDunionid:unionid}).remove();}}Unity 里面代码 还是哪个.cs脚本 直接更新usingUnityEngine;usingWeChatWASM;usingNewtonsoft.Json;[System.Serializable]publicclassResponseResultData{publicUserData[]data;publicstringerrMsg;}[System.Serializable]publicclassUserData{publicstring_id;publicstringopenid;publicGameDatagamedata;}[System.Serializable]publicclassGameData{publicstringavatarUrl;publicstringnickName;publicstringprovince;publicintscore0;publicintscore1;publicintweekNumber;publicstringdbName;}[System.Serializable]publicclassClearUserDataResponseData{publicstringerrMsg;publicClearUserDatastats;}[System.Serializable]publicclassClearUserData{publicstringremoved;}publicclassWXCloudFuncTest:MonoBehaviour{privatevoidCallSetUserData(GameDatagameData,stringfuncName,System.Actionstringcallbacknull){stringjsonDataJsonConvert.SerializeObject(gameData);Debug.LogError($CallSetUserData testData:{jsonData});WX.cloud.CallFunction(newCallFunctionParam(){namefuncName,datagameData,success(res){Debug.LogError($Call UpLoadData Success:{res.result});callback?.Invoke(res.result);},fail(res){Debug.LogError($Call UpLoadData Fail:{res.errMsg});},complete(res){Debug.LogError($Call UpLoadData Complete);}});}privatevoidSendScoreToServer(){GameDatagameDatanewGameData(){score0Random.Range(1,10),score1Random.Range(11,20),nickNameZyt,avatarUrl,weekNumberRandom.Range(0,7),province北京市,dbNameTest,};CallSetUserData(gameData,UpLoadData);}publicvoidOnClickSendScoreButton(){SendScoreToServer();}publicvoidOnClickGetUserDataButton(){GameDatagameDatanewGameData();gameData.dbNameTest;CallSetUserData(gameData,GetUserData,(jsonData){if(jsonData){Debug.LogError(未查询到数据!!!);return;}ResponseResultDatadataJsonConvert.DeserializeObjectResponseResultData(jsonData);Debug.LogError($CallSetUserData callback :{data.data.Length}{data.errMsg}{data.data[0]._id}{data.data[0].openid}{data.data[0].gamedata.province});});}publicvoidOnClickClearUserDataButton(){GameDatagameDatanewGameData();gameData.dbNameTest;CallSetUserData(gameData,ClearUserData,(result){ClearUserDataResponseDataclearUserDataResponseDataJsonConvert.DeserializeObjectClearUserDataResponseData(result);if(clearUserDataResponseData.stats.removed0clearUserDataResponseData.errMsgcollection.remove:fail){Debug.LogError(删除失败 数据库并没有查到此人数据);}else{Debug.LogError($删除成功 删除条目:{clearUserDataResponseData.stats.removed});}});}}按钮绑定一下事件测试直接重新出包 直接编译 然后运行后点击 ClearUserDataButton 然后就可以获取到如下数据了这个是有数据删除成功这个是没有数据 删除失败解决每次出包之后想要云函数列表会消失找到project.config.json复制cloudfunctions鼠标放到这里就出现按钮了 然后点击新建文件夹 把名字放进去然后右键选中这个文件夹 选择同步云函数列表右键选中函数 然后下载 等待下载完成之后就能看到之前写的云函数了 其他函数也是如此