In-App Ads for Cloud Games

InAppAdLibrary

Contains the functions related to Ads. Besides the APIs, the set up will be the same as for Instant Games. You will need to create an Ad Space and copy the placementID from the Monetization Manager. See the following guides: set-up, guidelines, and best practices.


Please note that callbacks are currently not supported when testing In-App-Ads on local testkit.

Also, please mute the game audio when an ad is being shown.

DaemonRequest.Callback

The callback expected by each library function returns a standard GraphResponse information, with additional information if the request was unsuccessful.

public interface Callback {
  /**
    * The method that will be called when a request completes.
    *
    * @param response    the Response of this request, which may include
    * error information if the request was unsuccessful
    */
  void onCompleted(GraphResponse response);
}

Example

DaemonRequest.Callback callback =
  new DaemonRequest.Callback() {
    @Override
    public void onCompleted(GraphResponse response) {
      if (response == null) {
        // handle here
      } else if (response.getError() != null) {
        // handle here
      } else if (response.getJSONObject() != null) {
        // handle here
      }
    }
  };

loadRewardedVideo()

/**
 * Sets a callback to be triggered after the rewarded video is loaded. This MUST be called before
 * showRewardedVideo().
 *
 * @param context the application context
 * @param placementID the placement ID of the ad
 * @param callback callback for success and error
 */
public static void loadRewardedVideo(
      Context context, String placementID, DaemonRequest.Callback callback)

Callback

If successful, response.getJSONObject() returns an empty object {}.

Example

InAppAdLibrary.loadRewardedVideo(getApplicationContext(), "111111111111111_222222222222222", callback);

In the callback, response.getJSONArray() will return:

{}

showRewardedVideo()

/**
 * Sets a callback to be triggered after the rewarded video is shown. This can only be called
 * after the loadRewardVideo() returns successfully.
 *
 * @param context the application context
 * @param placementID the placement ID of the ad
 * @param callback callback for success and error
 */
public static void showRewardedVideo(
      Context context, String placementID, DaemonRequest.Callback callback)

This should only be called after a successful callback from an earlier loadRewardedVideo() call.

Callback

If successful, response.getJSONObject() returns an empty object {}.

Example

InAppAdLibrary.showRewardedVideo(getApplicationContext(), "111111111111111_222222222222222", callback);

In the callback, response.getJSONArray() will return:

{}

loadInterstitialAd()

/**
 * Sets a callback to be triggered after the interstitial ad is loaded. This MUST be called before
 * showInterstitialAd().
 *
 * @param context the application context
 * @param placementID the placement ID of the ad
 * @param callback callback for success and error
 */
public static void loadInterstitialAd(
      Context context, String placementID, DaemonRequest.Callback callback)

Callback

If successful, response.getJSONObject() returns an empty object {}.

Example

InAppAdLibrary.loadInterstitialAd(getApplicationContext(), "111111111111111_333333333333333", callback);

In the callback, response.getJSONArray() will return:

{}

showInterstitialAd()

/**
 * Sets a callback to be triggered after the interstitial ad is shown. This can only be called
 * after the loadInterstitialAd() returns successfully.
 *
 * @param context the application context
 * @param placementID the placement ID of the ad
 * @param callback callback for success and error
 */
public static void showInterstitialAd(
      Context context, String placementID, DaemonRequest.Callback callback)

This should only be called after a successful callback from an earlier loadInterstitialAd() call.

Callback

If successful, response.getJSONObject() returns an empty object {}.

Example

InAppAdLibrary.showInterstitialAd(getApplicationContext(), "111111111111111_333333333333333", callback);

In the callback, response.getJSONArray() will return:

{}

Error Handling

A FacebookRequestError response of type INVALID_OPERATION from the DaemonRequest.Callback is possible for each function and may be indicative of a duplicate of an unresolved request, failing to process the request, or that the requested operation is invalid for the current game state. See the associated error message for more details.

Error Codes

CodeTypeDescription

-1

INVALID_OPERATION

Indicative of a duplicate of an unresolved request, failing to process the request, or that the requested operation is invalid for the current game state. See the associated error message for more details

