Search Results for

Show / Hide Table of Contents

# Building Native Ad Request

##### Note

Make sure you've done SDK Initialization before attempting to load any ads.

## Long story short

- Get a populated AdUnitData.
- Call an extension method BuildAdLoader(AdUnitData).

    _nativeAdLoader = nativeConfig.BuildAdLoader();
    _nativeAdLoader.LoadAd(OnAdLoadResult);

##### Tip

The resulting INativeAdLoader has LoadAd(Action<INativeAd?, AdError?>) you needed.

Now you can get back to step 2 of How to do Native Ads.

## NativeConfigPreset asset

AdUnitData has SerializableAttribute.

Which means you can have it edited via Unity Editor.

Most of the data is pretty static, so the logical thing to do is wrap AdUnitData into a ScriptableObject.

The "Native" sample has a `NativeConfigPreset` for that:

    using System;
    using AppstockSDK.Api;
    using UnityEngine;
    using AppstockSDK.Api.Native.Data.Request;

    namespace AppstockSDK.Demo.Native
    {
        [Serializable]
        [CreateAssetMenu(fileName = "NativeConfigPreset", menuName = "Appstock/Native Config Preset")]
        public class NativeConfigPreset : ScriptableObject
        {
            public ConfigWarnings warnings;
            public AdUnitData adUnitData;

            public INativeAdLoader BuildAdLoader()
                => adUnitData.BuildAdLoader();

        }
    }

Once imported, you can create assets of this class via respective menu entry (highlighted above).

ConfigWarnings serves as an effectively-readonly container to display any errors directly in inspector during the editing process.

## Placement/Endpoint ID

Fill in placementID or endpointID with a value generated on the Appstock SDK platform's UI.

## Ad Parameters

Fill in the Parameters to the best of your abilities.

##### Tip

Both AdUnitData and Parameters are structs.

This allows you to reuse a single `NativeConfigPreset` asset for different placements or contexts if needed.

### Ad Assets

assets (of type Assets) has 3 homogeneous arrays for each type of assets:

Asset Type | Config Samples
--- | ---
AssetTitle[] |
AssetImage[] |
AssetData[] |

##### Note

If all of the array are null or empty, the combined `assets` field will not be written to in native library (by default) and the warning is displayed in inspector.

allowEmpty lets you override this behaviour and intentionally pass the empty array down into native library (rather than keep default which most likely is `null`).

### Ext Slots

Some data structures expose public fields of type ExtSlot.

See Ext Slot page for more info.

## Using data from asset to load ad

##### Tip

You can find a functional code in "Native" sample.

### Declaring variables

Expose `NativeConfigPreset` inspectable variable on the relevant MonoBehaviour.

Prepare INativeAdLoader private variable.

    #nullable enable

    namespace AppstockSDK.Demo.Native
    {
        public class NativeDemo : MonoBehaviour
        {
            [SerializeField] private NativeConfigPreset? nativeConfig;
            [SerializeField] private TMP_Text? text;
            private INativeAdLoader? _nativeAdLoader;
            private INativeAd? _nativeAd;

`text` is the sample is used for displaying the content of INativeAd. It is for illustration purposes only.

`_nativeAd` will be used later (once received).

### Loading the ad

Build the ad loader.

    _nativeAdLoader = nativeConfig.BuildAdLoader();

In this case the convenience method by scriptable object was used.

    public INativeAdLoader BuildAdLoader()
        => adUnitData.BuildAdLoader();

If you need to adjust the data before building the loader, use a local variable, e.g.:

    AdUnitData dataCopy = nativeConfig.adUnitData;
    dataCopy.placementID = "99";
    dataCopy.parameters.sequence = 17;
    _nativeAdLoader = dataCopy.BuildAdLoader();

Load the ad.

    _nativeAdLoader.LoadAd(OnAdLoadResult);

The signature of `OnAdLoadResult` callback method is:

    private void OnAdLoadResult(INativeAd? nativeAd, AdError? status)

If it failed to load, `nativeAd` will be null and `status` may contain an error message.

##### Warning

Do not check if `status` is null to determine if ad has loaded.

`status` may contain success message.

If the ad did successfully load, `nativeAd` will be not null.

##### Tip

For applying the received data please continue reading Applying Native Ad Response.