Open data domain (OpenDataContextView)

1. Create OpenDataContextView in LayaAir IDE

As shown in Figure 1-1, you can right-click in the 'Hierarchy' window to create it, or you can drag and drop from the 'Widgets' window to add it.

1-1

(Picture 1-1)

After adding the OpenDataContextView component to the view area of ​​the scene editor, the exclusive properties of the OpenDataContextView component in the properties panel are as shown below:

1-2

(Figure 1-2)

It has only one attribute, FPS, which represents sharedCanvas (an off-screen canvas that can be accessed by both the main domain and the open data domain, see here) updated to the frame rate of the main domain.

In addition to the IDE, you can also use script code to adjust its properties. In the property settings panel of Scene2D, add a custom component script. Then, drag the OpenDataContextView into its exposed property entry. Here is a sample code to implement script control of OpenDataContextView:

const { regClass, property } = Laya;

@regClass()
export class NewScript extends Laya.Script {
    //declare owner : Laya.Sprite3D;

    @property({ type: Laya.OpenDataContextView })
    public opendata: Laya.OpenDataContextView;

    constructor() {
        super();
    }

    /**
     * Executed after the component is activated. At this time, all nodes and components have been created. This method is only executed once.
     */
    onAwake(): void {
        this.opendata.pos(100,100);
        this.opendata.size(500,500);
    }
}

2. Code to create OpenDataContextView

Sometimes, you don't want the OpenDataContextView to be on the stage from the beginning, so you have to create it through code. In the property settings panel of Scene2D, add a custom component script. The sample code is as follows:

const { regClass, property } = Laya;

@regClass()
export class NewScript extends Laya.Script {
    //declare owner : Laya.Sprite3D;

    constructor() {
        super();
    }

    /**
     * Executed after the component is activated. At this time, all nodes and components have been created. This method is only executed once.
     */
    onAwake(): void {
        let opendata = new Laya.OpenDataContextView();
        Laya.stage.addChild(opendata);
        opendata.pos(100,100);
        opendata.size(500,500);
    }
}

3. Function and effect

The open data domain is generally used to display friend rankings. It needs to be published as a WeChat mini-game, and then the WeChat developer tools can be used to see the effect. For detailed procedures, please refer to "WeChat Mini Game".

Copyright ©Layabox 2022 all right reserved,powered by LayaAir EngineUpdate: 2024-09-18 20:09:12

results matching ""

    No results matching ""