Untitled

 avatar
unknown
plain_text
3 years ago
9.6 kB
2
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.EventSystems;

#if UNITY_STANDALONE || IRON_SRC
public enum ShowResult
{
    Finished,
    Skipped,
    Failed
}
#endif


public class PlayAd : MonoBehaviour
{
    System.Action<ShowResult> m_Callback = null;
    EventSystem m_EventSystem = null;

//#if IRON_SRC
    bool rewardedVideoAvailability;

    void OnApplicationPause(bool isPaused)
    {
        IronSource.Agent.onApplicationPause(isPaused);
    }

    void Awake()
    {
        IronSource.Agent.init(GameGlobals.IRON_SOURCE_APP_ID, IronSourceAdUnits.REWARDED_VIDEO, IronSourceAdUnits.INTERSTITIAL);
        IronSource.Agent.validateIntegration();
    }

    void Start()
    {
        IronSourceEvents.onRewardedVideoAdOpenedEvent += RewardedVideoAdOpenedEvent;
        IronSourceEvents.onRewardedVideoAdClosedEvent += RewardedVideoAdClosedEvent;
        IronSourceEvents.onRewardedVideoAvailabilityChangedEvent += RewardedVideoAvailabilityChangedEvent;
        IronSourceEvents.onRewardedVideoAdStartedEvent += RewardedVideoAdStartedEvent;
        IronSourceEvents.onRewardedVideoAdEndedEvent += RewardedVideoAdEndedEvent;
        IronSourceEvents.onRewardedVideoAdRewardedEvent += RewardedVideoAdRewardedEvent;
        IronSourceEvents.onRewardedVideoAdShowFailedEvent += RewardedVideoAdShowFailedEvent;

        IronSourceEvents.onInterstitialAdReadyEvent += InterstitialAdReadyEvent;
        IronSourceEvents.onInterstitialAdLoadFailedEvent += InterstitialAdLoadFailedEvent;
        IronSourceEvents.onInterstitialAdShowSucceededEvent += InterstitialAdShowSucceededEvent;
        IronSourceEvents.onInterstitialAdShowFailedEvent += InterstitialAdShowFailedEvent;
        IronSourceEvents.onInterstitialAdClickedEvent += InterstitialAdClickedEvent;
        IronSourceEvents.onInterstitialAdOpenedEvent += InterstitialAdOpenedEvent;
        IronSourceEvents.onInterstitialAdClosedEvent += InterstitialAdClosedEvent;
    }

    void OnDestroy()
    {
        IronSourceEvents.onRewardedVideoAdOpenedEvent -= RewardedVideoAdOpenedEvent;
        IronSourceEvents.onRewardedVideoAdClosedEvent -= RewardedVideoAdClosedEvent;
        IronSourceEvents.onRewardedVideoAvailabilityChangedEvent -= RewardedVideoAvailabilityChangedEvent;
        IronSourceEvents.onRewardedVideoAdStartedEvent -= RewardedVideoAdStartedEvent;
        IronSourceEvents.onRewardedVideoAdEndedEvent -= RewardedVideoAdEndedEvent;
        IronSourceEvents.onRewardedVideoAdRewardedEvent -= RewardedVideoAdRewardedEvent;
        IronSourceEvents.onRewardedVideoAdShowFailedEvent -= RewardedVideoAdShowFailedEvent;

        IronSourceEvents.onInterstitialAdReadyEvent -= InterstitialAdReadyEvent;
        IronSourceEvents.onInterstitialAdLoadFailedEvent -= InterstitialAdLoadFailedEvent;
        IronSourceEvents.onInterstitialAdShowSucceededEvent -= InterstitialAdShowSucceededEvent;
        IronSourceEvents.onInterstitialAdShowFailedEvent -= InterstitialAdShowFailedEvent;
        IronSourceEvents.onInterstitialAdClickedEvent -= InterstitialAdClickedEvent;
        IronSourceEvents.onInterstitialAdOpenedEvent -= InterstitialAdOpenedEvent;
        IronSourceEvents.onInterstitialAdClosedEvent -= InterstitialAdClosedEvent;
    }

