Facebook Rewarded Ads Unity Tutorial
In this video tutorial, we’ll walk you through the process of integrating Facebook Rewarded Ads into your Unity project. We’ll start by importing the necessary SDK, then we’ll write a simple script to implement the ads functionality. Finally, we’ll demonstrate how to display the ads in your game, ensuring you understand each step along the way.
WATCH FULL TUTORIAL ON YOUTUBE
=========================================================
3 Unity Projects Only $50 - ON SALE!!!!
Contact: unitycoderz2022@gmail.com
=========================================================
Script:
FacebookRewarded.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AudienceNetwork;
public class FacebookRewarded : MonoBehaviour
{
public string rewardedID = "";
private bool isLoaded;
private RewardedVideoAd rewardedVideoAd;
// Start is called before the first frame update
void Start()
{
LoadRewardedVideo();
}
public void LoadRewardedVideo()
{
// Create the rewarded video unit with a placement ID (generate your own on the Facebook app settings).
// Use different ID for each ad placement in your app.
this.rewardedVideoAd = new RewardedVideoAd(rewardedID);
this.rewardedVideoAd.Register(this.gameObject);
// Set delegates to get notified on changes or when the user interacts with the ad.
this.rewardedVideoAd.RewardedVideoAdDidLoad = (delegate () {
Debug.Log("RewardedVideo ad loaded.");
this.isLoaded = true;
});
this.rewardedVideoAd.RewardedVideoAdDidFailWithError = (delegate (string error) {
Debug.Log("RewardedVideo ad failed to load with error: " + error);
LoadRewardedVideo();
});
this.rewardedVideoAd.RewardedVideoAdWillLogImpression = (delegate () {
Debug.Log("RewardedVideo ad logged impression.");
});
this.rewardedVideoAd.RewardedVideoAdDidClick = (delegate () {
Debug.Log("RewardedVideo ad clicked.");
});
this.rewardedVideoAd.RewardedVideoAdDidClose = (delegate () {
Debug.Log("Rewarded video ad did close.");
if (this.rewardedVideoAd != null)
{
this.rewardedVideoAd.Dispose();
}
LoadRewardedVideo();
});
this.rewardedVideoAd.RewardedVideoAdDidSucceed = (delegate () {
Debug.Log("Rewarded video ad validated by server");
//HERE GOES THE REWARD
});
this.rewardedVideoAd.RewardedVideoAdDidFail = (delegate () {
Debug.Log("Rewarded video ad not validated, or no response from server");
});
// Initiate the request to load the ad.
this.rewardedVideoAd.LoadAd();
}
public void ShowRewardedVideo()
{
if (this.isLoaded)
{
this.rewardedVideoAd.Show();
this.isLoaded = false;
}
else
{
Debug.Log("Ad not loaded. Click load to request an ad.");
}
}
}