Cocos Creator set get

TypeScript中的set与get

private _id:number;
public get id():number{
    return this._id;
}

public set id(value:number){//set:必须无返回值类型
    this._id=value;
}

Inspector面板序列化set与get

const{ccclass,property}=cc._decorator;
@ccclass
export default class Test extends cc.Component{
	@property //只写@property不设置参数
	private _snowPrefab:cc.Node=null;
	
	@property({type:cc.Node,visible:true})
	private set snowPrefab(value:cc.Node){
		cc.log("set snowPrefab");
		this._snowPrefab=value;
	}
	
	private get snowPrefab():cc.Node{
		return this._snowPrefab;
	}
}
注意:Inspector序列化set与get时,访问权限要保持一致。
原文地址:https://www.cnblogs.com/kingBook/p/8747079.html