首先使用3.x的导入功能导入2.x工程
然后就是解决问题
1.i18n使用不了,要转ts。这个已经手动转好了,见:
https://github.com/mapic91/CocosCreator2to3Tools/tree/master/ts/i18n
2.prefab 里的Label字体文件丢失,lineheight比fontsize小
解决方法:
先手动把字体加上,然后看prefab文件内容的前后变化
之前:
手动调整之后:
这样就找到规律了,之后就写个脚本,把所有的prefab文件里的label加上字体就行了。
脚本见:https://github.com/mapic91/CocosCreator2to3Tools/blob/master/tools/correctfont.py
3.因为工程使用的是protobuff,原来是js,我也手动转为ts了,文件见:
https://github.com/mapic91/CocosCreator2to3Tools/tree/master/ts/protobuf
4.然后就是修正代码,也写了脚本,加快修正速度。
https://github.com/mapic91/CocosCreator2to3Tools/blob/master/tools/js2ts.py
这个脚本主要是function转lambda,添加any
https://github.com/mapic91/CocosCreator2to3Tools/blob/master/tools/slashb2f.py
这个脚本是把impornt里的\转为/
其它可以使用VS Code里的自动功能,比如自动import,add all missing member
5.然后跑起来后,发现按钮点击不了,经过和正常按钮的对比,发现原来是prefab转换的时候,节点的z scale是0,导致点击不了,改为1就可以点击。
https://github.com/mapic91/CocosCreator2to3Tools/blob/master/tools/correctnodezscale.py
用这个脚本,就可以把所有prefab里的z scale为0的,改为1
6.因为代码里用了很多2.x的老接口和类的属性,3.x里面没有了,不过我们可以加回来。
https://github.com/mapic91/CocosCreator2to3Tools/blob/master/ts/cocosextend.ts
参考这个文件,就可以扩展cocos 里的类,把2.x的接口和属性加上。比如加上zIndex
declare module "cc" {
interface Node {
zIndex: number;
}
}
Object.defineProperty(Node.prototype, "zIndex", {
get: function() {
let t = this.getComponent(UITransform)
return t ? t.priority : 0
},
set: function(priority:number) {
let t = this.getComponent(UITransform)
if(!t) {
t = this.addComponent(UITransform)
}
t.priority = priority
},
enumerable: true,
configurable: true,
})