如何动态设置属性的type

大佬,没有用呀

/** 目标节点 */
@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 的其它对象了。

但不继承cc.Component的话传实例就不会生效

不继承cc.Component的情况下传实例不会生效,传类会生效
继承cc.Component,传实例和类都会生效

抱歉没看懂,给个完整点的代码?

大佬帮忙看看

@ccclass('xxxx')
export class xxxx {
    aaa: boolean = false;
    @property({
        type: cc.Enum({}),
        visible() {
            if (!this.aaa) {
                this.aaa = true;
                //@ts-ignore //无效的,xxxx没有继承cc.Component所有setClassAttr的第一个参数填this无效,继承cc.Component才能生效
                cc.Class.Attr.setClassAttr(this, 'm_iComponent', 'enumList', cc.Enum.getList({ a: 0, b: 1 }));

                //@ts-ignore //有效的,无论xxxx是否继承cc.Component,setClassAttr的第一个参数填类都有效
                cc.Class.Attr.setClassAttr(xxxx, 'm_iComponent', 'enumList', cc.Enum.getList({ a: 0, b: 1 }));
            }
            return true;
        },
    })
    bbb: number = -1;
}

最后一个参数,应该是 cc.Enum.getList(cc.Enum({ a: 0, b: 1 })),enumDef 等价于原先 type: 定义的值

OK,我试试看

还是不行

    @ccclass('xxxx')
    export class xxxx {
        aaa: boolean = false;
        @property({
        type: cc.Enum({}),
        visible() {
            if (!this.aaa) {
                this.aaa = true;
                //@ts-ignore //无效的,xxxx没有继承cc.Component所有setClassAttr的第一个参数填this无效,继承cc.Component才能生效
                cc.Class.Attr.setClassAttr(this, 'm_iComponent', 'enumList', cc.Enum.getList({ a: 0, b: 1 }));

                //@ts-ignore //无效的,xxxx没有继承cc.Component所有setClassAttr的第一个参数填this无效,继承cc.Component才能生效
                cc.Class.Attr.setClassAttr(this, 'm_iComponent', 'enumList', cc.Enum.getList(cc.Enum({ a: 0, b: 1 })));

                //@ts-ignore //有效的,无论xxxx是否继承cc.Component,setClassAttr的第一个参数填类都有效
                cc.Class.Attr.setClassAttr(xxxx, 'm_iComponent', 'enumList', cc.Enum.getList({ a: 0, b: 1 }));
                
                //@ts-ignore //有效的,无论xxxx是否继承cc.Component,setClassAttr的第一个参数填类都有效
                cc.Class.Attr.setClassAttr(xxxx, 'm_iComponent', 'enumList', cc.Enum.getList(cc.Enum({ a: 0, b: 1 })));
            }
            return true;
        },
    })
    bbb: number = -1;
}

在吗大佬。