Skip to content

sud.createBannerAd ​

Creates a banner ad component. Each call to this method returns a brand-new banner ad instance. The ad loads automatically after creation; load success is reported through BannerAd.onLoad(), and load or display failures are reported through BannerAd.onError().

typescript
createBannerAd(params: {
    adUnitId: string;
    style: {
        left: number;
        top: number;
        width: number;
        height: number;
    };
    /** Interval for automatic ad refresh, in seconds. The value must be greater than or equal to 30 (reserved parameter, not yet in effect) */
    adIntervals?: number;
}): BannerAd;

Banner auto-loading is rate-limited (currently triggered by repeated requests within 10 seconds). When the limit is hit, BannerAd.onError() returns the error code (errCode) and message (errMsg).

Parameters ​

Object object ​

PropertyTypeDefaultRequiredDescription
adUnitIdstringYesAd unit ID configured in the SUD OpenPaaS console
styleObjectYesPosition and size of the maximum available container for the banner ad
adIntervalsnumberNoInterval for automatic ad refresh, in seconds. The value must be greater than or equal to 30 (reserved parameter, not yet in effect)

Structure of object.style ​

PropertyTypeDefaultRequiredDescription
leftnumberYesX coordinate of the top-left corner of the maximum available container, in logical pixels
topnumberYesY coordinate of the top-left corner of the maximum available container, in logical pixels
widthnumberYesMaximum available width, must be greater than 0
heightnumberYesMaximum available height, must be greater than 0

width / height define the maximum available area. The ad is centered horizontally and aligned to the top within that area, without stretching or cropping; left / top are the coordinates of its top-left corner.

Sizes are in logical pixels (dp on Android, pt on iOS). Container dimensions and pixelRatio are available from sud.getContainerInfo; set the banner area with logical width and height directly, with no need to multiply or divide by pixelRatio. The actual size reported by BannerAd.onResize() needs no conversion either.

Return Value ​

BannerAd

Banner ad component.

style.width / style.height define the maximum available area, while style.realWidth / style.realHeight are the read-only actual display size and are 0 until the first successful display. Read the actual size when BannerAd.onResize() fires; the maximum available width and height stay unchanged.

Example Code ​

Create the banner ad on scene entry and display it after the BannerAd.onLoad() notification. This full-screen game example uses sud.getContainerInfo to reserve a top area as wide as the container and at most 100 logical pixels high. Keep game UI outside that area. For split-screen or embedded games, determine the reserved area from the actual game layout.

javascript
const { containerWidth, containerHeight } = sud.getContainerInfo();

const bannerAd = sud.createBannerAd({
    adUnitId: 'adunit-xxxxxxxx',
    style: {
        left: 0,
        top: 0,
        // Use logical dimensions directly for the reserved area.
        width: Math.floor(containerWidth),
        height: Math.min(100, Math.floor(containerHeight)),
    },
    // Optional parameter; reserved, not yet in effect
    adIntervals: 30,
});

let disposed = false;
let showRequested = false;

function onLoad() {
    if (disposed || showRequested) return;
    showRequested = true;
    bannerAd.show().catch(onError);
}

function onError(err) {
    if (disposed) return;
    console.error('Banner ad failed', err);
    onBannerSceneExit();
}

function onResize({ width, height }) {
    if (disposed) return;
    // Matches bannerAd.style.realWidth / realHeight; use it to adjust surrounding game UI.
    console.log('Banner actual size', width, height);
}

function onBannerSceneExit() {
    if (disposed) return;
    disposed = true;
    bannerAd.offLoad(onLoad);
    bannerAd.offError(onError);
    bannerAd.offResize(onResize);
    bannerAd.destroy();
}

bannerAd.onLoad(onLoad);
bannerAd.onError(onError);
bannerAd.onResize(onResize);

The reserved area is as wide as the container, and the ad is centered horizontally and top-aligned within it, so it appears centered on screen. Bind onBannerSceneExit() to the scene exit and call it even if the ad is still loading; once destroyed, a late load callback can no longer call show().