加载广告之前,请先调用 MobileAds.Initialize(),以便让应用初始化 Google 移动广告 SDK。此操作仅需执行一次,最好是在应用启动时执行。
以下示例展示了如何调用 MobileAds.Initialize():
MobileAds.Initialize((InitializationStatusinitstatus)=>
{if(initstatus==null){Debug.LogError("Google Mobile Ads initialization failed.");return;}Debug.Log("Google Mobile Ads initialization complete.");// Google Mobile Ads events are raised off the Unity Main thread. If you need to// access UnityEngine objects after initialization,// use MobileAdsEventExecutor.ExecuteInUpdate(). For more information, see:// https://developers.google.com/admob/unity/global-settings#raise_ad_events_on_the_unity_main_thread});
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThe Google Mobile Ads Unity plugin allows Unity developers to integrate Google mobile ads into Android and iOS apps without writing Java or Objective-C.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers must use Unity 2019.4 or higher, with Xcode 15.3 or higher for iOS deployment and a minimum Android API level of 21 for Android deployment.\u003c/p\u003e\n"],["\u003cp\u003eThe plugin can be installed via OpenUPM-CLI, OpenUPM, or by importing a \u003ccode\u003e.unitypackage\u003c/code\u003e from GitHub.\u003c/p\u003e\n"],["\u003cp\u003eBefore loading ads, initialize the Google Mobile Ads SDK by calling \u003ccode\u003eMobileAds.Initialize()\u003c/code\u003e at app launch.\u003c/p\u003e\n"],["\u003cp\u003eAdMob offers various ad formats including banner, interstitial, native, and rewarded, each with implementation guides provided.\u003c/p\u003e\n"]]],["The Google Mobile Ads Unity plugin allows Unity developers to integrate Google mobile ads into Android and iOS apps using C#. Installation requires Unity 2019.4+, specific Xcode/iOS and Android API levels, and the use of OpenUPM or direct GitHub import. Developers must remove old `.unitypackage` assets if migrating. The SDK needs to be initialized with `MobileAds.Initialize()`. Developers can set an Ad Manager app ID and choose from various ad formats such as Banner, Interstitial, Native, and Rewarded ads.\n"],null,["# Get started\n\nSelect platform: [Android](/ad-manager/mobile-ads-sdk/android/quick-start \"View this page for the Android platform docs.\") [iOS](/ad-manager/mobile-ads-sdk/ios/quick-start \"View this page for the iOS platform docs.\") [Unity](/ad-manager/mobile-ads-sdk/unity/quick-start \"View this page for the Unity platform docs.\") [Flutter](/ad-manager/mobile-ads-sdk/flutter/quick-start \"View this page for the Flutter platform docs.\")\n\n\u003cbr /\u003e\n\nThe Google Mobile Ads Unity plugin lets Unity developers serve Google mobile ads\non Android and iOS apps without having to write Java or Objective-C code. The\nplugin provides a C# interface for requesting ads that is used by C# scripts in\nyour Unity project.\n\nThis guide is intended for publishers who want to monetize a Unity app.\n\nPrerequisites\n-------------\n\n- Use Unity Editor 2019.4 or higher\n- To deploy to iOS\n - Xcode 16.0 or higher\n - Target iOS 12.0 or higher\n - [CocoaPods](//guides.cocoapods.org/using/getting-started.html)\n- To deploy to Android\n - Minimum Android API level of 23 or higher\n - Target Android API level 34 or higher\n\nAndroid API level is set in **Project Settings \\\u003e Player \\\u003e\nAndroid \\\u003e Other Settings \\\u003e Other Settings**.\n\nImport the Mobile Ads for Unity plugin\n--------------------------------------\n\n### OpenUPM-CLI\n\nIf you have the [OpenUPM CLI](//github.com/openupm/openupm-cli#installation)\ninstalled, you can install the OpenUPM registry with the following\ncommand: \n\n openupm add com.google.ads.mobile\n\n\n### Remove `.unitypackage` plugin assets\n\nIf you are migrating from using a `.unitypackage` to using OpenUPM, you\nmust manually uninstall the assets from the old location. Remove the\nfollowing directories that were imported from the `.unitypackage`:\n\n- Assets/ExternalDependencyManager\n- Assets/GoogleMobileAds\n- Assets/Plugins/Android/googlemobileads-unity.aar\n- Assets/Plugins/Android/GoogleMobileAdsPlugin\n- Assets/Plugins/iOS/GADUAdNetworkExtras\n- Assets/Plugins/iOS/unity-plugin-library.a \u003cbr /\u003e\n\n### OpenUPM\n\n1. Open the [package manager settings](//docs.unity3d.com/Manual/class-PackageManager.html) by selecting the Unity menu option **Edit \\\u003e Project Settings \\\u003e Package Manager**.\n2. Add OpenUPM as a scoped registry to the Package Manager window:\n\n Name: OpenUPM\n URL: https://package.openupm.com\n Scopes: com.google\n\n3. Open the [package manager menu](//docs.unity3d.com/Manual/upm-ui-install.html) by selecting the Unity\n menu option **Window \\\u003e Package Manager**.\n\n4. Set the manager scope drop-down to select **My Registries**.\n\n5. Select the **Google Mobile Ads for Unity** package from the package list\n and press **Install**.\n\n\n### Remove `.unitypackage` plugin assets\n\nIf you are migrating from using a `.unitypackage` to using OpenUPM, you\nmust manually uninstall the assets from the old location. Remove the\nfollowing directories that were imported from the `.unitypackage`:\n\n- Assets/ExternalDependencyManager\n- Assets/GoogleMobileAds\n- Assets/Plugins/Android/googlemobileads-unity.aar\n- Assets/Plugins/Android/GoogleMobileAdsPlugin\n- Assets/Plugins/iOS/GADUAdNetworkExtras\n- Assets/Plugins/iOS/unity-plugin-library.a \u003cbr /\u003e\n\n### Import from GitHub\n\n1. Download the latest [`.unitypackage`](//github.com/googleads/googleads-mobile-unity/releases/latest) release from GitHub.\n2. Import the `.unitypackage` file by selecting the Unity menu option **Assets \\\u003e Import package \\\u003e Custom Package** and importing all items.\n\nSet your Ad Manager app ID\n--------------------------\n\nIn the Unity editor, select **Assets \\\u003e Google Mobile Ads \\\u003e Settings**\nfrom the menu.\n\nEnter your Android and iOS\n\n[Ad Manager app ID](//support.google.com/admanager/answer/1656921)\n\nin each field.\n\nInitialize the SDK\n------------------\n\nBefore loading ads, have your app initialize the Google Mobile Ads SDK by\ncalling `MobileAds.Initialize()`. This needs to be done only once, ideally at\napp launch.\n| **Warning:** Ads can be preloaded by the Google Mobile Ads SDK or mediation partner SDKs upon calling `MobileAds.Initialize()`. If you need to obtain consent from users in the European Economic Area (EEA), set any request-specific flags, such as `tagForChildDirectedTreatment` or `tag_for_under_age_of_consent`, or otherwise take action before loading ads. Verify you do this before initializing the Google Mobile Ads SDK.\n\nHere's an example of how to call `MobileAds.Initialize()`: \n\n MobileAds.Initialize((InitializationStatus initstatus) =\u003e\n {\n if (initstatus == null)\n {\n Debug.LogError(\"Google Mobile Ads initialization failed.\");\n return;\n }\n\n Debug.Log(\"Google Mobile Ads initialization complete.\");\n\n // Google Mobile Ads events are raised off the Unity Main thread. If you need to\n // access UnityEngine objects after initialization,\n // use MobileAdsEventExecutor.ExecuteInUpdate(). For more information, see:\n // https://developers.google.com/admob/unity/global-settings#raise_ad_events_on_the_unity_main_thread\n }); \n https://github.com/googleads/googleads-mobile-unity/blob/2de30b494685db5152d07b44ab01e833ca8f20f7/samples/HelloWorld/Assets/Scripts/GoogleMobileAdsController.cs#L105-L134\n\nIf you're using mediation, wait until the callback occurs before loading ads to\nverify that all mediation adapters are initialized.\n\nSelect an ad format\n-------------------\n\nThe Google Mobile Ads SDK is now included in your Unity app when deploying to\neither the Android or iOS platform. You're now ready to implement an ad. AdMob\noffers a number of different ad formats, so you can choose the one that makes\nfor the best user experience.\n\n### Banner\n\nBanner ad units display rectangular ads that occupy a portion of an app's\nlayout. They can refresh automatically after a set period of time. This means\nusers view a new ad at regular intervals, even if they stay on the same\nscreen in your app. They're also the simplest ad format to implement. \n\n[Implement banner ads](/ad-manager/mobile-ads-sdk/unity/banner)\n\n### Interstitial\n\nInterstitial ad units show full-page ads in your app. Place them at natural\nbreaks and transitions in your app's interface, such as after level completion\nin a gaming app. \n\n[Implement interstitial ads](/ad-manager/mobile-ads-sdk/unity/interstitial)\n\n### Native\n\nNative ads are ads where you can customize the way assets such as headlines and\ncalls to action are presented in your apps. By styling the ad yourself, you can\ncreate a natural, unobtrusive ad presentations that can add to a rich user\nexperience. \n\n[Implement native ads](/ad-manager/mobile-ads-sdk/unity/native)\n\n### Rewarded\n\nRewarded ad units enable users to play games, take surveys, or watch videos to\nearn in-app rewards, such as coins, extra lives, or points. You can set\ndifferent rewards for different ad units, and specify the reward values and\nitems the user received. \n\n[Implement rewarded ads](/ad-manager/mobile-ads-sdk/unity/rewarded)"]]