تضمين إعلانات في وضع "نافذة ضمن النافذة" (إصدار تجريبي)

اختيار النظام الأساسي: Android iOS Unity

نافذة ضمن النافذة

يتم عرض "إعلانات نافذة ضمن النافذة" في نافذة عائمة تظهر فوق المحتوى على الشاشة، مثل المقالات أو الخلاصات أو أسلوب اللعب. يتيح هذا الشكل للمستخدمين التفاعل مع تطبيقك بينما يظل الإعلان مرئيًا. اختَر هذا الشكل لعرض إعلانات صورية لا تظهر على الشاشة بأكملها. مزيد من المعلومات حول الإعلانات في وضع "نافذة ضمن النافذة"

يتناول هذا الدليل كيفية طلب "إعلانات ضمن النافذة" وعرضها في تطبيقك باستخدام Google Mobile Ads Unity Plugin.

قبل البدء

قبل المتابعة، عليك تنفيذ ما يلي:

  • فعِّل الإعلانات الاختبارية واستخدِم أرقام تعريف الوحدات الإعلانية الاختبارية التالية:

    Android

    ca-app-pub-3940256099942544/9657123429
    

    iOS

    ca-app-pub-3940256099942544/8810945611
    

تحميل إعلان

لتحميل كائن PictureInPictureAd، أنشئ طلب عرض إعلان واستدعِ طريقة التحميل:

public void LoadPictureInPictureAd()
{
    // Clean up any existing ad before loading a new one.
    if (_pictureInPictureAd != null)
    {
        _pictureInPictureAd.Destroy();
        _pictureInPictureAd = null;
    }

    // Create our request used to load the ad.
    var adRequest = new AdRequest();

    // Send the request to load the ad.
    PictureInPictureAd.Load(_adUnitId, adRequest,
        (PictureInPictureAd ad, LoadAdError error) =>
        {
            if (error != null || ad == null)
            {
                Debug.LogWarning(
                    "Picture-in-picture ad failed to load: " + error);
                return;
            }

            // Capture the PictureInPictureAd reference for later use.
            _pictureInPictureAd = ad;
        });
}

استبدِل _adUnitId برقم تعريف الوحدة الإعلانية.

عرض الإعلان

لعرض "الإعلان ضمن النافذة" على الشاشة، استخدِم طريقة العرض. يعرض المثال التالي الإعلان في الموضع التلقائي:

public void ShowPictureInPictureAd()
{
    // Use the ad reference saved from the Load callback.
    if (_pictureInPictureAd != null)
    {
        _pictureInPictureAd.Show();
    }
    else
    {
        Debug.Log("No ad to show.");
    }
}

ضبط الموضع

يعرض Google Mobile Ads Unity Plugin تلقائيًا إعلانًا في "نافذة ضمن النافذة" في أسفل يسار الشاشة عند عرضه لأول مرة، أو في آخر موضع معروف إذا تم عرضه سابقًا. لتخصيص مكان ظهور الإعلان، حدِّد موضعًا عند عرض الإعلان. يضبط المثال التالي الموضع أعلى المحتوى في الزاوية اليمنى العليا من الشاشة:

public void ShowPictureInPictureAdTopLeft()
{
    // Use the ad reference saved from the Load callback.
    if (_pictureInPictureAd != null)
    {
        _pictureInPictureAd.Show(PictureInPictureAdPosition.TopLeft);
    }
    else
    {
        Debug.Log("No ad to show.");
    }
}

للاطّلاع على جميع الوظائف المتاحة، يُرجى الانتقال إلى PictureInPictureAdPosition.

الاستماع إلى أحداث الإعلانات

للتعامل مع أحداث يتم في مراحل نشاط الإعلان في ميزة "نافذة ضمن النافذة"، عليك التسجيل في أحداث الإعلانات قبل عرض إعلانك. تسجّل هذه الأحداث التفاعلات مع الإعلانات، مثل النقرات ومرّات الظهور. تُبلِغ هذه الأحداث أيضًا عن أحداث خاصة بوضع "نافذة ضمن النافذة"، مثل وقت ظهور الإعلان أو إخفائه:

private void RegisterEventHandlers(PictureInPictureAd ad)
{
    // Raised when the ad is estimated to have earned money.
    ad.OnAdPaid += (AdValue adValue) =>
    {
        Debug.Log($"Picture-in-picture ad paid: {adValue.Value} {adValue.CurrencyCode}");
    };
    // Raised when an impression is recorded for an ad.
    ad.OnAdImpressionRecorded += () =>
    {
        Debug.Log("Picture-in-picture ad recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    ad.OnAdClicked += () =>
    {
        Debug.Log("Picture-in-picture ad recorded a click.");
    };
    // Raised when the picture-in-picture ad is displayed on the screen.
    ad.OnAdShown += () =>
    {
        Debug.Log("Picture-in-picture ad shown.");
    };
    // Raised when the picture-in-picture ad is hidden from the screen.
    ad.OnAdHidden += () =>
    {
        Debug.Log("Picture-in-picture ad hidden.");
    };
    // Raised when an ad opened full screen content.
    ad.OnAdFullScreenContentOpened += () =>
    {
        Debug.Log("Picture-in-picture ad opened full screen content.");
    };
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += () =>
    {
        Debug.Log("Picture-in-picture ad closed full screen content.");
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogWarning(
            "Picture-in-picture ad failed to open full screen " +
            "content: " + error);
    };
}

إخفاء الإعلان

لإزالة الإعلان العائم من الشاشة، استدعِ طريقة الإخفاء. تستدعي هذه الطريقة الحدث المخفي للإعلان:

public void HidePictureInPictureAd()
{
    // Use the ad reference saved from the Load callback.
    if (_pictureInPictureAd != null)
    {
        _pictureInPictureAd.Hide();
    }
    else
    {
        Debug.Log("No ad to hide.");
    }
}

تنظيف موارد الإعلانات

لتجنُّب تسرُّب الذاكرة، عليك إسقاط مرجعك إلى عنصر الإعلان عندما ينتهي تطبيقك من استخدام الإعلان. على سبيل المثال، عندما يتوقف تطبيقك عن عرض الإعلان أو التفاعل معه مرة أخرى:

public void DestroyPictureInPictureAd()
{
    // Use the ad reference saved from the Load callback.
    if (_pictureInPictureAd != null)
    {
        _pictureInPictureAd.Destroy();
        _pictureInPictureAd = null;
    }
}