رویدادهای سفارشی به ناشرانی که از میانجیگری AdMob استفاده میکنند، این امکان را میدهد که میانجیگری آبشاری را برای یک شبکه تبلیغاتی شخص ثالث که جزو شبکههای تبلیغاتی پشتیبانیشده نیست، اضافه کنند. این راهنما نحوه استفاده از یک رویداد سفارشی موجود که برای اندروید و iOS در یک پروژه Unity ساخته شده است را توضیح میدهد.
پیشنیازها
قبل از ادامه، موارد زیر را انجام دهید:
Set up Google Mobile Ads Unity Plugin . Your Unity app should already have the Google Mobile Ads Unity plug-in imported.
Custom event adapters already built for Android and iOS. To create custom event adapters, refer to our custom events guides on Android and iOS .
تعریف رویداد سفارشی
In order for a custom event to participate in mediation, the custom event must be defined in the AdMob web interface. Add a custom event to both of your Android and iOS mediation groups.
This screenshot shows some sample custom event settings:

| نحوه پر کردن پارامترها | |
|---|---|
| نام کلاس (iOS) | For iOS, enter the name of the class that implements the custom event. If your class is implemented in Swift, you need to prefix the class name with the name of its app / framework module (for example, اگر چندین هدف در پروژه خود دارید یا اگر نام پروژه با نام هدف متفاوت است، نام هدف الزامی است. با نام هدف، به این شکل خواهد بود: |
| نام کلاس (اندروید) | For Android, make sure the value you give for the Class Name is the fully qualified class name for Android (for example com.google.ads.mediation.sample.customevent.SampleCustomEvent ). |
| برچسب | یک نام منحصر به فرد برای رویداد وارد کنید. |
| پارامتر | If you wish to pass a string argument to your custom event, for example an ad unit ID. |
وارد کردن کتابخانههای رویدادهای سفارشی
Custom events might require additional libraries to be included to work properly. For example, you might need to include the following libraries:
- SDK شخص ثالث اندروید
- رویداد سفارشی شخص ثالث اندروید
- کیت توسعه نرمافزاری تبلیغات شخص ثالث iOS
- رویداد سفارشی شخص ثالث iOS
انواع کتابخانهها
There are multiple ways to import Android or iOS code into a Unity project, including:
- وارد کردن مصنوعات از پیش ساخته شده اندروید یا iOS با استفاده از مدیر وابستگی خارجی برای یونیتی
- وارد کردن افزونههای AAR و کتابخانههای اندروید
- وارد کردن فایلهای منبع جاوا و کاتلین
- وارد کردن فایلهای منبع iOS و کتابخانههای استاتیک
Depending on how the libraries you use are packaged, you might need a different import strategy for each library. Each option is discussed in more detail later.
(Recommended) Import prebuilt Android or iOS artifacts
Import prebuilt artifacts from Maven or CocoaPods using the External Dependency Manager for Unity . This plug-in is included with the GoogleMobileAds plug-in.
To import existing artifacts, create a configuration file to define your imports. The filename and path have the following requirements:
- این فایل باید در پوشه
/Editor/وجود داشته باشد. - نام فایل باید با
Dependencies.xmlخاتمه یابد.
For example, to import custom event's adapters for a hypothetical ad network named AdPub , create the file:
Assets/AdPub/Editor/AdPubDependencies.xml
در مرحله بعد، وابستگیهای خود را در فایل AdPubDependencies.xml تعریف کنید. قوانین پیکربندی ایمپورتها را میتوانید در External Dependency Manager for Unity Getting Started بیابید. قطعه کد زیر شامل SDK اندروید و iOS و کتابخانههای رویدادهای سفارشی برای یک شبکه تبلیغاتی فرضی "AdPub" است.
Assets/AdPub/Editor/AdPubDependencies.xml
<dependencies>
<androidPackages>
<androidPackage spec="com.adpub.android:adpub-sdk:1.0.0" />
<androidPackage spec="com.adpub.android:adpub-custom-event:1.0.0">
<repositories>
<repository>https://repo.maven.apache.org/maven2/</repository>
<repository>https://dl.google.com/dl/android/maven2/</repository>
</repositories>
</androidPackage>
</androidPackages>
<iosPods>
<iosPod name="AdPubSDK" version="1.0" />
<iosPod name="AdPubCustomEvent" version="1.0">
<sources>
<source>https://github.com/CocoaPods/Specs</source>
</sources>
</iosPod>
</iosPods>
</dependencies>
If your custom event artifact already has a dependency on the required ad network SDK, you don't need to define the SDK dependency explicitly: Example
The External Dependency Manager automatically monitors for configuration changes and resolves dependencies. You can also execute manual resolution with the following menu command:
Assets > External Dependency Manager > Android Resolver > Force Resolve
افزونههای AAR و کتابخانههای اندروید را وارد کنید
یونیتی از وارد کردن فایلهای *.aar و همچنین پروژههای کتابخانه اندروید پشتیبانی میکند. اگر رویداد سفارشی اندروید شما به این روش بستهبندی شده است، برای دستورالعملهای نحوه گنجاندن این فایلها در پروژه یونیتی خود ، به افزونههای AAR و کتابخانههای اندروید مراجعه کنید.
فایلهای منبع جاوا و کاتلین را وارد کنید
Starting in Unity 2018.2 or higher, if your Android custom event code comprises of uncompiled *.java or *.kt files, you can use Java or Kotlin source files as plug-ins .
فایلهای منبع iOS و کتابخانههای استاتیک را وارد کنید
Unity supports *.framework artifacts, *.h , and *.m source files. Importing iOS artifacts and source files is explained in Unity's guide for native plug-ins .
رویدادهای سفارشی را با بازرس تبلیغات آزمایش کنید
Ad inspector can be used to test that custom events have been imported correctly into your application. Ad inspector can be open with just gestures or programmatically with minimal code.
(اختیاری) فراخوانی متدهای بومی SDK شخص ثالث از اسکریپتهای C#
Third-party ad network SDKs could have special requirements which require calling Android or iOS methods directly. The process for calling these methods directly are as follows:
- تعریف یک رابط مشترک برای کلاینتهای پلتفرم
- پیادهسازی یک کلاینت پیشفرض برای پلتفرمهای پشتیبانینشده
- پیادهسازی یک کلاینت اندروید برای فراخوانی متدهای اندروید
- پیادهسازی یک کلاینت iOS برای فراخوانی متدهای iOS
- Implement a client factory to conditionally switch between iOS and Android clients
- Define an API for accessing all third-party ad network SDK functionalities
بخش زیر نحوه پیادهسازی این مراحل را برای یک شبکه تبلیغاتی فرضی به نام "AdPub" در یک API سیشارپ که میتواند متدهای اندروید و iOS را فراخوانی کند، نشان میدهد:
اندروید
package com.adpub.android;
public class AdPubSdk
{
public static void setHasUserConsent(boolean hasUserConsent);
}
آیاواس
@interface AdPubSdk : NSObject
+ (void)setHasUserConsent:(BOOL)hasUserConsent;
@end
تعریف یک رابط مشترک برای کلاینتهای پلتفرم
یک رابط IAdPubClient با متدی که نشاندهندهی API اصلی اندروید و iOS است، ایجاد کنید.
Assets/AdPub/Common/IAdPubClient.cs
namespace AdPub.Common
{
public interface IAdPubClient
{
///<summary>
/// Sets a flag indicating if the app has user consent for advertisement.
///</summary>
void SetHasUserConsent(bool hasUserConsent);
}
}
تعریف کلاینت پیشفرض برای پلتفرمهای پشتیبانینشده
Create a DefaultClient class implementing the IAdPubClient interface that just logs the method name. Use this implementation for the Unity editor and all platforms other than Android or iOS.
Assets/AdPub/Common/DefaultClient.cs
namespace AdPub.Common
{
public class DefaultClient : IAdPubClient
{
public void SetHasUserConsent(bool hasUserConsent)
{
Debug.Log("SetHasUserConsent was called.");
}
}
}
پیادهسازی یک کلاینت پلتفرم iOS
یک کلاس iOSAdPubClient ایجاد کنید که رابط IAdPubClient را در iOS پیادهسازی کند. این پیادهسازی از InteropServices برای فراخوانی متد setHasUserConsent() در کلاس iOS AdPubSdk استفاده میکند.
Assets/AdPub/Platforms/iOS/iOSAdPubClient.cs
// Wrap this class in a conditional operator to make sure it only runs on iOS.
#if UNITY_IOS
// Reference InteropServices to include the DLLImportAttribute type.
using System.Runtime.InteropServices;
using AdPub.Common;
namespace AdPub.Platforms.Android
{
public class iOSAdPubClient : IAdPubClient
{
public void SetHasUserConsent(bool hasUserConsent)
{
GADUAdPubSetHasUserConsent(hasUserConsent);
}
[DllImport("__Internal")]
internal static extern void GADUAdPubSetHasUserConsent(bool hasUserConsent);
}
}
#endif
در مرحله بعد، متد GADUAdPubSetHasUserConsent() را که در بالا تعریف شده است، پیادهسازی کنید. AdPubClientBridge.m را با متد GADUAdPubSetHasUserConsent() C ایجاد کنید تا فراخوانی متد از Unity را مدیریت کند و AdPubSDK فراخوانی کند.
AdPubClientBridge یک فایل منبع iOS است و باید همانطور که در راهنمای Unity برای افزونههای بومی توضیح داده شده است، در پوشه Plugins/iOS قرار گیرد.
Assets/AdPub/Plugins/iOS/AdPubClientBridge.m
#import <AdPubSDK/AdPubSDK.h>
void GADUAdPubSetHasUserConsent(BOOL hasUserConsent) {
[AdPubSDK setHasUserConsent:hasUserConsent];
}
پیادهسازی یک کلاینت پلتفرم اندروید
یک کلاس AndroidAdPubCient ایجاد کنید که رابط IAdPubClient را در اندروید پیادهسازی کند. این پیادهسازی از کلاسهای کمکی جاوای اندروید برای فراخوانی متد استاتیک اندروید setHasUserConsent() استفاده میکند.
از آنجایی که کلاسهای کمکی جاوای اندروید فقط در زمان اجرای اندروید در دسترس هستند، میتوانید با استفاده از دستورالعمل کامپایلر UNITY_ANDROID برای قرار دادن کلاس به صورت نشان داده شده در قطعه کد، از خطاهای کامپایل جلوگیری کنید. به عنوان یک جایگزین، میتوانید از تعاریف Assembly در Unity 2017.4 و بالاتر برای حل این مشکل استفاده کنید.
Assets/AdPub/Platforms/Android/AndroidAdPubClient.cs
// Wrap this class in a conditional operator to make sure it only runs on Android.
#if UNITY_ANDROID
// Reference the UnityEngine namespace which contains the JNI Helper classes.
using UnityEngine;
using AdPub.Common;
namespace AdPub.Platforms.Android
{
public class AndroidAdPubClient : IAdPubClient
{
public void SetHasUserConsent(bool hasUserConsent)
{
// Make a reference to the com.adpub.AdPubSDK.
AndroidJavaClass adPubSdk = new AndroidJavaClass("com.adpub.AdPubSdk");
// Call the native setHasUserConsent method of com.adpub.AdPubSDK.
adPubSdk.CallStatic("setHasUserConsent", hasUserConsent);
}
}
}
#endif
Create a factory method to return the correct client implementation
حالا که پیادهسازیهای کلاینت را برای هر پلتفرم دارید، یک کلاس AdPubClientFactory ایجاد کنید تا پیادهسازی صحیح رابط IAdPubClient را بسته به پلتفرم زمان اجرا برگرداند. این کلاس از دستورالعملهای کامپایلر برای برگرداندن کلاینت IAdPubClient صحیح استفاده میکند.
Assets/AdPub/Common/AdPubClientFactory.cs
namespace AdPub.Common
{
public class AdPubClientFactory
{
// Return the correct platform client.
public static IAdPubClient GetClient()
{
#if !UNITY_EDITOR && UNITY_ANDROID
return new AdPub.Platforms.Android.AndroidAdPubClient();
#elif !UNITY_EDITOR && UNITY_IOS
return new AdPub.Platforms.iOS.iOSAdPubClient();
#else
// Returned for the Unity Editor and unsupported platforms.
return new DefaultClient();
#endif
}
}
}
برای هر متد رابط، یک API عمومی تعریف کنید
یک کلاس AdPubApi ایجاد کنید که برای هر متد کلاینت در رابط IAdPubClient شما فراخوانی متد داشته باشد. این کلاس از AdPubClientFactory برای دریافت نمونهای از IAdPubClient استفاده میکند و آن کلاینت را برای قابلیتهای اساسی SDK فراخوانی میکند.
Assets/AdPub/AdPubApi.cs
using AdPub.Common;
namespace AdPub
{
public class AdPubApi
{
private static readonly IAdPubClient client = GetAdPubClient();
// Returns the correct client for the current runtime platform.
private static IAdPubClient GetAdPubClient()
{
return AdPubClientFactory.GetClient();
}
// Sets the user consent using the underlying SDK functionality.
public static void SetHasUserConsent(bool hasUserConsent)
{
client.SetHasUserConsent(hasUserConsent);
}
}
}
API تازه تعریف شده خود را فراخوانی کنید
نحوه فراخوانی API تعریف شده در بالا به صورت زیر است:
Assets/Scripts/AdPubController.cs
using UnityEngine;
using AdPub;
public class AdPubController : MonoBehaviour
{
// TODO: Get consent from the user and update this userConsent field.
public bool userConsent;
// Called on startup of the GameObject it's assigned to.
public void Start()
{
// Pass the user consent to AdPub.
AdPubApi.SetHasUserConsent(userConsent);
}
}
نمونههای اضافی از آداپتور شبکه تبلیغاتی شخص ثالث
برای مشاهدهی نمونههای بیشتر از آداپتورهای واسطهی شخص ثالث که APIهای C# را برای پوشش فراخوانیها به متدهای iOS و اندروید پیادهسازی میکنند ، از مخزن گیتهاب افزونهی Google Mobile Ads Unity دیدن کنید.