求助:如何动态创建内置预制体

  • Creator 版本:3.8.2

  • 目标平台:

  • 重现方式:

  • 首个报错:

  • 之前哪个版本是正常的:

  • 手机型号:

  • 手机浏览器:

  • 编辑器操作系统:

  • 重现概率:

我看到小伙伴动态创建一个button需要 create Node–> 添加组件 Button
我在internal里看到有内置Button.prefab。请问我想用代码创建Button.prefab 该如何操作

编辑器的api在上线发布后还能使用吗?或者说动态加载内置预制体本来就是错误的?只能拷贝成自定义预制体然后动态加载

啊?为啥会有 动态创建一个内置按钮节点的需求 不都是给一个节点添加按钮组件或者实例化一个自定义按钮的预制体吗 还是我哪里理解的不对 :thinking:

因为要动态复制的节点十分简单,只是一个button或者label,想要动态创建每次都要create Node–> addComponent(xxxx)。想用resources.load这种方式加载好像只能处理自定义的。不过 在Component子类里 @property(Prefab) buttonTest: Prefab = null!; let button = instantiate(this.buttonTest) 是可以动态创建的。

抑或有类似的全局变量可以instantiate(xxxx)

自己写个工具函数呗

  /**
     * @description 创建新的节点
     * @param options
     * @returns
     */
    public static createNode(options?: NodeCreateOptions): cc.Node {
        options ??= NodeCreateOptions.create();
        options.nodeName ??= `NODE_${StringUtils.uuid()}`;
        let newNode = new cc.Node(options.nodeName);
        for (const component of options.components) {
            newNode.addComponent(component);
        }

        let uiTransform = newNode.getComponent(cc.UITransform);
        if (uiTransform) {
            uiTransform.setContentSize(options.width, options.height);
        }

        newNode.layer = options.uiLayer;
        newNode.parent = options.parent;
        return newNode;
    }


export class NodeCreateOptions {
    public nodeName?: string;

    public width?: number = 0;

    public height?: number = 0;

    public parent?: cc.Node;

    public components?: Array<CCComponent>;

    public uiLayer?: cc.Layers.Enum = cc.Layers.Enum.UI_3D;

    public static create(): NodeCreateOptions {
        let nodeCreateOptions = new NodeCreateOptions();
        return nodeCreateOptions;
    }
}
        this._uiLockNode = NodeHelper.createNode({
            parent: this._uiLayerNode[UILayer.System],
            components: [cc.BlockInputEvents],
            nodeName: 'UI_LOCK_NODE',
            width: cc.screen.windowSize.width,
            height: cc.screen.windowSize.height,
        });

只能如此了,想着是有内置的看看不可以省一下

你可以多定义几个模板,可以方便的调用image