Unity Ads 4.0.x- 广告不会在加载下一个场景或重新加载场景后工作
Unity Ads 4.0.x- ads wont work on loading next scene or after reloading scene
好吧,我想我的标题说明了问题所在。
DontDestroyOnLoad();
用起来毫无意义,因为“bs”objects在场景中是不一样的,应该调用一次AD Initialization。我在控制台中没有收到任何错误。初始化按预期工作。总之,一切正常,当我进入新场景时,初始化按预期工作,但是当我单击奖励按钮时,什么也没有发生。重新加载场景后也是如此。
接下来我该做什么?
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;
public class AD_Manager : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
{
[SerializeField] string _androidGameId = "12345678";
[SerializeField] string _iOSGameId = "87654321";
[SerializeField] bool _testMode = true;
private string _gameId;
[SerializeField] Button _showAdButton; //You can remove this if want to add the function manually from OnClick()
[SerializeField] string _androidAdUnitId = "Rewarded_Android";
[SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
string _adUnitId = null; // This will remain null for unsupported platforms
private bool showAd = false;
private bool onInitializationFailedToLoad = false;
private bool onAdFailToLoad = false;
// private bool networkIsNotActive = false;
[SerializeField] GameObject errorPanel;
// [SerializeField] GameObject errorPanel02;
[SerializeField] GameObject mainPanel;
[SerializeField] GameObject bs01;//bs stands for bullshit
[SerializeField] GameObject bs02;//ah yes, shit coming with they own parts ......
[SerializeField] GameObject bs03;//...
[SerializeField] GameObject bs04;// . . .
[SerializeField] GameObject bs05;// :|
private string m_ReachabilityText;
void Awake()
{
InitializeAds();
#if UNITY_EDITOR
Debug.Log("Awake");
#endif
_adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
? _iOSAdUnitId
: _androidAdUnitId;
#if UNITY_EDITOR
Debug.Log("the _adUnitId is: " + _adUnitId);
#endif
}
private void Update()
{
//Output the network reachability to the console window
#if UNITY_EDITOR
Debug.Log("Internet : " + m_ReachabilityText);
#endif
//Check if the device cannot reach the internet
if (Application.internetReachability == NetworkReachability.NotReachable)
{
//Change the Text --- nope :)
onAdFailToLoad = true;
m_ReachabilityText = "Not Reachable.";
}
else
{
onAdFailToLoad = false;
m_ReachabilityText = "Is Reachable.";
}
}
public void Quit()
{
Application.Quit();
#if UNITY_EDITOR
Debug.Log("Quit executed!");
#endif
}
public void EmptyCoins()
{
ScoreTextScript.coinAmount = 0;
}
public void InitializeAds()
{
_gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
? _iOSGameId
: _androidGameId;
Advertisement.Initialize(_gameId, _testMode, this);
}
public void OnInitializationComplete()
{
#if UNITY_EDITOR
Debug.Log("Unity Ads initialization complete.");
#endif
onInitializationFailedToLoad = false;
LoadAd();
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
onInitializationFailedToLoad = true;
#if UNITY_EDITOR
Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
#endif
}
public void LoadAd()
{
// IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled on top of the script).
#if UNITY_EDITOR
Debug.Log("Loading Ad: " + _adUnitId);
#endif
Advertisement.Load(_adUnitId, this);
}
// If the ad successfully loads, add a listener to the button and enable it:
public void OnUnityAdsAdLoaded(string adUnitId)
{
#if UNITY_EDITOR
Debug.Log("Ad Loaded: " + adUnitId);
#endif
if (adUnitId.Equals(_adUnitId))
{
// Configure the button to call the ShowAd() method when clicked:
_showAdButton.onClick.AddListener(ShowAd); //You can remove this if want to add the function manually from OnClick()
// Enable the button for users to click:
_showAdButton.interactable = true; //You can remove this if want to add the function manually from OnClick()
}
}
// Implement a method to execute when the user clicks the button:
public void ShowAd()
{
if (showAd == false)
{
#if UNITY_EDITOR
Debug.Log("Showing Ad");
#endif
// Disable the button:
_showAdButton.interactable = false; //You can remove this if want to add the function manually from OnClick()
// Then show the ad:
Advertisement.Show(_adUnitId, this);
_showAdButton.onClick.RemoveAllListeners(); //You can remove this if want to add the function manually from OnClick()
#if UNITY_EDITOR
Debug.Log("All Listeners Removed");
#endif
showAd = true;
}
}
//Simply don't load AD if onInitializationFailed == true or if onAdFailToLoad == true or if newtwork is not Reachable
public void DontShowAd()
{
if (onInitializationFailedToLoad == true || onAdFailToLoad == true)
{
mainPanel.SetActive(false);
bs01.SetActive(false);
bs02.SetActive(false);
bs03.SetActive(false);
bs04.SetActive(false);
bs05.SetActive(false);
errorPanel.SetActive(true);
}
}
// Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
{
if (showAd == true)
{
if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
{
#if UNITY_EDITOR
Debug.Log("Unity Ads Rewarded Ad Completed");
#endif
// Grant a reward.
ScoreTextScript.coinAmount += Random.Range(5, 25);
// Load another ad:
Advertisement.Load(_adUnitId, this);
showAd = false;
}
}
}
public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
{
#if UNITY_EDITOR
Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
#endif
onAdFailToLoad = true;
// Use the error details to determine whether to try to load another ad.
}
public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
{
#if UNITY_EDITOR
Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
#endif
onAdFailToLoad = true;
// Use the error details to determine whether to try to load another ad.
}
public void OnUnityAdsShowStart(string adUnitId) { }
public void OnUnityAdsShowClick(string adUnitId) { }
} // Class
固定代码:
void Awake()
{
InitializeAds();
#if UNITY_EDITOR
Debug.Log("Awake");
#endif
_adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
? _iOSAdUnitId
: _androidAdUnitId;
#if UNITY_EDITOR
Debug.Log("the _adUnitId is: " + _adUnitId);
#endif
LoadAd();
}
public void LoadAd()
{
// IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled on top of the script).
#if UNITY_EDITOR
Debug.Log("Loading Ad: " + _adUnitId);
#endif
Advertisement.Load(_adUnitId, this);
}
好吧,我想我的标题说明了问题所在。
DontDestroyOnLoad();
用起来毫无意义,因为“bs”objects在场景中是不一样的,应该调用一次AD Initialization。我在控制台中没有收到任何错误。初始化按预期工作。总之,一切正常,当我进入新场景时,初始化按预期工作,但是当我单击奖励按钮时,什么也没有发生。重新加载场景后也是如此。
接下来我该做什么?
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;
public class AD_Manager : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
{
[SerializeField] string _androidGameId = "12345678";
[SerializeField] string _iOSGameId = "87654321";
[SerializeField] bool _testMode = true;
private string _gameId;
[SerializeField] Button _showAdButton; //You can remove this if want to add the function manually from OnClick()
[SerializeField] string _androidAdUnitId = "Rewarded_Android";
[SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
string _adUnitId = null; // This will remain null for unsupported platforms
private bool showAd = false;
private bool onInitializationFailedToLoad = false;
private bool onAdFailToLoad = false;
// private bool networkIsNotActive = false;
[SerializeField] GameObject errorPanel;
// [SerializeField] GameObject errorPanel02;
[SerializeField] GameObject mainPanel;
[SerializeField] GameObject bs01;//bs stands for bullshit
[SerializeField] GameObject bs02;//ah yes, shit coming with they own parts ......
[SerializeField] GameObject bs03;//...
[SerializeField] GameObject bs04;// . . .
[SerializeField] GameObject bs05;// :|
private string m_ReachabilityText;
void Awake()
{
InitializeAds();
#if UNITY_EDITOR
Debug.Log("Awake");
#endif
_adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
? _iOSAdUnitId
: _androidAdUnitId;
#if UNITY_EDITOR
Debug.Log("the _adUnitId is: " + _adUnitId);
#endif
}
private void Update()
{
//Output the network reachability to the console window
#if UNITY_EDITOR
Debug.Log("Internet : " + m_ReachabilityText);
#endif
//Check if the device cannot reach the internet
if (Application.internetReachability == NetworkReachability.NotReachable)
{
//Change the Text --- nope :)
onAdFailToLoad = true;
m_ReachabilityText = "Not Reachable.";
}
else
{
onAdFailToLoad = false;
m_ReachabilityText = "Is Reachable.";
}
}
public void Quit()
{
Application.Quit();
#if UNITY_EDITOR
Debug.Log("Quit executed!");
#endif
}
public void EmptyCoins()
{
ScoreTextScript.coinAmount = 0;
}
public void InitializeAds()
{
_gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
? _iOSGameId
: _androidGameId;
Advertisement.Initialize(_gameId, _testMode, this);
}
public void OnInitializationComplete()
{
#if UNITY_EDITOR
Debug.Log("Unity Ads initialization complete.");
#endif
onInitializationFailedToLoad = false;
LoadAd();
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
onInitializationFailedToLoad = true;
#if UNITY_EDITOR
Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
#endif
}
public void LoadAd()
{
// IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled on top of the script).
#if UNITY_EDITOR
Debug.Log("Loading Ad: " + _adUnitId);
#endif
Advertisement.Load(_adUnitId, this);
}
// If the ad successfully loads, add a listener to the button and enable it:
public void OnUnityAdsAdLoaded(string adUnitId)
{
#if UNITY_EDITOR
Debug.Log("Ad Loaded: " + adUnitId);
#endif
if (adUnitId.Equals(_adUnitId))
{
// Configure the button to call the ShowAd() method when clicked:
_showAdButton.onClick.AddListener(ShowAd); //You can remove this if want to add the function manually from OnClick()
// Enable the button for users to click:
_showAdButton.interactable = true; //You can remove this if want to add the function manually from OnClick()
}
}
// Implement a method to execute when the user clicks the button:
public void ShowAd()
{
if (showAd == false)
{
#if UNITY_EDITOR
Debug.Log("Showing Ad");
#endif
// Disable the button:
_showAdButton.interactable = false; //You can remove this if want to add the function manually from OnClick()
// Then show the ad:
Advertisement.Show(_adUnitId, this);
_showAdButton.onClick.RemoveAllListeners(); //You can remove this if want to add the function manually from OnClick()
#if UNITY_EDITOR
Debug.Log("All Listeners Removed");
#endif
showAd = true;
}
}
//Simply don't load AD if onInitializationFailed == true or if onAdFailToLoad == true or if newtwork is not Reachable
public void DontShowAd()
{
if (onInitializationFailedToLoad == true || onAdFailToLoad == true)
{
mainPanel.SetActive(false);
bs01.SetActive(false);
bs02.SetActive(false);
bs03.SetActive(false);
bs04.SetActive(false);
bs05.SetActive(false);
errorPanel.SetActive(true);
}
}
// Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
{
if (showAd == true)
{
if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
{
#if UNITY_EDITOR
Debug.Log("Unity Ads Rewarded Ad Completed");
#endif
// Grant a reward.
ScoreTextScript.coinAmount += Random.Range(5, 25);
// Load another ad:
Advertisement.Load(_adUnitId, this);
showAd = false;
}
}
}
public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
{
#if UNITY_EDITOR
Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
#endif
onAdFailToLoad = true;
// Use the error details to determine whether to try to load another ad.
}
public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
{
#if UNITY_EDITOR
Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
#endif
onAdFailToLoad = true;
// Use the error details to determine whether to try to load another ad.
}
public void OnUnityAdsShowStart(string adUnitId) { }
public void OnUnityAdsShowClick(string adUnitId) { }
} // Class
固定代码:
void Awake()
{
InitializeAds();
#if UNITY_EDITOR
Debug.Log("Awake");
#endif
_adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
? _iOSAdUnitId
: _androidAdUnitId;
#if UNITY_EDITOR
Debug.Log("the _adUnitId is: " + _adUnitId);
#endif
LoadAd();
}
public void LoadAd()
{
// IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled on top of the script).
#if UNITY_EDITOR
Debug.Log("Loading Ad: " + _adUnitId);
#endif
Advertisement.Load(_adUnitId, this);
}