Skip to content
AssetUtil

Utils / AssetUtil

AssetUtil Class

Asset management tools

When using the resources in the left toolbar, it is necessary to download and load them in advance.

The asyncDownloadAsset interface can be used to dynamically download corresponding resources in the code. Resources can also be manually dragged into the priority loading queue on the right side of the editor.

Table of contents

Methods

assetLoaded(InAssetId: string): boolean other
Is the resource loaded
asyncDownloadAsset(InAssetId: string): Promise<boolean> other
Download and load resources
isLocalMaterialAsset(InAssetId: string): boolean other
Judge whether the material asset is a local asset

Methods

assetLoaded

Static assetLoaded(InAssetId): boolean other

Is the resource loaded

Parameters

InAssetId stringUsage: Resource ID range: The string size depends on the resource ID

Returns

booleanNot loaded will return false

Usage example: create a script named AssetExample, place it in the object bar, open the script, change the original content to the following, save and run the game, and the log will output whether the 7711 asset id is loaded, and generate a stair at the origin after the asset is loaded

ts
@Component
export default class AssetExample extends Script {

    protected onStart(): void {
        const cubeAssetId = "7711";
        if (AssetUtil.assetLoaded(cubeAssetId)) {
            console.log("AssetExample: Cube asset is already loaded.");
            let obj = GameObject.spawn({ guid: cubeAssetId });
            obj.worldTransform.position = new Vector(0, 0, 0);
        } else {
            console.log("AssetExample: Cube asset is not loaded, downloading...");
            AssetUtil.asyncDownloadAsset(cubeAssetId).then(() => {
                let obj = GameObject.spawn({ guid: cubeAssetId });
                obj.worldTransform.position = new Vector(0, 0, 0);
            });
        }
    }
}
@Component
export default class AssetExample extends Script {

    protected onStart(): void {
        const cubeAssetId = "7711";
        if (AssetUtil.assetLoaded(cubeAssetId)) {
            console.log("AssetExample: Cube asset is already loaded.");
            let obj = GameObject.spawn({ guid: cubeAssetId });
            obj.worldTransform.position = new Vector(0, 0, 0);
        } else {
            console.log("AssetExample: Cube asset is not loaded, downloading...");
            AssetUtil.asyncDownloadAsset(cubeAssetId).then(() => {
                let obj = GameObject.spawn({ guid: cubeAssetId });
                obj.worldTransform.position = new Vector(0, 0, 0);
            });
        }
    }
}

asyncDownloadAsset

Static asyncDownloadAsset(InAssetId): Promise<boolean> other

Download and load resources

Parameters

InAssetId stringUsage: asset ID range: string size depends on asset ID

Returns

Promise<boolean>Download failure will return false

This function can only pass in the asset ID

Usage example: create a script named AssetExample, place it in the object bar, open the script, change the original content to the following, save and run the game, and the log will output whether the 7711 asset id is loaded, and generate a stair at the origin after the asset is loaded

ts
@Component
export default class AssetExample extends Script {

    protected onStart(): void {
        const cubeAssetId = "7711";
        if (AssetUtil.assetLoaded(cubeAssetId)) {
            console.log("AssetExample: Cube asset is already loaded.");
            let obj = GameObject.spawn({ guid: cubeAssetId });
            obj.worldTransform.position = new Vector(0, 0, 0);
        } else {
            console.log("AssetExample: Cube asset is not loaded, downloading...");
            AssetUtil.asyncDownloadAsset(cubeAssetId).then(() => {
                let obj = GameObject.spawn({ guid: cubeAssetId });
                obj.worldTransform.position = new Vector(0, 0, 0);
            });
        }
    }
}
@Component
export default class AssetExample extends Script {

    protected onStart(): void {
        const cubeAssetId = "7711";
        if (AssetUtil.assetLoaded(cubeAssetId)) {
            console.log("AssetExample: Cube asset is already loaded.");
            let obj = GameObject.spawn({ guid: cubeAssetId });
            obj.worldTransform.position = new Vector(0, 0, 0);
        } else {
            console.log("AssetExample: Cube asset is not loaded, downloading...");
            AssetUtil.asyncDownloadAsset(cubeAssetId).then(() => {
                let obj = GameObject.spawn({ guid: cubeAssetId });
                obj.worldTransform.position = new Vector(0, 0, 0);
            });
        }
    }
}

isLocalMaterialAsset

Static isLocalMaterialAsset(InAssetId): boolean other

Judge whether the material asset is a local asset

Parameters

InAssetId stringUsage: Resource ID range: The string size depends on the resource ID

Returns

booleanLocal asset when true