如何检测Unity是否安装了Google Play服务?

时间:2015-09-22 15:58:23

标签: c# android unity3d google-play-services

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

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

1 个答案:

答案 0 :(得分:4)

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

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