using UnityEngine; /// /// Creates the InterstitialAdsManager and RewardedAdsManager singletons at runtime. /// /// SETUP: Add this component to any GameObject in your first (or earliest-loaded) scene. /// It will automatically create and configure both ad manager GameObjects on Start. /// You do not need to add InterstitialAdsManager or RewardedAdsManager to your scene manually. /// /// Both managers persist across scene loads via DontDestroyOnLoad. /// public class AutoCreateAdManagers : MonoBehaviour { [Header("Manager Creation")] [Tooltip("Create managers automatically when this object starts. Leave enabled unless you need manual control.")] [SerializeField] private bool createOnStart = true; [Tooltip("Create the RewardedAdsManager (required for rewarded ads).")] [SerializeField] private bool createRewardedAdsManager = true; [Tooltip("Create the InterstitialAdsManager (required for interstitial ads).")] [SerializeField] private bool createInterstitialAdsManager = true; private void Start() { if (createOnStart) CreateManagers(); } /// Create all enabled ad managers. Also callable from the Inspector via right-click. [ContextMenu("Create Ad Managers")] public void CreateManagers() { if (createRewardedAdsManager) CreateRewardedAdsManager(); if (createInterstitialAdsManager) CreateInterstitialAdsManager(); } private void CreateRewardedAdsManager() { if (GameObject.Find("RewardedAdsManager") != null || RewardedAdsManager.Instance != null) { Debug.Log("[AutoCreateAdManagers] RewardedAdsManager already exists — skipping."); return; } var go = new GameObject("RewardedAdsManager"); go.AddComponent(); DontDestroyOnLoad(go); Debug.Log("[AutoCreateAdManagers] Created RewardedAdsManager."); } private void CreateInterstitialAdsManager() { if (GameObject.Find("InterstitialAdsManager") != null || InterstitialAdsManager.Instance != null) { Debug.Log("[AutoCreateAdManagers] InterstitialAdsManager already exists — skipping."); return; } var go = new GameObject("InterstitialAdsManager"); go.AddComponent(); DontDestroyOnLoad(go); Debug.Log("[AutoCreateAdManagers] Created InterstitialAdsManager."); } }