Unity多次奖励视频广告注册。统一广告

thi*_*054 1 c# unity-game-engine unityads

这是我的奖励视频脚本。它连接到一个UI按钮。

\n\n
using UnityEngine;\nusing UnityEngine.UI;\nusing UnityEngine.Advertisements;\nusing UnityEngine.SceneManagement;\n\n[RequireComponent(typeof(Button))]\npublic class RewardedAdsButton : MonoBehaviour, IUnityAdsListener\n{\n\n#if UNITY_IOS\n    private string gameId = "1234567";\n#elif UNITY_ANDROID\n    private string gameId = "7654321";\n#endif\n\n    Button myButton;\n    public string myPlacementId = "rewardedVideo";\n\n    void Start()\n    {\n        myButton = GetComponent<Button>();\n\n        // Set interactivity to be dependent on the Placement\xe2\x80\x99s status:\n        myButton.interactable = Advertisement.IsReady(myPlacementId);\n\n        // Map the ShowRewardedVideo function to the button\xe2\x80\x99s click listener:\n        if (myButton) myButton.onClick.AddListener(ShowRewardedVideo);\n\n        // Initialize the Ads listener and service:\n        Advertisement.AddListener(this);\n        Advertisement.Initialize(gameId, true);\n    }\n\n    // Implement a function for showing a rewarded video ad:\n    void ShowRewardedVideo()\n    {\n        Advertisement.Show(myPlacementId);\n    }\n\n    // Implement IUnityAdsListener interface methods:\n    public void OnUnityAdsReady(string placementId)\n    {\n        // If the ready Placement is rewarded, activate the button: \n        if (placementId == myPlacementId)\n        {\n            myButton.interactable = true;\n        }\n    }\n\n    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)\n    {\n        // Define conditional logic for each ad completion status:\n        if (SceneManager.GetActiveScene().name == "GameplayScene")\n        {\n            if (showResult == ShowResult.Finished)\n            {\n                GameObject.Find("GameManager").GetComponent<GameManagerScript>().ResumeGame();\n            }\n            else if (showResult == ShowResult.Skipped)\n            {\n                SceneManager.LoadScene("MenuScene");\n            }\n            else if (showResult == ShowResult.Failed)\n            {\n                Debug.LogWarning("The ad did not finish due to an error.");\n            }\n        }\n        if(SceneManager.GetActiveScene().name == "CharacterScene")\n        {\n            if (showResult == ShowResult.Finished)\n            {\n                PlayerPrefs.SetInt("coin", PlayerPrefs.GetInt("coin", 0) + 50);\n            }\n            else if (showResult == ShowResult.Skipped)\n            {\n                //Do nothing.\n            }\n            else if (showResult == ShowResult.Failed)\n            {\n                Debug.LogWarning("The ad did not finish due to an error.");\n            }\n        }\n    }\n\n    public void OnUnityAdsDidError(string message)\n    {\n        // Log the error.\n    }\n\n    public void OnUnityAdsDidStart(string placementId)\n    {\n        // Optional actions to take when the end-users triggers an ad.\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

它应该只添加 50 个硬币,但至少会添加 100 或 50 倍的倍数,该按钮正在记录多次点击。知道发生了什么吗?

\n

小智 5

是正确的134054,对于那些有同样问题的人,一个可能的解决方案是添加这个方法:

public void OnDestroy ()
{
   Advertisement.RemoveListener (this);
}
Run Code Online (Sandbox Code Playgroud)

这样,如果您转到另一个场景并返回到该场景,则只会订阅最后创建的元素。


@Nicola,我不会将其添加到 OnUnityAdsDidFinish 中,因为这样无论结果如何,订阅都会在第一次调用后被删除。