问题描述
我做了一个按钮,可以在Unity中显示广告。在我的计算机上的统一应用程序中,它可以很好地运行,但是当我将游戏转移到手机上并在手机上尝试时,我需要通过垃圾邮件广告按钮来显示它。
这是按钮的代码:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
[RequireComponent(typeof(Button))]
public class ReviveAd : MonoBehavIoUr,IUnityAdsListener
{
public PlayerMovement movement;
public Transform player;
public Vector3 revivePosOffset;
public GameObject reviveCanvas;
public bool isShowingReviveCanvas;
public GameObject RestartCanvas;
public bool isShowingRestart;
#if UNITY_ANDROID
private string gameId = "gone";
#endif
Button myButton;
public string myPlacementId = "rewardedVideo";
void Start()
{
Advertisement.Initialize(gameId,true);
myButton = GetComponent<Button>();
// Set interactivity to be dependent on the Placement’s status:
myButton.interactable = Advertisement.IsReady(myPlacementId);
// Map the ShowRewardedVideo function to the button’s click listener:
if (myButton) myButton.onClick.AddListener(ShowRewardedVideo);
// Initialize the Ads listener and service:
Advertisement.AddListener(this);
}
// Implement a function for showing a rewarded video ad:
void ShowRewardedVideo()
{
Advertisement.Show(myPlacementId);
}
// Implement IUnityAdsListener interface methods:
public void OnUnityAdsReady(string placementId)
{
// If the ready Placement is rewarded,activate the button:
if (placementId == myPlacementId)
{
myButton.interactable = true;
}
}
public void OnUnityAdsDidFinish(string placementId,ShowResult showResult)
{
//When ads finished
if (showResult == ShowResult.Finished)
{
isShowingReviveCanvas = true;
reviveCanvas.SetActive(isShowingReviveCanvas);
isShowingRestart = false;
RestartCanvas.SetActive(isShowingRestart);
}
else if (showResult == ShowResult.Skipped)
{
Debug.Log("Skipped");
}
else if (showResult == ShowResult.Failed)
{
Debug.LogWarning("The ad did not finish due to an error.");
}
}
public void OnUnityAdsDidError(string message)
{
// Log the error.
}
public void OnUnityAdsDidStart(string placementId)
{
//Respawns player after the obstacle
player.transform.position = player.position + revivePosOffset;
}
}
请注意,在这个问题中,我也用 gone 替换了我的游戏ID,所以这不是问题中的问题
解决方法
有了给定的信息,我们只能假设多次按下按钮后它起作用的原因。但是,请记住,Show()
仅在准备好shown的广告时才有效。
Show()
Displays an ad in the default Placement if it is ready.
根据您的解释,我只能猜测,当您按下按钮时,还不能显示它。
请添加以下调试代码,并查看结果并根据需要更新您的问题。
// Implement IUnityAdsListener interface methods:
public void OnUnityAdsReady(string placementId)
{
Debug.Log("Got callback that I can show an ad.");
// If the ready Placement is rewarded,activate the button:
if (placementId == myPlacementId)
{
myButton.interactable = true;
}
}
在开发过程中始终添加调试代码
// Implement a function for showing a rewarded video ad:
void ShowRewardedVideo()
{
var isReady = myButton.interactable = Advertisement.IsReady(myPlacementId);
var message = $"Is ready when I pressed the button: {isReady}";
Debug.Log(message);
Advertisement.Show(myPlacementId);
}