# Generic Ad Manager

This Manager doesn't require any ad packages before using API. So you can leave AdManager calls in your game and then just add AdNetwork implementation in the end.

{% file src="<https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2F3P3UWsTqpNZdTlawl05o%2Fgeneric_ad_manager_v3.2.2%20(Alchemy).unitypackage?alt=media&token=77c6d8e8-ed9b-4045-8226-3cd9c92ecb75>" %}
Package (DOTween)
{% endfile %}

{% file src="<https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FMixoxHrQCh6dUR6Jp1bj%2Fgeneric_ad_manager_v3.2.3%20(Alchemy).unitypackage?alt=media&token=5a0731fe-624b-4a77-b881-375ef509e3c4>" %}
Package (PrimeTween)
{% endfile %}

## How to use

Replace AdManager.prefab in loader scene.

Add placement names to AdPlacements enum (in AdManager.cs file).\
for example `LevelContinue, RewardCoins`

{% hint style="warning" %} <mark style="color:green;">**// todo implement check ..**</mark>

Implement these lines in AdManager.cs where it's replaced. \
Every game has it's own logic for game number, first start (you could determine first start always if player didn't finish tutorial and etc).
{% endhint %}

Add AdNetwork implementation script as component to AdManager prefab in inspector and reference it to ActiveNetwork field. Use existing [#adnetwork-implementations](#adnetwork-implementations "mention")\
Or implement your custom one -> [implementing-custom-adnetwork](https://reddycat.gitbook.io/base-project-cube-doc/other/add-on-.unitypackage-archive/generic-ad-manager/implementing-custom-adnetwork "mention")

### Call ads

Call reward/inter/banner methods from AdManager.Instance where you need.

<pre class="language-csharp"><code class="lang-csharp"><strong>// AdManager Reward/Interstitials methods works with callbacks
</strong><strong>// and callback will be invoked even if there is no ads to keep game flow
</strong><strong>
</strong><strong>public void OnContinueButtonClick() {
</strong>    AdManager.Instance.ShowInter(AdPlacements.LevelContinue, LoadNextLevel);
}

private void LoadNextLevel() {
    // actions for new level load
}
</code></pre>

{% hint style="info" %}
You can pass string to ShowInter or Rewarded as well. But we recommend to use enum for consistency
{% endhint %}

### Use AdButton for rewards

You can use *<mark style="color:purple;">**AdButton.cs**</mark>* script with rewarded button (add it as new component to button object) to update it's interactible state (by default).

<figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FouXjc25dbEKrG51Vnwqv%2Fimage.png?alt=media&#x26;token=dc5a0638-d90f-44fe-901b-fb4febe19bc9" alt=""><figcaption><p>How it looks in inspector</p></figcaption></figure>

* Placement field is used with AdNetwork. Some ad networks use them to lock rewards for a while and for analytics.
* inherit it and override `OnRewardedReadyStateChanged(bool isReady)` to implement custom visuals change logic.
* You can change placement name runtime with ChangePlacement() method.

Or you can write code as below:

```csharp
// snippet how to toggle button state depending on reward ad ready

// to test button active state you need to run game from loader,
// because AdManager has reference to network, but if you start play from other scene
// so you have skipped loader scene
// then AdManager object will be created runtime without references
// (it's only case in editor while testing)

if (AdManager.Instance.IsEnabled) {
    if (AdManager.Instance.IsRewardedReady(AdPlacements.GetAdCoins)) {
        SetAdButtonActiveState();
    }
    else {
        // you can subscribe to reward ready event here
        // AdManager.OnRewardAdLoaded.AddListener(SetAdButtonActiveState);
        // then avoid listener dublicates if this can be called multiple times without this monobehaviour object destroy
        
        SetAdButtonDisabledState();
    }
}
else {
    SetAdButtonDisabledState();
}
```

### Use Interstitial coffee break ad

This is simillar to insterstitial ad, but with warning for player that soon will be ad.

<figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FbFtqRWeSxjPr5oOmfsMN%2Fimage.png?alt=media&#x26;token=f328bcee-d8b4-4a66-9a70-95af6c64b2d6" alt=""><figcaption><p>Default visuals for coffee break interstitial</p></figcaption></figure>

```csharp
// with callback example
AdManager.Instance.ShowInterCoffeeBreak(AdPlacements.LevelContinue, () => {
    // give player little reward or change level and etc
});

// do nothing after ad example
AdManager.Instance.ShowInterCoffeeBreak(AdPlacements.LevelContinue, null);
```

You can change ***AdCoffeeBreak.prefab*** visuals to fit your game.

### Interstitials delay between

{% hint style="warning" %}
Use **Ads Settings.asset** scriptable object to define ad cooldown / delays for intersitial (after rewarded, between interstitial, on app launch).

*<mark style="color:red;">**Note**</mark>*, that if rewarded ad watched -> interstitial cooldown will be overridden to cooldown value after rewarded ad.
{% endhint %}

{% hint style="success" %}
You can use \[[#unity-remote-config-wrapper](#unity-remote-config-wrapper "mention") ]\(unity-remote-config-wrapper)with this manager to retrieve ad settings from unity dashboard remote config page.

\
Setup:

* Add  \[[#unity-remote-config-wrapper](#unity-remote-config-wrapper "mention")]\(unity-remote-config-wrapper), don't remove it's default fields (AdDelays, EnableAd and etc)
* add **REMOTE\_CONFIG\_UNITY** script symbols in Player settings located "Other Settings" section.
  {% endhint %}

## AdNetwork implementations

{% file src="<https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2Fa0gqJDDSsuvf7LDnyzmQ%2Fapplovin_network_v3.0.unitypackage?alt=media&token=ed572f3b-7e80-430f-8002-a21e2d0b6e37>" %}
MAX AppLovin Ads
{% endfile %}

{% file src="<https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2Fqmlq04JM0VU3xp4KOEhu%2Fclever_ads_solutions_network_v3.0.unitypackage?alt=media&token=97197f82-1208-4e8a-904c-260bad9c7261>" %}
Clever Ads Solution
{% endfile %}

{% file src="<https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FXtcAx9e2wZ80gpPfUrIS%2Fappodeal_network_v3.2.unitypackage?alt=media&token=e910cd8d-9f05-4f51-9d36-679e98bac462>" %}
Appodeal
{% endfile %}