    //Invoked when the RewardedVideo ad view has opened.
    //Your Activity will lose focus. Please avoid performing heavy 
    //tasks till the video ad will be closed.
    void RewardedVideoAdOpenedEvent()
    {
    }
    //Invoked when the RewardedVideo ad view is about to be closed.
    //Your activity will now regain its focus.
    void RewardedVideoAdClosedEvent()
    {
    }
    //Invoked when there is a change in the ad availability status.
    //@param - available - value will change to true when rewarded videos are available. 
    //You can then show the video by calling showRewardedVideo().
    //Value will change to false when no videos are available.
    void RewardedVideoAvailabilityChangedEvent(bool available)
    {
        //Change the in-app 'Traffic Driver' state according to availability.
        rewardedVideoAvailability = available;
    }
    //Invoked when the video ad starts playing.
    void RewardedVideoAdStartedEvent()
    {
    }
    //Invoked when the video ad finishes playing.
    void RewardedVideoAdEndedEvent()
    {
        if (m_EventSystem)
            m_EventSystem.enabled = true;
    }
    //Invoked when the user completed the video and should be rewarded. 
    //If using server-to-server callbacks you may ignore this events and wait for 
    //the callback from the ironSource server.
    //@param - placement - placement object which contains the reward data
    void RewardedVideoAdRewardedEvent(IronSourcePlacement placement)
    {
        if (m_EventSystem)
            m_EventSystem.enabled = true;
        CustomCallback(ShowResult.Finished);
    }
    //Invoked when the Rewarded Video failed to show
    //@param description - string - contains information about the failure.
    void RewardedVideoAdShowFailedEvent(IronSourceError error)
    {
        if (m_EventSystem)
            m_EventSystem.enabled = true;
        CustomCallback(ShowResult.Failed);
    }
//#endif

    //Invoked when the initialization process has failed.
    //@param description - string - contains information about the failure.
    void InterstitialAdLoadFailedEvent(IronSourceError error)
    {
        print("Interstitial ad load failed..... " + error + " or " + error.getDescription());
    }
    //Invoked when the ad fails to show.
    //@param description - string - contains information about the failure.
    void InterstitialAdShowFailedEvent(IronSourceError error)
    {
        print("Interstitial show ad failed..... " + error + " or " + error.getDescription());
    }
    // Invoked when end user clicked on the interstitial ad
    void InterstitialAdClickedEvent()
    {
        print("clicked Interstitial ad");
    }
    //Invoked when the interstitial ad closed and the user goes back to the application screen.
    void InterstitialAdClosedEvent()
    {
    }
    //Invoked when the Interstitial is Ready to shown after load function is called
    void InterstitialAdReadyEvent()
    {
        print("Interstitial ready");
    }
    //Invoked when the Interstitial Ad Unit has opened
    void InterstitialAdOpenedEvent()
    {
    }
    //Invoked right before the Interstitial screen is about to open. NOTE - This event is available only for some of the networks. 
    //You should treat this event as an interstitial impression, but rather use InterstitialAdOpenedEvent
    void InterstitialAdShowSucceededEvent()
    {
    }

    public void ShowInterstitialAd()
    {
        if (IronSource.Agent.isInterstitialReady())
        {
            print("dead ready");
            IronSource.Agent.showInterstitial("DefaultInterstitial");
        }
        else
            print("dead not");
    }
    public void ShowAd(System.Action<ShowResult> callback, string itemName, string placementName, System.Action NoAdsReadyCallBack)
    {
        if (AnalyticsTracker.Instance)
            AnalyticsTracker.Instance.TrackAdButtonClicked();

        Debug.Log("[ShowAd] 1. Ad available: " + rewardedVideoAvailability);
        rewardedVideoAvailability = IronSource.Agent.isRewardedVideoAvailable();
        //Debug.Log("[ShowAd] 2. Ad available: " + rewardedVideoAvailability);


        if (rewardedVideoAvailability)
        {
            m_EventSystem = UnityEngine.EventSystems.EventSystem.current;
            if(m_EventSystem !=null)
                m_EventSystem.enabled = false;
            if (callback != null)
            {
                Debug.LogError("CALLBACK NOT NULL");
                m_Callback = callback;
            }
            //Debug.Log("UIManager.Instance.CurrentUser.ID: " + UserManager.Instance.CurrentUser.ID);
            //IronSource.Agent.clearRewardedVideoServerParams();
            //IronSource.Agent.setUserId(UserManager.Instance.CurrentUser.ID);
            //Dictionary<string, string> customParams = new Dictionary<string, string>();
            //customParams.Add("item", itemName);
            //IronSource.Agent.setRewardedVideoServerParams(customParams);
            //Debug.Log("[ShowAd] showing reward video..." + placementName);
            
            IronSource.Agent.showRewardedVideo();
            /*
			if(placementName == "DefaultInterstitial")
				IronSource.Agent.showInterstitial(placementName);
			else
            {
				print(placementName);
				IronSource.Agent.showRewardedVideo(placementName);
			}			
			*/
        }
        else
        {
            print("rewardedVideoAvailability is null");
#if UNITY_EDITOR || UNITY_STANDALONE
            if (callback != null)
                callback(ShowResult.Finished);
#else
            if (NoAdsReadyCallBack!=null)
			{
				NoAdsReadyCallBack();
			}
			            if(callback != null)
callback(ShowResult.Failed);
#endif
        }
    }

    private void CustomCallback(ShowResult result)
    {
        if(m_Callback !=null)
            m_Callback(result);
    }

    private void OnApplicationFocus(bool focus)
    {
        Debug.LogError("OnApplicationFocus Changed to: " + focus);
    }

    private void OnApplicationQuit()
    {
        Debug.LogError("OnApplicationQUIT CALLED");
    }

}
Editor is loading...