天天看點

unity 中廣告banner參考

using System;
using UnityEngine;

namespace AdUnion
{
    /*
     * 4399廣告聯盟Banner   
     */
    public class AdUnionBanner
    {

        private static int MATCH_PARENT = -1;
        private static int WRAP_CONTENT = -2;

        private static int GRAVITY_TOP = 48;
        private static int GRAVITY_BOTTOM = 80;

        //廣告位ID
        private string posId;
        
        public AdUnionBanner(string posId)
        {
            this.posId = posId;
        }

        /*
         * 加載banner廣告       
         */
        public void loadBanner()
        {
            Debug.Log("loadBanner---");
            AndroidJavaObject banner = new AndroidJavaObject("com.mob4399.adunion.AdUnionBanner");

            OnAuBannerAdListenerProxy listener
            = new OnAuBannerAdListenerProxy(AdUnionContext.GetInstance().GetRootLayout());

       
            //加載廣告
            banner.Call("loadBanner",
                AdUnionContext.GetInstance().GetActivity(), posId, listener);

        }


        /*
         * Banner廣告回調方法       
         */
        class OnAuBannerAdListenerProxy : AndroidJavaProxy
        {
            private AndroidJavaObject parentContainer;

            public OnAuBannerAdListenerProxy(AndroidJavaObject parent) : base("com.mob4399.adunion.listener.OnAuBannerAdListener")
            {
                this.parentContainer = parent;
            }

            /*
           * 加載成功,傳回廣告view
           */
            void onBannerLoaded(AndroidJavaObject mBannerView)
            {
                Debug.Log("Banner loaded");

                AdUnionContext.GetInstance().RunOnUIThread(new AndroidJavaRunnable(() =>
                {

                    //廣告添加到容器中
                    AndroidJavaObject layoutParams = null;
                    layoutParams = new AndroidJavaObject("android.widget.FrameLayout$LayoutParams",
                        MATCH_PARENT, WRAP_CONTENT, GRAVITY_BOTTOM);
                    //add banner to parent container
                    parentContainer.Call("addView", mBannerView, layoutParams);

                    if (ADSMgr.Inst.curView != null)
                    {
                        ADSMgr.Inst.curView.Call("setVisibility", 8);
                        parentContainer.Call("removeView", ADSMgr.Inst.curView);
                    }
                    ADSMgr.Inst.curView = mBannerView;


                    //mBannerView.Call("setVisibility",8);
                   

                }));

            }

            /**
             * 加載失敗
             * @param message
             */
            void onBannerFailed()
            {
                Debug.Log("Banner onInterstitialLoadFailed");
            }

            /**
             *廣告被點選
             */
            void onBannerClicked()
            {

            }

            /**
             * 廣告關閉
             */
            void onBannerClosed()
            {
                Debug.Log("close Banner");
            }
        }
    }


}
           

繼續閱讀