终于等到你,远程真机调试+Cocos开发鸿蒙游戏,真香!
@[toc](目录)
6.25新鲜出炉的DevEco 2.2 beta 提供了远程真机调试,而上周6.18发布的Cocos Creator 3.2 支持一键打包鸿蒙项目。
作为一个没有鸿蒙手机的游戏开发者,突发奇想,能否双剑合璧,用远程真机设备来开发鸿蒙游戏呢?
总个过程比预想的顺畅多了,没有遇到太多坑,推荐游戏开发者去体验一下鸿蒙的开发过程。
先放一张测试截图:
![buildandrunharmonygamewithcocoscreatoronremotedevice01.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/61635a7934552dcf5da65567a1a1ed96e65eba.png?x-oss-process=image/resize,w_1135,h_625)
# 利用真机调试+Cocos Creator开发鸿蒙游戏详细步骤
## 1. 账号准备
* 1). 华为开发者账号
https://developer.harmonyos.com/
* 2). 华为商店账号
https://developer.huawei.com/consumer/cn/service/josp/agc/index.html#/
* 3). Cocos账号(登录Cocos Creator用)
https://www.cocos.com/creator
## 2. 下载Cocos Creator 3.2
> https://www.cocos.com/creator
![buildandrunharmonygamewithcocoscreatoronremotedevice02.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/b94b21a2078a82cc8e9750adc020f9699ee156.png?x-oss-process=image/resize,w_1174,h_567)
## 3. 下载鸿蒙开发工具DevEco 2.2
https://developer.harmonyos.com/cn/develop/deveco-studio
![buildandrunharmonygamewithcocoscreatoronremotedevice03.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/52116e521b041058e2a65066afe851d58f154a.png?x-oss-process=image/resize,w_1338,h_371)
![buildandrunharmonygamewithcocoscreatoronremotedevice04.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/67a307721d0ed7a78cb413bae44bd408977147.png?x-oss-process=image/resize,w_1052,h_603)
## 4. 启动DevEco并下载鸿蒙SDK和NDK(必须)
要用Cocos Creator打包鸿蒙项目,需要安装鸿蒙Java SDK和Native SDK (也叫NDK)
启动后,打开设置界面,选择HarmonyOS SDK,或者通过SDK Manager,下载Harmony OS SDK。
![buildandrunharmonygamewithcocoscreatoronremotedevice05.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/d9a22df97b97655b28f705dc8ad5e8992afa4b.png?x-oss-process=image/resize,w_977,h_723)
## 5. 用Cocos Creator创建一个游戏
Cocos Creator 3.2提供了一个相对完整的游戏示例项目(Example Taxi Game),虽然有一些bug,但是不影响测试。
直接使用这个示例项目来创建游戏。
![buildandrunharmonygamewithcocoscreatoronremotedevice06.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/e207ac142b6b7d0ab69936d9a7bb1f89d4b4cf.png?x-oss-process=image/resize,w_926,h_621)
## 6. 在Cocos Creator中配置鸿蒙SDK并打包鸿蒙项目
回到Cocos Creator,打开Project->Build菜单
![buildandrunharmonygamewithcocoscreatoronremotedevice07.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/51d765f41f70e3d750b503b0c36fca8f2da8aa.png?x-oss-process=image/resize,w_365,h_265)
修改几个地方:
* 1). Platform平台,选择 Harmony OS
![buildandrunharmonygamewithcocoscreatoronremotedevice08.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/0189f9465b1558c9f3350989d316f4508fae7d.png?x-oss-process=image/resize,w_593,h_744)
* 2). Game Package Name 游戏包名,这个很重要。
![buildandrunharmonygamewithcocoscreatoronremotedevice09.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/d69e3e3793476e13be53528444b9a94ba910aa.png?x-oss-process=image/resize,w_545,h_474)
* 3). 如果是首次打包,还需要设置鸿蒙SDK
点击 Set Harmony OS SDK
![buildandrunharmonygamewithcocoscreatoronremotedevice10.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/49d740d015204d37463026742345e28fc3ec72.png?x-oss-process=image/resize,w_587,h_779)
配置 Harmony OS NDK和Harmony OS SDK
![buildandrunharmonygamewithcocoscreatoronremotedevice11.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/17cabe775e0c71de2f76262792bab53f855409.png?x-oss-process=image/resize,w_817,h_561)
* 4). 点Build进行打包
首次打包会慢一些,因为需要编译Cocos引擎的代码。
![buildandrunharmonygamewithcocoscreatoronremotedevice12.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/0291581264643f16aaf697fbac12afc4cf9936.png?x-oss-process=image/resize,w_625,h_247)
## 7. 在华为商店创建一个项目,并添加鸿蒙应用
> https://developer.huawei.com/consumer/cn/service/josp/agc/index.html#/myProject
添加项目
![buildandrunharmonygamewithcocoscreatoronremotedevice13.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/872e6936127a2da72da343a46f64329ee5b851.png?x-oss-process=image/resize,w_887,h_588)
![buildandrunharmonygamewithcocoscreatoronremotedevice14.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/87ebb90069d1494a7fe329dad6f33a6d3244b7.png?x-oss-process=image/resize,w_646,h_268)
添加应用
![buildandrunharmonygamewithcocoscreatoronremotedevice15.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/b5c53a6462f802777f0697b13d71e48c539cd0.png?x-oss-process=image/resize,w_869,h_279)
![buildandrunharmonygamewithcocoscreatoronremotedevice16.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/d8eaac8717fcd1c84fd735334d5a906fe7c888.png?x-oss-process=image/resize,w_1079,h_654)
注意,应用包名需要与Cocos项目中的Game Package Name一致。
如果不一致,也可以稍后在鸿蒙项目中修改bundle name(见下一步)。
## 8. 用DevEco 2.2打开鸿蒙游戏项目
Creator打包好的鸿蒙项目,目录跟其他平台的不一样,默认是 /native/engine/ohos
用DevEco的Open Project菜单打开这个目录。
![buildandrunharmonygamewithcocoscreatoronremotedevice17.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/49fb94a04b0e61d0817087d7543f67ed0cc02e.png?x-oss-process=image/resize,w_1182,h_379)
检查ohos/entry/src/main/config.json中 app->bundleName是否与应用商店的应用包名一致。
如果不一致,修改成一致。
![buildandrunharmonygamewithcocoscreatoronremotedevice18.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/02193e803319186d5b56523bda94b990fc9774.png?x-oss-process=image/resize,w_887,h_612)
## 9. 连接远程真机设备
打开DevEco->Tools->Device Manager
根据提示登录华为开发者账号,并授权。
![buildandrunharmonygamewithcocoscreatoronremotedevice19.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/784256b73e3133ed8a3359ffc2ba5f7b961b8c.png?x-oss-process=image/resize,w_1459,h_730)
![buildandrunharmonygamewithcocoscreatoronremotedevice20.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/091429f43e31b7cde723078e2e522227e06ca7.png?x-oss-process=image/resize,w_771,h_217)
授权完毕后,返回DevEco,切换到Remote Device -> Phone 可以看到一台
Mate 40 Pro+ !!!
![buildandrunharmonygamewithcocoscreatoronremotedevice21.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/83b07d5867432c50c17655d92895ee18268281.png?x-oss-process=image/resize,w_1442,h_720)
启动!
哒哒~~~~
![buildandrunharmonygamewithcocoscreatoronremotedevice22.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/12d130074e5879de052186d788d812d1f9c12e.png?x-oss-process=image/resize,w_561,h_593)
对比一下p40模拟器
![buildandrunharmonygamewithcocoscreatoronremotedevice23.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/778173230221152d6171362cb6bf067bd4d74b.png?x-oss-process=image/resize,w_531,h_657)
## 10. 利用DevEco自动生成打包证书
点这个按钮,打开Project Structure界面
![buildandrunharmonygamewithcocoscreatoronremotedevice24.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/d6d7ff2755c9976595b1755d52859bb6cb26e7.png?x-oss-process=image/resize,w_678,h_148)
勾选自动生成证书(Automatically generate signing)
![buildandrunharmonygamewithcocoscreatoronremotedevice25.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/559a5f376c886e149802830ae70ebd612046e2.png?x-oss-process=image/resize,w_866,h_152)
如果商店后台设置正确,过一会以后,就会自动生成证书。
![buildandrunharmonygamewithcocoscreatoronremotedevice26.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/69445139515b71282310903eaeb9e39a67b689.png?x-oss-process=image/resize,w_888,h_683)
### 注意事项
* 1). 每次连接新的远程真机,都需要重新生成证书。
所以,自动生成证书真是太方便了!!!
* 2). 如果出现无法保存的错误提示
![buildandrunharmonygamewithcocoscreatoronremotedevice27.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/a36ab1468b161f31954266fe768349b095fcff.png?x-oss-process=image/resize,w_485,h_171)
则回到项目中,将项目中所有build.gradle中的signingConfigs字段都删掉,这些是Cocos Creator生成的项目中填写的Cocos的证书。
然后重新生成证书,就可以了。
## 11. 打包并运行
点击右上角的Run entry,打包并运行
![buildandrunharmonygamewithcocoscreatoronremotedevice30.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/08ee615173839df70d8518ab6e42f71d8b8830.png?x-oss-process=image/resize,w_548,h_104)
过一会……
再过一会……
再过一会会……
然后……
就能看到Cocos的启动画面……
然后……
哒哒,老司机们,来开车吧~
![buildandrunharmonygamewithcocoscreatoronremotedevice31.png](https://harmonyos.oss-cn-beijing.aliyuncs.com/images/202106/e82946d8918e2c28fa8072a8a96dba0ed01169.png?x-oss-process=image/resize,w_537,h_680)
# 几个问题
调试过程发现一些问题,但是都与鸿蒙无关:
* 1). Cocos Creator提供的游戏示例,有非常多bug,这个真不是鸿蒙的问题。
* 2). 远程真机调试有延迟,对于操作敏感的游戏,体验稍差。
* 3). 远程真机调试的画面稍微有点渣。
* 4). 断开真机重连的话,需要更新证书,因为可能连的不是上一个。
[想了解更多关于鸿蒙的内容,请访问:](https://harmonyos.51cto.com/#bkwz)
[51CTO和华为官方战略合作共建的鸿蒙技术社区](https://harmonyos.51cto.com/#bkwz)
https://harmonyos.51cto.com/#bkwz