Unity游戏实现每5轮/失败后自动播放广告的技术咨询
Hey there! Let's get your ad system working properly—since you're new to Unity and C#, I'll break this down step by step with exact code changes you need to make.
一、先搞定基础配置(这是前提!)
First off, your AdsManager has placeholder IDs that need to be replaced with real values from your Unity Ads dashboard:
- Open AdsManager.cs and replace the
#######ingameIdwith your actual iOS/Android game IDs (they’re different for each platform). - Make sure your ad unit IDs (like
Interstitial_Android) match exactly what you created in the Unity Ads dashboard—case-sensitive! Update the code to handle both platforms properly:
public void PlayAd() { string adUnitId = ""; #if UNITY_IOS adUnitId = "Interstitial_iOS"; // Replace with your iOS interstitial ad ID #else adUnitId = "Interstitial_Android"; // Replace with your Android interstitial ad ID #endif if(Advertisement.IsReady(adUnitId)) Advertisement.Show(adUnitId); else Debug.Log("Interstitial ad isn't ready yet!"); }
二、让AdsManager成为单例(避免引用丢失)
Right now, if your ads reference in GameManager isn’t set correctly in the Inspector, the ad code will fail. Let’s turn AdsManager into a singleton so it’s always accessible:
Add this code to AdsManager.cs at the top of the class, plus an Awake() method:
public class AdsManager : MonoBehaviour, IUnityAdsListener { // Singleton setup public static AdsManager Instance; #if UNITY_IOS string gameId = "YOUR_IOS_GAME_ID"; #else string gameId = "YOUR_ANDROID_GAME_ID"; #endif Action onRewardedAdSuccess; void Awake() { // Ensure only one AdsManager exists across scenes if(Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); // Keep this object when switching scenes } else { Destroy(gameObject); } } // Rest of your existing code stays the same... }
三、修复“失败后播放广告”的逻辑
Update your GameManager to use the singleton instead of a manual reference, so you don’t have to drag anything in the Inspector:
Modify GameManager.cs:
public class GameManager : MonoBehaviour { public GameObject gameOverCanvas; // Remove the old public AdsManager ads; line—we'll use the singleton instead private void Start() { Time.timeScale = 1; AdsManager.Instance.ShowBanner(); // Use singleton to show banner } public void GameOver() { gameOverCanvas.SetActive(true); Time.timeScale = 0; AdsManager.Instance.PlayAd(); // Trigger interstitial on game over } public void Replay() { SceneManager.LoadScene(0); } }
四、添加“每5轮播放广告”的触发逻辑
We’ll add a round counter to GameManager, and trigger an ad whenever the player completes 5 rounds.
Update GameManager.cs with these additions:
public class GameManager : MonoBehaviour { public GameObject gameOverCanvas; private int roundCount = 0; // Track how many rounds the player has completed private void Start() { Time.timeScale = 1; AdsManager.Instance.ShowBanner(); } // Call this method whenever the player finishes a round! // (You'll need to hook this up to your game's round-completion logic) public void OnRoundComplete() { roundCount++; if(roundCount >= 5) { AdsManager.Instance.PlayAd(); roundCount = 0; // Reset counter after showing ad } } public void GameOver() { gameOverCanvas.SetActive(true); Time.timeScale = 0; AdsManager.Instance.PlayAd(); } public void Replay() { SceneManager.LoadScene(0); roundCount = 0; // Reset counter when restarting the game } }
重要:关联轮次完成逻辑
找到你游戏中定义“轮次完成”的地方(比如玩家通关关卡、完成一波敌人后),添加下面的代码:
// 示例:在你的Player或LevelManager脚本中 public void FinishRound() { // 你现有的轮次完成逻辑... GameManager.Instance.OnRoundComplete(); // 触发广告计数 }
五、测试小贴士
- 在Unity中启用测试模式:前往
Project Settings > Services > Ads,勾选"Test Mode",这样会显示测试广告(不会产生真实收益,但测试时避免账户问题)。 - 查看控制台日志:如果广告不显示,找类似"Interstitial ad isn't ready yet"的调试信息或错误日志,它们会告诉你问题所在。
- 确保AdsManager在初始场景中:因为我们加了
DontDestroyOnLoad,它会在场景切换时保留下来。
内容的提问来源于stack exchange,提问作者Niks Rodžers

