Search Unity

Question Help Needed for Facebook Audience Network Integration

Discussion in 'Scripting' started by Only4gamers, Jan 19, 2021.

  1. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Hello everyone,
    I am integrating Facebook Audience Network in my Android Game.

    I am using FAN to load rewarded ad after game start (ASAP) then show it to user when user click a button. Did you used FAN? I have few doubts:
    1. How to verify that user watched full ad and then reward user. Without using Server Side Reward Validation.
    According to FAN Docs for Server Side Reward Validation:
    "This is optional! You don't have to implement server side reward validation to make use of rewarded video ads. This is only required if you decide to validate rewards on your own server to improve the security by introducing a validation step at your own server. Please provide your publisher end point to your Facebook representative in order to enable this feature."

    2. Do I need to load ad again after an ad served? or it automatically load itself?

    I just hate all Official Docs. And there is absolutely no user generated content I can find in google related to FAN Integration.
    Here is official doc:
    https://developers.facebook.com/docs/audience-network/guides/ad-formats/rewarded-video/unity

    This is my script so far. But I am not sure if it's all right. I tested it on Android device and ad is not loading. If someone can please share your complete script for rewarded ad then it will be very helpful.
    Code (CSharp):
    1.  
    2. using UnityEngine.SceneManagement;
    3. using UnityEngine;
    4. using System.Collections;
    5. using AudienceNetwork;
    6.  
    7. namespace MoreMountains.InfiniteRunnerEngine
    8. {
    9.  
    10. public class InitializeAdsScript : MonoBehaviour
    11. {
    12.     public GameObject popUpAdFailedToLoad;
    13.     public GameObject checkYourInternet;
    14.     public GameObject popUpAddEnergy;
    15.     public Canvas canvas;
    16.     GameObject mainMenuPage;
    17.     public static bool isEnergyPage = false;
    18.  
    19.  
    20.     private RewardedVideoAd rewardedVideoAd;
    21.     private bool isLoaded;
    22.  
    23.     private void Awake()
    24.     {
    25.         AudienceNetworkAds.Initialize();
    26.     }
    27.  
    28.     void Start()
    29.     {
    30.         DontDestroyOnLoad(this.gameObject);
    31.         DontDestroyOnLoad(canvas);
    32.         LoadRewardedVideo();
    33.     }
    34.  
    35.     public void LoadRewardedVideo()
    36.     {
    37.         // Create the rewarded video unit with a placement ID (generate your own on the Facebook app settings).
    38.         // Use different ID for each ad placement in your app.
    39.         rewardedVideoAd = new RewardedVideoAd("my placement id"); // Placement ID
    40.  
    41.         rewardedVideoAd.Register(gameObject);
    42.  
    43.         // Set delegates to get notified on changes or when the user interacts with the ad.
    44.         rewardedVideoAd.RewardedVideoAdDidLoad = (delegate() {
    45.             Debug.Log("RewardedVideo ad loaded.");
    46.             isLoaded = true;
    47.         });
    48.         rewardedVideoAd.RewardedVideoAdDidFailWithError = (delegate(string error) {
    49.             popUpAdFailedToLoad.SetActive(false);
    50.             Debug.Log("RewardedVideo ad failed to load with error: " + error);
    51.         });
    52.         rewardedVideoAd.RewardedVideoAdWillLogImpression = (delegate() {
    53.             Debug.Log("RewardedVideo ad logged impression.");
    54.         });
    55.         rewardedVideoAd.RewardedVideoAdDidClick = (delegate() {
    56.             Debug.Log("RewardedVideo ad clicked.");
    57.         });
    58.  
    59.         rewardedVideoAd.RewardedVideoAdDidSucceed = delegate ()
    60.         {
    61. // Rewarding User here. Is this right?
    62.             if(LevelManager.boolAddLife && !(SceneManager.GetActiveScene().name == "LevelSelection"))
    63.             {
    64.                 GameManager.Instance.SetLives(3);
    65.             }
    66.             else
    67.             {
    68.                 PlayerPrefs.SetInt("TotalEnergyLeft", PlayerPrefs.GetInt("TotalEnergyLeft")+10);
    69.                 PlayerPrefs.Save();
    70.             }
    71.             if(SceneManager.GetActiveScene().name == "LevelSelection")
    72.             {
    73.                 mainMenuPage.SetActive(true);
    74.             }
    75.             Debug.Log("Rewarded video ad validated by server");
    76.         };
    77.  
    78.         rewardedVideoAd.RewardedVideoAdDidFail = delegate ()
    79.         {
    80.             popUpAdFailedToLoad.SetActive(true);
    81.             Debug.Log("Rewarded video ad not validated, or no response from server");
    82.         };
    83.  
    84.         rewardedVideoAd.RewardedVideoAdDidClose = (delegate() {
    85.             Debug.Log("Rewarded video ad did close.");
    86.             if (rewardedVideoAd != null) {
    87.                 rewardedVideoAd.Dispose();
    88.             }
    89.         });
    90.  
    91.         // Initiate the request to load the ad.
    92.         rewardedVideoAd.LoadAd();
    93.     }
    94.  
    95.     public virtual void OnClickWatchAd()
    96.     {
    97.         if (isLoaded)
    98.         {
    99.             rewardedVideoAd.Show();
    100.             isLoaded = false;
    101.         } else
    102.         {
    103.             checkYourInternet.SetActive(true);
    104.             Debug.Log("Ad not loaded. Click load to request an ad.");
    105.         }
    106.         popUpAddEnergy.SetActive(false);
    107.     }
    108. }
    109. }
     
    Last edited: Jan 20, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    That error has a LOT of google responses in the context of FAN. Rather than us googling it for you and laboriously asking you if you have tried each one, let me do it in aggregate: what have you tried from the googling you have already done?
     
    Only4gamers and Joe-Censored like this.
  3. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Actually some scripts included in FAN only works in Android device, that's why I am getting this error in Unity Editor. So, above error not matter now. I am updated my post for updated problems and doubts. Please take a look.
     
    Last edited: Jan 20, 2021
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    This is usually handled by conditional compilation, to disable code that is not supported:

    https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

    Fair enough, but not sure what the alternative is. I have not used FAN personally but I know that some people make it work. Perhaps one of them can chime in, or perhaps you can look on the Facebook tech forums for more info.
     
    Only4gamers likes this.
  5. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Yeah, Thank you so much for help. I am contacting FAN support. I hope they will solve my problem ASAP.