/ 开发笔记

#Cocos Creator# 序列化自定义类报错:Can not serialize 'xxx' because the specified type is anonymous, please provide a class name

想在Cocos Creator的场景编辑器的使用自定义的ccclass子类,却遇到这个奇葩的错误:

Can not serialize 'BalancePlayground.testRulers' 
because the specified type is anonymous, 
please provide a class name 
or 
set the 'serializable' attribute of 'BalancePlayground.testRulers' to 'false'.

同时,在编辑器的属性检查器里,无法正常显示该类的属性值:
Snip20190226_8

这是为什么呢?

这个错误描述是相当的容易误会。

上代码,原始出错的代码(TypeScript):

// 定义
@ccclass
export default class RulerConfig {
    @property("number")
    height:number = 0;
    @property(cc.SpriteFrame)
    photo:cc.SpriteFrame = undefined;
    @property("string")
    desc:string = "";
}

// 引用
...
    @property(RulerConfig)
    testRulers:Array<RulerConfig> = [];
...

修改方法
在定义的地方,为ccclass提供一个name参数,比如:

@ccclass("RulerConfig")
export default class RulerConfig {
    @property("number")
    height:number = 0;
    @property(cc.SpriteFrame)
    photo:cc.SpriteFrame = undefined;
    @property("string")
    desc:string = "";
}

// 引用地方不需要改动

然后就OK了,属性检测器显示正常:
Snip20190226_9