2581001

CLIENT_UNSUPPORTED_OPERATION

The client does not support the current operation. This may be due to lack of support on the client version or platform, or because the operation is not allowed for the game or player.

2581003

NETWORK_FAILURE

The user made a choice that resulted in a rejection. For example, if the game calls up the Context Switch dialog and the player closes it, this error code will be included in the promise rejection.

2581004

USER_INPUT

The user made a choice that resulted in a rejection. For example, if the game calls up the Context Switch dialog and the player closes it, this error code will be included in the promise rejection.

2581005

INVALID_OPERATION

The requested operation is invalid for the current game state. This may include requests that violate limitations, such as exceeding storage thresholds, or are not available in a certain state, such as making a context-specific request in a solo context.

2581006

INVALID_PARAM

The parameter(s) passed to the API are invalid. Could indicate an incorrect type, invalid number of arguments, or a semantic issue (for example, passing an unserializable object to a serializing function).

Unity SDK


LoadRewardedVideo()

public static void LoadRewardedVideo (string placementID, FacebookDelegate<IRewardedVideoResult> callback);

Callback

The callback takes IRewardedVideoResult result as a parameter.

If successful,

result.Error == null

ShowRewardedVideo()

public static void ShowRewardedVideo (string placementID, FacebookDelegate<IRewardedVideoResult> callback);

This should only be called after a successful callback from an earlier LoadRewardedVideo() call.

Callback

The callback takes IRewardedVideoResult result as a parameter.

If successful,

result.Error == null

Check the following to trigger behavior if the user skips the rewarded video early and does not wait the full duration:

if (result.Cancelled == true)
{
	// do something
}

LoadInterstitialAd()

public static void LoadInterstitialAd (string placementID, FacebookDelegate<IInterstitialAdResult> callback);

The callback takes IInterstitialAdResult result as a parameter.

If successful,

result.Error == null

ShowInterstitialAd()

public static void ShowInterstitialAd (string placementID, FacebookDelegate<IInterstitialAdResult> callback);

This should only be called after a successful callback from an earlier LoadInterstitialAd() call.

The callback takes IInterstitialAdResult result as a parameter.

If successful,

result.Error == null

Code Sample

  
// Creating store UI  
private void Populate()
{
  ...
  
  FBGamingServices.LoadRewardedVideo(REWARDED_VIDEO_PLACEMENT_ID, delegate (IRewardedVideoResult result)
  {
    if (result.Error == null && result.ResultDictionary != null)
    {
      GameObject newEntry = Instantiate(prefabItem);
      newEntry.transform.SetParent(listRoot, false);

      PremiumListItem item = newEntry.GetComponent<PremiumListItem>();

      item.buyButton.image.sprite = item.buyButtonSprite;

      item.nameText.text = "5 Coins";
      item.priceText.text = "Video Ad";

      item.buyButton.GetComponentsInChildren<Text>()[0].text = ">";
      item.buyButton.onClick.AddListener(delegate () { WatchRewardedVideo(newEntry, 5); });
    }
    else
    {
      Debug.Log("Could not load ad succesfully.");
    }
  });
}
  
private void WatchRewardedVideo(GameObject obj, int numCoins)
{
  FBGamingServices.ShowRewardedVideo(REWARDED_VIDEO_PLACEMENT_ID, delegate (IRewardedVideoResult result)
  {
    if (result.Error == null && result.ResultDictionary != null)
    {
      // Awarding premium coins
      PlayerData.instance.premium += numCoins;
      PlayerData.instance.Save();
    }
    else
    {
      Debug.Log("Rewarded Video failed to be shown or user cancelled early.");
    }

    // Reload
    FBGamingServices.LoadRewardedVideo(REWARDED_VIDEO_PLACEMENT_ID, delegate (IRewardedVideoResult reloadResult)
    {
      if (reloadResult.Error != null || reloadResult.ResultDictionary == null)
      {
        // Remove item
        Destroy(obj);
        Debug.Log("Failed to reload Rewarded Video");
      }
    });
  });
}