如何检测是否从 Unity 安装了 Google Play 服务? [英] How to detect if Google Play Services is installed from Unity?

查看:157
本文介绍了如何检测是否从 Unity 安装了 Google Play 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些设备未安装 Google Play 服务,因此我无法使用 Google Play 游戏服务等 API.在 Unity 中,如何检测未安装 Play 服务,因此我必须跳过调用 GPGS?

Some devices do not have Google Play Services installed, so I can't use APIs like Google Play Game Services. From Unity, how can I detect that Play Services is not installed so I have to skip calling GPGS?

我知道我需要调用 GoogleApiAvailability.isGooglePlayServicesAvailable,但我需要来自 C#.

I know I need to invoke GoogleApiAvailability.isGooglePlayServicesAvailable, but I need to from C#.

推荐答案

您需要使用 Unity 中的 JNI 支持来从 C# 调用 Java API.在这种情况下,对 isGooglePlayServicesAvailable 的调用将如下所示:

You need to use the JNI support in Unity to invoke Java API calls from C#. In this case the call to isGooglePlayServicesAvailable would look like:

 public bool IsPlayServicesAvailable()
        {
            const string GoogleApiAvailability_Classname = 
                "com.google.android.gms.common.GoogleApiAvailability";
            AndroidJavaClass clazz = 
                new AndroidJavaClass(GoogleApiAvailability_Classname);
            AndroidJavaObject obj = 
                clazz.CallStatic<AndroidJavaObject>("getInstance");

            var androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            var activity = androidJC.GetStatic<AndroidJavaObject>("currentActivity");

            int value = obj.Call<int>("isGooglePlayServicesAvailable", activity);

            // result codes from https://developers.google.com/android/reference/com/google/android/gms/common/ConnectionResult

            // 0 == success
            // 1 == service_missing
            // 2 == update service required
            // 3 == service disabled
            // 18 == service updating
            // 9 == service invalid
            return value == 0;
        }

这篇关于如何检测是否从 Unity 安装了 Google Play 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