c1_unity/Assets/Scripts/Common/SDK/BFIronSourceSDKManager.cs
2023-07-19 15:45:52 +08:00

304 lines
9.6 KiB
C#

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using com.adjust.sdk;
using Newtonsoft.Json;
namespace BF
{
// Example for IronSource Unity.
public class BFIronSourceSDKManager : MonoBehaviour
{
public Action<int> luaShowCallback;
public Action<int> luaLoadedCallback;
public Action<int, string> luaEarnedRewardCallback;
public Action<string> luaAdRevenuePaidEventCallback;
public bool AdLoaded = false;
public bool AdInitialized = false;
public void Start()
{
#if UNITY_ANDROID
// string appKey = "85460dcd";
string appKey = "1a6aacc25";
BFLog.Log("unity-script: IronSource.Agent.validateIntegration");
IronSource.Agent.validateIntegration();
BFLog.Log("unity-script: unity version" + IronSource.unityVersion());
// SDK init
BFLog.Log("unity-script: IronSource.Agent.init");
IronSource.Agent.init(appKey);
IronSource.Agent.setManualLoadRewardedVideo(true);
#elif UNITY_IPHONE
// string appKey = "8545d445";
#else
// string appKey = "unexpected_platform";
#endif
// BFLog.Log("unity-script: IronSource.Agent.validateIntegration");
// IronSource.Agent.validateIntegration();
// BFLog.Log("unity-script: unity version" + IronSource.unityVersion());
// // SDK init
// BFLog.Log("unity-script: IronSource.Agent.init");
// IronSource.Agent.init(appKey);
// IronSource.Agent.setManualLoadRewardedVideo(true);
}
void OnEnable()
{
//Add Init Event
IronSourceEvents.onSdkInitializationCompletedEvent += SdkInitializationCompletedEvent;
//Add Rewarded Video Events
// IronSourceEvents.onRewardedVideoAdOpenedEvent += RewardedVideoAdOpenedEvent;
// IronSourceEvents.onRewardedVideoAdClosedEvent += RewardedVideoAdClosedEvent;
// IronSourceEvents.onRewardedVideoAvailabilityChangedEvent += RewardedVideoAvailabilityChangedEvent;
// IronSourceEvents.onRewardedVideoAdStartedEvent += RewardedVideoAdStartedEvent;
// IronSourceEvents.onRewardedVideoAdEndedEvent += RewardedVideoAdEndedEvent;
// IronSourceEvents.onRewardedVideoAdRewardedEvent += RewardedVideoAdRewardedEvent;
// IronSourceEvents.onRewardedVideoAdShowFailedEvent += RewardedVideoAdShowFailedEvent;
// IronSourceEvents.onRewardedVideoAdClickedEvent += RewardedVideoAdClickedEvent;
//Add ImpressionSuccess Event
IronSourceEvents.onImpressionSuccessEvent += ImpressionSuccessEvent;
IronSourceEvents.onImpressionDataReadyEvent += ImpressionDataReadyEvent;
//Add AdInfo Rewarded Video Events
IronSourceRewardedVideoEvents.onAdOpenedEvent += ReardedVideoOnAdOpenedEvent;
IronSourceRewardedVideoEvents.onAdClosedEvent += ReardedVideoOnAdClosedEvent;
IronSourceRewardedVideoEvents.onAdAvailableEvent += ReardedVideoOnAdAvailable;
IronSourceRewardedVideoEvents.onAdUnavailableEvent += ReardedVideoOnAdUnavailable;
IronSourceRewardedVideoEvents.onAdShowFailedEvent += ReardedVideoOnAdShowFailedEvent;
IronSourceRewardedVideoEvents.onAdRewardedEvent += ReardedVideoOnAdRewardedEvent;
IronSourceRewardedVideoEvents.onAdClickedEvent += ReardedVideoOnAdClickedEvent;
}
public void SetAdRevenuePaidEventCallback(Action<string> callback)
{
luaAdRevenuePaidEventCallback = callback;
}
void OnApplicationPause(bool isPaused)
{
#if UNITY_ANDROID
BFLog.Log("unity-script: OnApplicationPause = " + isPaused);
IronSource.Agent.onApplicationPause(isPaused);
#endif
}
public void ShowFullScreenAds()
{
if(IronSource.Agent.isRewardedVideoAvailable())
{
IronSource.Agent.showRewardedVideo();
}
else
{
// TryLoadRewardedAd();
BFLog.Log("unity-script: IronSource.Agent.isRewardedVideoAvailable - False");
}
}
public void TryLoadRewardedAd()
{
IronSource.Agent.loadRewardedVideo();
}
public void SetAdPlacement(string placement)
{
// BFMain.Instance.SDKMgr.BFNativeSDKMgr.SetAdPlacement(placement);
}
public void SetAdShowCallback(Action<int> callback)
{
luaShowCallback = callback;
}
// 设置广告加载回调
public void SetAdLoadedCallback(Action<int> callback)
{
luaLoadedCallback = callback;
}
// 设置广告看完回调
public void SetAdEarnedRewardCallback(Action<int, string> callback)
{
luaEarnedRewardCallback = callback;
}
public void ShowFullScreenAdFinish(int code)
{
if (luaShowCallback != null)
{
luaShowCallback(code);
}
}
#region Init callback handlers
void SdkInitializationCompletedEvent()
{
BFLog.Log("unity-script: I got SdkInitializationCompletedEvent");
}
#endregion
#region AdInfo Rewarded Video
void ReardedVideoOnAdOpenedEvent(IronSourceAdInfo adInfo) {
BFLog.Log("unity-script: I got ReardedVideoOnAdOpenedEvent With AdInfo " + adInfo.ToString());
ShowFullScreenAdFinish(0);
}
void ReardedVideoOnAdClosedEvent(IronSourceAdInfo adInfo)
{
BFLog.Log("unity-script: I got ReardedVideoOnAdClosedEvent With AdInfo " + adInfo.ToString());
}
void ReardedVideoOnAdAvailable(IronSourceAdInfo adInfo)
{
BFLog.Log("unity-script: I got ReardedVideoOnAdAvailable With AdInfo " + adInfo.ToString());
AdLoaded = true;
if (luaLoadedCallback != null)
{
luaLoadedCallback(0);
}
}
void ReardedVideoOnAdUnavailable()
{
BFLog.Log("unity-script: I got ReardedVideoOnAdUnavailable");
AdLoaded = false;
if (luaLoadedCallback != null)
{
luaLoadedCallback(1);
}
}
void ReardedVideoOnAdShowFailedEvent(IronSourceError ironSourceError,IronSourceAdInfo adInfo)
{
BFLog.Log("unity-script: I got RewardedVideoAdOpenedEvent With Error"+ironSourceError.ToString() + "And AdInfo " + adInfo.ToString());
ShowFullScreenAdFinish(1);
}
void ReardedVideoOnAdRewardedEvent(IronSourcePlacement ironSourcePlacement,IronSourceAdInfo adInfo)
{
BFLog.Log("unity-script: I got ReardedVideoOnAdRewardedEvent With Placement" + ironSourcePlacement.ToString()+ "And AdInfo " + adInfo.ToString());
if (luaEarnedRewardCallback != null)
{
luaEarnedRewardCallback(0, "");
}
}
void ReardedVideoOnAdClickedEvent(IronSourcePlacement ironSourcePlacement, IronSourceAdInfo adInfo)
{
BFLog.Log("unity-script: I got ReardedVideoOnAdClickedEvent With Placement" + ironSourcePlacement.ToString() + "And AdInfo " + adInfo.ToString());
}
#endregion
// #region RewardedAd callback handlers
// void RewardedVideoAvailabilityChangedEvent(bool canShowAd)
// {
// BFLog.Log("unity-script: I got RewardedVideoAvailabilityChangedEvent, value = " + canShowAd);
// }
// void RewardedVideoAdOpenedEvent()
// {
// BFLog.Log("unity-script: I got RewardedVideoAdOpenedEvent");
// }
// void RewardedVideoAdRewardedEvent(IronSourcePlacement ssp)
// {
// BFLog.Log("unity-script: I got RewardedVideoAdRewardedEvent, amount = " + ssp.getRewardAmount() + " name = " + ssp.getRewardName());
// }
// void RewardedVideoAdClosedEvent()
// {
// BFLog.Log("unity-script: I got RewardedVideoAdClosedEvent");
// }
// void RewardedVideoAdStartedEvent()
// {
// BFLog.Log("unity-script: I got RewardedVideoAdStartedEvent");
// }
// void RewardedVideoAdEndedEvent()
// {
// BFLog.Log("unity-script: I got RewardedVideoAdEndedEvent");
// }
// void RewardedVideoAdShowFailedEvent(IronSourceError error)
// {
// BFLog.Log("unity-script: I got RewardedVideoAdShowFailedEvent, code : " + error.getCode() + ", description : " + error.getDescription());
// }
// void RewardedVideoAdClickedEvent(IronSourcePlacement ssp)
// {
// BFLog.Log("unity-script: I got RewardedVideoAdClickedEvent, name = " + ssp.getRewardName());
// }
// #endregion
#region ImpressionSuccess callback handler
void ImpressionSuccessEvent(IronSourceImpressionData impressionData)
{
// BFLog.Log("unity - script: I got ImpressionSuccessEvent ToString(): " + impressionData.ToString());
// BFLog.Log("unity - script: I got ImpressionSuccessEvent allData: " + impressionData.allData);
}
void ImpressionDataReadyEvent(IronSourceImpressionData impressionData)
{
if (impressionData == null)
{
return;
}
if (ReferenceEquals(impressionData.revenue, null))
{
return;
}
double revenue = (double)impressionData.revenue;
AdjustAdRevenue adjustAdRevenue = new AdjustAdRevenue(AdjustConfig.AdjustAdRevenueSourceIronSource);
adjustAdRevenue.setRevenue(revenue, "USD");
// optional fields
adjustAdRevenue.setAdRevenueNetwork(impressionData.adNetwork);
adjustAdRevenue.setAdRevenueUnit(impressionData.adUnit);
adjustAdRevenue.setAdRevenuePlacement(impressionData.placement);
// track Adjust ad revenue
Adjust.trackAdRevenue(adjustAdRevenue);
if (luaAdRevenuePaidEventCallback == null)
{
return;
}
var dict = new Dictionary<string, string>();
dict.Add("revenue", revenue.ToString());
dict.Add("auction_id", impressionData.auctionId);
dict.Add("country_code", impressionData.country);
dict.Add("network_name", impressionData.adNetwork);
dict.Add("ad_unit_Id", impressionData.adUnit);
dict.Add("placement", impressionData.placement);
dict.Add("ad_format_name", impressionData.instanceName);
dict.Add("ad_format_id", impressionData.instanceId);
if (!string.IsNullOrEmpty(impressionData.segmentName))
{
dict.Add("segment_name", impressionData.segmentName);
}
if (!ReferenceEquals(impressionData.lifetimeRevenue, null))
{
dict.Add("lifetime_revenue", impressionData.lifetimeRevenue.ToString());
}
if (!string.IsNullOrEmpty(impressionData.encryptedCPM))
{
dict.Add("encrypted_cpm", impressionData.encryptedCPM);
}
dict.Add("currency", "USD");
dict.Add("precision", impressionData.precision);
var result = JsonConvert.SerializeObject(dict);
luaAdRevenuePaidEventCallback(result);
}
#endregion
}
}