美文网首页
Unity游戏上线Google Play渠道

Unity游戏上线Google Play渠道

作者: LEO_青蛙 | 来源:发表于2019-11-14 16:32 被阅读0次

    1、签名文件的密码不能过于简单,否则安全性太低,过不了审核

    2、Build Bundle打包出aab后缀的包体

    3、接入广告和统计SDK

    (1)接入AppsFlyer
    (2)接入MoPub
    (3)初始化AppLovin的SDK
    (4)接入FaceBook统计

    Unity游戏接入以上SDK,有两种方式:
    1、使用Unity插件,直接将SDK导入Unity工程中

    (1)下载 AppsFlyer 的 Unity 插件
    您可以在 Github 上找到该插件:https://github.com/AppsFlyerSDK/Unity
    具体接入流程,如下
    https://support.appsflyer.com/hc/zh-cn/articles/213766183-AppsFlyer-SDK-%E5%AF%B9%E6%8E%A5-Unity
    配置AndroidManifest.xml文件

    <!-- receiver should be inside the <application> tag -->
    <receiver android:name="com.appsflyer.MultipleInstallBroadcastReceiver" android:exported="true">
        <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER" />
        </intent-filter>
    </receiver>
    <!-- Mandatory permission: -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- Optional : -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    

    初始化

    public class AppsFlyerManager : MonoSingletonBase<AppsFlyerManager>
    {
    
        public override void Init()
        {
            DontDestroyOnLoad(gameObject);
    
            /* Mandatory - set your AppsFlyer’s Developer key. */
            AppsFlyer.setAppsFlyerKey("N1dYcN1umSwcECm11DVorA");
            /* For detailed logging */
            /* AppsFlyer.setIsDebug (true); */
    #if UNITY_IOS
            /* Mandatory - set your apple app ID
             NOTE: You should enter the number only and not the "ID" prefix */
            AppsFlyer.setAppID ("com.your.package");
            AppsFlyer.trackAppLaunch ();
    #elif UNITY_ANDROID
            /* Mandatory - set your Android package name */
            AppsFlyer.setAppID("com.your.package");
            /* For getting the conversion data in Android, you need to add the "AppsFlyerTrackerCallbacks" listener.*/
            AppsFlyer.init("N1dYcN1umSwcECm11DVorA", "AppsFlyerTrackerCallbacks");
    #endif
        }
    
    }
    

    (2)下载最新版本的MoPub Unity Plugin
    下载各个广告平台的适配器
    https://developers.mopub.com/publishers/mediation/integrate/
    注意选择相应的平台,这一步可能需要翻墙
    配置AndroidManifest.xml文件

    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-1628017894463920~8273413917" />
    

    初始化

    public class MopubManager : MonoSingletonBase<MopubManager>
    {
        private string itunesAppId;
        private string AdUnitId = "c9f9f94be94b47718daf79879e4b7d8b";
        private bool LocationAware;
        private bool AllowLegitimateInterest;
        private MoPub.LogLevel LogLevel = MoPub.LogLevel.Info;
    
        private MoPub.SdkConfiguration SdkConfiguration
        {
            get
            {
                var config = new MoPub.SdkConfiguration
                {
                    AdUnitId = AdUnitId,
                    AllowLegitimateInterest = AllowLegitimateInterest,
                    LogLevel = LogLevel,
                    MediatedNetworks = GetComponents<MoPubNetworkConfig>().Where(nc => nc.isActiveAndEnabled).Select(nc => nc.NetworkOptions).ToArray()
                };
                return config;
            }
        }
    
        //private readonly string[] _bannerAdUnits = { "b195f8dd8ded45fe847ad89ed1d016da" };
        private readonly string[] _interstitialAdUnits = { "24534e1901884e398f1253216226017e" };
        private readonly string[] _rewardedVideoAdUnits = { "920b6145fb1546cf8b5cf2ac34638bb7" };
        //private readonly string[] _rewardedRichMediaAdUnits = { "15173ac6d3e54c9389b9a5ddca69b34b" };
    
        public override void Init()
        {
            DontDestroyOnLoad(gameObject);
    
            //设置监听
    
            // register for initialized callback event in the app
            MoPubManager.OnSdkInitializedEvent += OnSdkInitializedEvent;
    
            MoPubManager.OnAdLoadedEvent += OnAdLoadedEvent;
            MoPubManager.OnAdFailedEvent += OnAdFailedEvent;
    
            MoPubManager.OnInterstitialLoadedEvent += OnInterstitialLoadedEvent;
            MoPubManager.OnInterstitialFailedEvent += OnInterstitialFailedEvent;
            MoPubManager.OnInterstitialDismissedEvent += OnInterstitialDismissedEvent;
            MoPubManager.OnInterstitialExpiredEvent += OnInterstitialExpiredEvent;
            MoPubManager.OnInterstitialShownEvent += OnInterstitialShownEvent;
            MoPubManager.OnInterstitialClickedEvent += OnInterstitialClickedEvent;
    
            MoPubManager.OnRewardedVideoLoadedEvent += OnRewardedVideoLoadedEvent;
            MoPubManager.OnRewardedVideoFailedEvent += OnRewardedVideoFailedEvent;
            MoPubManager.OnRewardedVideoExpiredEvent += OnRewardedVideoExpiredEvent;
            MoPubManager.OnRewardedVideoShownEvent += OnRewardedVideoShownEvent;
            MoPubManager.OnRewardedVideoClickedEvent += OnRewardedVideoClickedEvent;
            MoPubManager.OnRewardedVideoFailedToPlayEvent += OnRewardedVideoFailedToPlayEvent;
            MoPubManager.OnRewardedVideoReceivedRewardEvent += OnRewardedVideoReceivedRewardEvent;
            MoPubManager.OnRewardedVideoClosedEvent += OnRewardedVideoClosedEvent;
            MoPubManager.OnRewardedVideoLeavingApplicationEvent += OnRewardedVideoLeavingApplicationEvent;
    
            MoPubManager.OnImpressionTrackedEvent += OnImpressionTrackedEvent;
    
            //初始化
    
            MoPub.InitializeSdk(SdkConfiguration);
            MoPub.ReportApplicationOpen(itunesAppId);
            MoPub.EnableLocationSupport(LocationAware);
    
            //加载广告插件
    
            //MoPub.LoadBannerPluginsForAdUnits(_bannerAdUnits);
            MoPub.LoadInterstitialPluginsForAdUnits(_interstitialAdUnits);
            MoPub.LoadRewardedVideoPluginsForAdUnits(_rewardedVideoAdUnits);
            //MoPub.LoadRewardedVideoPluginsForAdUnits(_rewardedRichMediaAdUnits);
        }
    
        // create your handler
        private void OnSdkInitializedEvent(string adUnitId)
        {
            // The SDK is initialized here. Ready to make ad requests.
            RequestInterstitialAd();
            RequestRewardedVideo();
        }
    
        private void AdFailed(string adUnitId, string action, string error)
        {
            var errorMsg = "Failed to " + action + " ad unit " + adUnitId;
            if (!string.IsNullOrEmpty(error))
                errorMsg += ": " + error;
            Debug.LogError("Error: " + errorMsg);
        }
    
    
        // Banner Events
        private void OnAdLoadedEvent(string adUnitId, float height)
        {
            Debug.Log("OnAdLoadedEvent : " + adUnitId);
        }
    
        private void OnAdFailedEvent(string adUnitId, string error)
        {
            AdFailed(adUnitId, "load banner", error);
        }
    
        // Interstitial Events
        private void OnInterstitialLoadedEvent(string adUnitId)
        {
            Debug.Log("OnInterstitialLoadedEvent : " + adUnitId);
        }
    
        private void OnInterstitialFailedEvent(string adUnitId, string errorMsg)
        {
            AdFailed(adUnitId, "load interstitial", errorMsg);
        }
    
        private void OnInterstitialDismissedEvent(string adUnitId)
        {
            Debug.Log("OnInterstitialDismissedEvent : " + adUnitId);
        }
    
        private void OnInterstitialExpiredEvent(string adUnitId)
        {
            Debug.Log("OnInterstitialExpiredEvent : " + adUnitId);
        }
    
        private void OnInterstitialShownEvent(string adUnitId)
        {
            Debug.Log("OnInterstitialShownEvent : " + adUnitId);
        }
    
        private void OnInterstitialClickedEvent(string adUnitId)
        {
            Debug.Log("OnInterstitialClickedEvent : " + adUnitId);
        }
    
        // Rewarded Video Events
        private void OnRewardedVideoLoadedEvent(string adUnitId)
        {
            //var availableRewards = MoPub.GetAvailableRewards(adUnitId);
            Debug.Log("OnRewardedVideoLoadedEvent : " + adUnitId);
        }
    
        private void OnRewardedVideoFailedEvent(string adUnitId, string errorMsg)
        {
            AdFailed(adUnitId, "load rewarded video", errorMsg);
            if (videoCallBack != null)
                videoCallBack(false, false);
        }
    
        private void OnRewardedVideoExpiredEvent(string adUnitId)
        {
            Debug.Log("OnRewardedVideoExpiredEvent : " + adUnitId);
        }
    
        private void OnRewardedVideoShownEvent(string adUnitId)
        {
            Debug.Log("OnRewardedVideoShownEvent : " + adUnitId);
        }
    
        private void OnRewardedVideoClickedEvent(string adUnitId)
        {
            Debug.Log("OnRewardedVideoClickedEvent : " + adUnitId);
        }
    
        private void OnRewardedVideoFailedToPlayEvent(string adUnitId, string errorMsg)
        {
            AdFailed(adUnitId, "play rewarded video", errorMsg);
        }
    
        private void OnRewardedVideoReceivedRewardEvent(string adUnitId, string label, float amount)
        {
            Debug.Log("OnRewardedVideoReceivedRewardEvent : " + adUnitId);
            isReward = true;
        }
    
        private void OnRewardedVideoClosedEvent(string adUnitId)
        {
            Debug.Log("OnRewardedVideoClosedEvent : " + adUnitId);
            if (videoCallBack != null)
                videoCallBack(isReward, false);
            //播放完成,重新加载
            RequestRewardedVideo();
        }
    
        private void OnRewardedVideoLeavingApplicationEvent(string adUnitId)
        {
            Debug.Log("OnRewardedVideoLeavingApplicationEvent : " + adUnitId);
        }
    
        private void OnImpressionTrackedEvent(string adUnitId, MoPub.ImpressionData impressionData)
        {
            Debug.Log("OnImpressionTrackedEvent : " + adUnitId);
        }
    
        #region 广告调用接口
        public void RequestInterstitialAd()
        {
            MoPub.RequestInterstitialAd(_interstitialAdUnits[0]);
        }
    
        public void ShowInterstitialAd()
        {
            MoPub.ShowInterstitialAd(_interstitialAdUnits[0]);
        }
    
        public void RequestRewardedVideo()
        {
            MoPub.RequestRewardedVideo(_rewardedVideoAdUnits[0]);
        }
    
        private Action<bool, bool> videoCallBack;
        private bool isReward = false;
    
        public void ShowRewardedVideo(Action<bool, bool> callback)
        {
            isReward = false;
            videoCallBack = callback;
            if (MoPub.HasRewardedVideo(_rewardedVideoAdUnits[0]))
            {
                MoPub.ShowRewardedVideo(_rewardedVideoAdUnits[0]);
            }
            else
            {
                //没有广告,直接返回失败
                videoCallBack(false, false);
            }
        }
        #endregion
    }
    

    (3)下载AppLovin的Unity插件
    https://dash.applovin.com/docs/integration#unity3dIntegration
    修改AndroidManifest.xml文件中的package和applovin.sdk.key

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.crazy.rider.merge.game">
        <application>
            <meta-data
                android:name="applovin.sdk.key"
                android:value="Fs-cUqJfRU6DI-3nHAtCUubM2g2mHMT4kl_2_v9IyohMfXicNfA0eEwvSJ6gvrtpXtmu2TpTdL-QrLAMqwaXPS" />
        </application>
    </manifest>
    

    初始化

    public class AppLovinManager : MonoSingletonBase<AppLovinManager>
    {
        public override void Init()
        {
            DontDestroyOnLoad(gameObject);
    
    #if UNITY_EDITOR || UNITY_EDITOR_OSX
            return;
    #endif
    
            AppLovin.SetSdkKey("Fs-cUqJfRU6DI-3nHAtCUubM2g2mHMT4kl_2_v9IyohMfXicNfA0eEwvSJ6gvrtpXtmu2TpTdL-QrLAMqwaXPS");
            AppLovin.InitializeSdk();
        }
    
        public void PreloadInterstitial()
        {
            AppLovin.PreloadInterstitial();
        }
    
        public void ShowInterstitial()
        {
            if(AppLovin.HasPreloadedInterstitial())
            {
                AppLovin.ShowInterstitial();
            }
        }
    }
    
    

    (4)下载FaceBook的Unity插件
    https://developers.facebook.com/docs/unity/
    配置AndroidManifest.xml文件

    <!-- facebook -->
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="886221318440110"/>
    

    初始化

    public class FaceBookManager : MonoSingletonBase<FaceBookManager>
    {
        public override void Init()
        {
            FB.Init(onInitComplete);
        }
    
        private void onInitComplete()
        {
            Debug.Log("FB InitComplete");
        }
    
        public void SendEvent(string eventName, string eventParam)
        {
            Dictionary<string, object> eventParams = new Dictionary<string, object>();
            string[] strArr = eventParam.Split('|');
            for(int i=0; i<strArr.Length; ++i)
            {
                string[] dataArr = strArr[i].Split('-');
                eventParams.Add(dataArr[0], dataArr[1]);
            }
            LogAppEvent(eventName, eventParams);
        }
    
        private void LogAppEvent(string eventName, Dictionary<string, object> eventParams)
        {
            FB.LogAppEvent(eventName, null, eventParams);
        }
    }
    

    注意事项:
    (1)Plugins/Android文件夹中的aar、jar可能有重复引用,导致冲突,可以删除多余的包
    (2)导入的SDK插件过多,可能会导致class数量过多,不能直接编译apk,可以导出Android Studio项目,在build.gradle中添加multiDexEnabled true,拆分dex

    2、使用Unity导出Android Studio工程,通过Gradle接入SDK

    (1)引用AppsFlyer包

    repositories {
        mavenCentral()
    }
    dependencies {
        implementation 'com.appsflyer:af-android-sdk:4.9.0'
        implementation 'com.android.installreferrer:installreferrer:1.0'
    }
    

    配置AndroidManifest.xml

    <!-- receiver should be inside the <application> tag -->
    <receiver android:name="com.appsflyer.MultipleInstallBroadcastReceiver" android:exported="true">
        <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER" />
        </intent-filter>
    </receiver>
    <!-- Mandatory permission: -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- Optional : -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    

    初始化

    AppsFlyerConversionListener conversionDataListener = new AppsFlyerConversionListener() {
        @Override
        public void onInstallConversionDataLoaded(Map<String, String> map) {
            Log.v("AppsFlyer", "onInstallConversionDataLoaded");
        }
    
        @Override
        public void onInstallConversionFailure(String s) {
            Log.v("AppsFlyer", "onInstallConversionFailure");
        }
    
        @Override
        public void onAppOpenAttribution(Map<String, String> map) {
            Log.v("AppsFlyer", "onAppOpenAttribution");
        }
    
        @Override
        public void onAttributionFailure(String s) {
            Log.v("AppsFlyer", "onAttributionFailure");
        }
    };
    AppsFlyerLib.getInstance().init("N1dYcN1umSdcECm11DVorA", conversionDataListener, getApplicationContext());
    AppsFlyerLib.getInstance().setAppId("com.your.package");
    AppsFlyerLib.getInstance().startTracking(this.getApplication());
    

    (2)引用MoPub包,引用AppLovin、Facebook、AdMob和Unity Ads的适配器

    dependencies {
        //mopub all
        implementation('com.mopub:mopub-sdk:5.9.1@aar') {
            transitive = true
        }
    
        // AppLovin
        implementation 'com.applovin:applovin-sdk:9.9.2'
        implementation 'com.mopub.mediation:applovin:9.9.2.1'
    
        // Facebook Audience Network
        implementation 'com.facebook.android:audience-network-sdk:5.6.0'
        implementation 'com.mopub.mediation:facebookaudiencenetwork:5.6.0.0'
    
        // Google (AdMob & Ad Manager)
        implementation 'com.google.android.gms:play-services-ads:18.2.0'
        implementation 'com.mopub.mediation:admob:18.2.0.4'
    
        // Unity Ads
        implementation 'com.unity3d.ads:unity-ads:3.3.0'
        implementation 'com.mopub.mediation:unityads:3.3.0.1'
    
        implementation 'com.google.android.gms:play-services-identity:17.0.0'
        implementation 'com.google.android.gms:play-services-base:17.1.0'
    }
    

    配置AndroidManifest.xml文件

    <!-- MoPub's consent dialog -->
    <activity android:name="com.mopub.common.privacy.ConsentDialogActivity"     android:configChanges="keyboardHidden|orientation|screenSize"/>
    
    <!-- All ad formats -->
    <activity android:name="com.mopub.common.MoPubBrowser"  android:configChanges="keyboardHidden|orientation|screenSize"/>
    
    <!-- Interstitials -->
    <activity android:name="com.mopub.mobileads.MoPubActivity"  android:configChanges="keyboardHidden|orientation|screenSize"/>
    <activity android:name="com.mopub.mobileads.MraidActivity"  android:configChanges="keyboardHidden|orientation|screenSize"/>
    
    <!-- Rewarded Video and Rewarded Playables -->
    <activity android:name="com.mopub.mobileads.RewardedMraidActivity"  android:configChanges="keyboardHidden|orientation|screenSize"/>
    <activity android:name="com.mopub.mobileads.MraidVideoPlayerActivity"   android:configChanges="keyboardHidden|orientation|screenSize"/>
    
    <!-- Google Ads -->
    <!-- note: this is the sample app id from Google's documentation -->
    <!-- https://developers.google.com/admob/android/quick-start#update_your_androidmanifestxml -->
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-9628067894463920~8273413947" />
    
    <!-- Google Play Services -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    

    初始化

    SdkConfiguration sdkConfiguration = new SdkConfiguration.Builder(rewardedVideoAdUnit)
            .withLogLevel(MoPubLog.LogLevel.DEBUG)
            .withLegitimateInterestAllowed(false)
            .build();
    MoPub.initializeSdk(this, sdkConfiguration, new SdkInitializationListener() {
        @Override
        public void onInitializationFinished() {
            Log.v("MoPub", "onInitializationFinished");
            //Sdk初始化完成后,再加载广告
            mInterstitial.load();
            MoPubRewardedVideos.loadRewardedVideo(rewardedVideoAdUnit);
        }
    });
    
    mInterstitial = new MoPubInterstitial(this, interstitialAdUnit);
    mInterstitial.setInterstitialAdListener(new MoPubInterstitial.InterstitialAdListener() {
        @Override
        public void onInterstitialLoaded(MoPubInterstitial interstitial) {
            Log.v("MoPub", "onInterstitialLoaded");
        }
    
        @Override
        public void onInterstitialFailed(MoPubInterstitial interstitial, MoPubErrorCode errorCode) {
            Log.v("MoPub", "onInterstitialFailed : " + errorCode);
        }
    
        @Override
        public void onInterstitialShown(MoPubInterstitial interstitial) {
            Log.v("MoPub", "onInterstitialShown");
        }
    
        @Override
        public void onInterstitialClicked(MoPubInterstitial interstitial) {
            Log.v("MoPub", "onInterstitialClicked");
        }
    
        @Override
        public void onInterstitialDismissed(MoPubInterstitial interstitial) {
            Log.v("MoPub", "onInterstitialDismissed");
        }
    });
    
    rewardedVideoListener = new MoPubRewardedVideoListener() {
        @Override
        public void onRewardedVideoLoadSuccess(@NonNull String adUnitId) {
            Log.v("MoPub", "onRewardedVideoLoadSuccess");
        }
    
        @Override
        public void onRewardedVideoLoadFailure(@NonNull String adUnitId, @NonNull MoPubErrorCode errorCode) {
            Log.v("MoPub", "onRewardedVideoLoadFailure : " + errorCode);
        }
    
        @Override
        public void onRewardedVideoStarted(@NonNull String adUnitId) {
            Log.v("MoPub", "onRewardedVideoStarted");
        }
    
        @Override
        public void onRewardedVideoPlaybackError(@NonNull String adUnitId, @NonNull MoPubErrorCode errorCode) {
            Log.v("MoPub", "onRewardedVideoPlaybackError : " + errorCode);
        }
    
        @Override
        public void onRewardedVideoClicked(@NonNull String adUnitId) {
            Log.v("MoPub", "onRewardedVideoClicked");
        }
    
        @Override
        public void onRewardedVideoClosed(@NonNull String adUnitId) {
            Log.v("MoPub", "onRewardedVideoClosed");
            if(reward_verify)
                UnityPlayer.UnitySendMessage("AndroidManager", "AdsCallBack", "Success");
            else
                UnityPlayer.UnitySendMessage("AndroidManager", "AdsCallBack", "Fail");
            //观看完广告,需要重新加载广告
            MoPubRewardedVideos.loadRewardedVideo(rewardedVideoAdUnit);
        }
    
        @Override
        public void onRewardedVideoCompleted(@NonNull Set<String> adUnitIds, @NonNull MoPubReward reward) {
            Log.v("MoPub", "onRewardedVideoCompleted");
            reward_verify = true;
        }
    };
    MoPubRewardedVideos.setRewardedVideoListener(rewardedVideoListener);
    

    (3)引用AppLovin包

    dependencies {
        implementation 'com.applovin:applovin-sdk:9.9.2'
    }
    

    初始化

    AppLovinSdk.initializeSdk(this);
    /*
    AppLovinSdk.getInstance(this).getAdService().loadNextAd(AppLovinAdSize.INTERSTITIAL, new AppLovinAdLoadListener() {
        @Override
        public void adReceived(AppLovinAd ad) {
            Log.v("AppLovin", "adReceived");
        }
    
        @Override
        public void failedToReceiveAd(int errorCode) {
            Log.v("AppLovin", "failedToReceiveAd");
        }
    });
    */
    

    (4)引用FaceBook包

    dependencies {
        implementation 'com.facebook.android:facebook-android-sdk:[5,6)'
    }
    

    初始化

    //facebook数据统计
    AppEventsLogger logger = AppEventsLogger.newLogger(this);
    logger.logEvent(event, 1, StrToBundle(key_value));
    

    注意事项:
    (1)facebook的广告,需要手机安装facebook客户端,否则会加载不出广告
    (2)VPN不稳定,网络状态不好,会导致广告的填充率降低
    (3)尽量使用三星手机作为测试机

    相关文章

      网友评论

          本文标题:Unity游戏上线Google Play渠道

          本文链接:https://www.haomeiwen.com/subject/yldkictx.html