如何动态设置属性的type

image
就相当于实现EventHandle的component可以根据拉入的node来自动改变展示的枚举选项功能

image
就是这个type如何动态改变

枚举的话可以用

function setEnumAttr(obj, propName, enumDef) {
    cc.Class.Attr.setClassAttr(obj, propName, 'type', 'Enum');
    cc.Class.Attr.setClassAttr(obj, propName, 'enumList', cc.Enum.getList(enumDef));
}

然后 setEnumAttr(object, “m_iComponent”, {});

2赞


2.4版本的cocoscreator怎么弄呀

内部接口,直接用就行。别管报错

2赞

非常感谢。

mark mark!

皮皮大佬最近有上架什么新的插件吗

大佬,没有用呀

/** 目标节点 */
@property({
    type: cc.Node,
    displayName: 'Node',
    tooltip: '目标节点',
})
m_ndTarget: cc.Node = null;
/** 组件数组 */
m_arrComponents: cc.Component[] = null;
/** 组件索引 */
@property({
    visible() {
        if (!this.m_ndTarget && this.m_arrComponents !== null) {
            this.m_arrComponents = null;
            this.m_iComponent = -1
        }
        if (this.m_ndTarget && this.m_arrComponents === null) {
            this.m_arrComponents = this.m_ndTarget.getComponents(cc.Component);
            const enumComponents = {};
            const componentCount = {};
            let component = null;
            let className = '';
            for (let i = 0, length = this.m_arrComponents.length; i < length; i++) {
                component = this.m_arrComponents[i];
                className = cc.js.getClassName(component);
                if (componentCount[className] === undefined) {
                    componentCount[className] = 0;
                }
                else {
                    componentCount[className]++;
                    className += ' - ' + (componentCount[className] < 10 ? '00' : componentCount[className] < 100 ? '0' : '') + componentCount[className];
                }
                enumComponents[className] = i;
            }
            setEnumAttr(this, 'm_iComponent', enumComponents);
        }
        return this.m_ndTarget;
    },
    displayName: 'Component',
    tooltip: '目标组件(节点上的组件)',
})
m_iComponent: number = -1;

大佬帮我看看怎么回事

这个跟类绑定了,type改变后所有的实例的type都变了

/** 目标节点 */
@property({
    type: cc.Node,
    displayName: 'Node',
    tooltip: '目标节点',
})
m_ndTarget: cc.Node = null;
/** 组件数组 */
m_arrComponents: cc.Component[] = null;
/** 组件索引 */
@property({
    type: cc.Enum({}),
    visible() {
        if (!this.m_ndTarget && this.m_arrComponents !== null) {
            this.m_arrComponents = null;
            this.m_iComponent = -1
        }
        if (this.m_ndTarget && this.m_arrComponents === null) {
            this.m_arrComponents = this.m_ndTarget.getComponents(cc.Component);
            const enumComponents = {};
            const componentCount = {};
            let component = null;
            let className = '';
            for (let i = 0, length = this.m_arrComponents.length; i < length; i++) {
                component = this.m_arrComponents[i];
                className = cc.js.getClassName(component);
                if (componentCount[className] === undefined) {
                    componentCount[className] = 0;
                }
                else {
                    componentCount[className]++;
                    className += ' - ' + (componentCount[className] < 10 ? '00' : componentCount[className] < 100 ? '0' : '') + componentCount[className];
                }
                enumComponents[className] = i;
            }
            setEnumAttr(TweenTarget, 'm_iComponent', enumComponents);
            // cc.Class.attr(this, 'm_iComponent', {
            //     type: 'Enum',
            //     enumList: cc.Enum.getList(cc.Enum(enumComponents))
            // });
            // cc.Class.Attr.setClassAttr(this, 'm_iComponent', 'enumList', cc.Enum.getList(enumComponents));
        }
        return this.m_ndTarget;
    },
    displayName: 'Component',
    tooltip: '目标组件(节点上的组件)',
})
m_iComponent: number = -1;

如何只改变实例的属性的type呢

我之前没继承cc.Component,继承了cc.Component就可以只改变实例的属性的type了,尴尬

继承component后就不能当属性的类型了怎么办


image

皮皮大佬帮帮忙呀

编辑器支持的类型不应该继承 Component 哦

但不继承component的话
就不能cc.Class.Attr.setClassAttr(obj, propName, ‘type’, ‘Enum’);了
只能cc.Class.Attr.setClassAttr(class, propName, ‘type’, ‘Enum’);
这样所有new出来的实例的这个属性的type就都变了

emmmm,我也不太懂该咋整

好吧,谢谢大佬

第一个参数没有限制非得是什么基类。要传任意类型的实例,或者传整个类都可以。传入实例的话就不会影响到同一个 type 的其它对象了。