|
|
@@ -26,28 +26,54 @@ public class GPSTool
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+ #if UNITY_ANDROID
|
|
|
+ private static Action<string[]> callbackRecord;
|
|
|
+ public static void OnLocationUpdate()
|
|
|
+ {
|
|
|
+ if (callbackRecord != null)
|
|
|
+ {
|
|
|
+ GetAddress(callbackRecord);
|
|
|
+ callbackRecord = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #endif
|
|
|
//GPS获取地址
|
|
|
//成功返回:string[]{国,省,市} (数组中不允许有null)
|
|
|
//失败返回:null
|
|
|
public static void GetAddress(Action<string[]> callback)
|
|
|
{
|
|
|
#if UNITY_ANDROID
|
|
|
- Func<string[]> func = delegate ()
|
|
|
+ Action callAndroidMethod = delegate ()
|
|
|
{
|
|
|
- if (Application.isEditor) return null;
|
|
|
+ if (Application.isEditor) return;
|
|
|
+ string[] address = null;
|
|
|
using (var clsU3D = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
|
|
using (var objCtx = clsU3D.GetStatic<AndroidJavaObject>("currentActivity"))
|
|
|
using (var objGPSTool = new AndroidJavaClass("com.example.smartbowlib.GPSTool"))
|
|
|
{
|
|
|
- string[] address = objGPSTool.CallStatic<string[]>("getAddress", objCtx);
|
|
|
+ bool gpsOpened = objGPSTool.CallStatic<bool>("hasOpenGPS", objCtx);
|
|
|
+ if (!gpsOpened)
|
|
|
+ {
|
|
|
+ var view = OpenGPSView.Init();
|
|
|
+ if (view) view.onOpened = () => GetAddress(callback);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ address = objGPSTool.CallStatic<string[]>("getAddress", objCtx);
|
|
|
if (address != null)
|
|
|
{
|
|
|
foreach (var item in address)
|
|
|
- if (item == null) return null;
|
|
|
+ {
|
|
|
+ if (item == null)
|
|
|
+ {
|
|
|
+ address = null;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- return address;
|
|
|
+ if (address != null) callback?.Invoke(address);
|
|
|
+ //如果安卓层未能获取到位置(比如gps刚打开),会等待gps更新,至到知道位置后,会通知Unity
|
|
|
+ //因此可以先保留callback用于下次回调
|
|
|
+ else if (address == null) callbackRecord = callback;
|
|
|
}
|
|
|
};
|
|
|
List<string> permissions = new List<string>();
|
|
|
@@ -65,7 +91,7 @@ public class GPSTool
|
|
|
{
|
|
|
Debug.Log("用户同意" + s);
|
|
|
needPermissionCount--;
|
|
|
- if (needPermissionCount == 0) callback.Invoke(func.Invoke());
|
|
|
+ if (needPermissionCount == 0) callAndroidMethod.Invoke();
|
|
|
};
|
|
|
requestCB.PermissionDenied += (s) =>
|
|
|
{
|
|
|
@@ -79,7 +105,7 @@ public class GPSTool
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- callback?.Invoke(func.Invoke());
|
|
|
+ callAndroidMethod.Invoke();
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
@@ -93,5 +119,4 @@ public class GPSTool
|
|
|
|
|
|
|
|
|
}
|
|
|
-
|
|
|
}
|