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

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

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

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

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

قبل البدء

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

تحميل إعلان

لتحميل عنصر 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 AdManagerAdRequest();

    // 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;
    }
}