lvjincheng hace 3 años
padre
commit
d87fe8572f

+ 38 - 10
Assets/BowArrow/Scripts/Expand/GPSTool.cs

@@ -1,25 +1,53 @@
+using System;
 using System.Collections;
 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine;
+using UnityEngine.Android;
 
 
 public class GPSTool
 public class GPSTool
 {
 {
     //GPS获取地址
     //GPS获取地址
-    //成功返回:string[]{国,省,市}
+    //成功返回:string[]{国,省,市} (数组中不允许有null)
     //失败返回:null
     //失败返回:null
-    public static string[] GetAddress()
-    {
-        string[] address = null;
-        using (var clsU3D = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
-        {
+    public static void GetAddress(Action<string[]> callback) {
+        #if UNITY_ANDROID
+        Func<string[]> func = delegate() {
+            using (var clsU3D = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
             using (var objCtx = clsU3D.GetStatic<AndroidJavaObject>("currentActivity"))
             using (var objCtx = clsU3D.GetStatic<AndroidJavaObject>("currentActivity"))
+            using (var objGPSTool = new AndroidJavaClass("com.example.smartbowlib.GPSTool"))
             {
             {
-                using (var objGPSTool = new AndroidJavaClass("com.example.smartbowlib.GPSTool"))
-                {
-                    address = objGPSTool.CallStatic<string[]>("getAddress", objCtx);
+                string[] address = objGPSTool.CallStatic<string[]>("getAddress", objCtx);
+                if (address != null) {
+                    foreach (var item in address)
+                        if (item == null) return null;
                 }
                 }
+                return address;
             }
             }
+        };
+        List<string> permissions = new List<string>();
+        permissions.Add(Permission.CoarseLocation);
+        permissions.Add(Permission.FineLocation);
+        for (int i = permissions.Count - 1; i >= 0; i--) {
+            if (Permission.HasUserAuthorizedPermission(permissions[i])) permissions.RemoveAt(i);
+        } 
+        if (permissions.Count > 0) {
+            var requestCB = new PermissionCallbacks();
+            int needPermissionCount = permissions.Count;
+            requestCB.PermissionGranted += (s) => {
+                Debug.Log("用户同意" + s);
+                needPermissionCount--;
+                if (needPermissionCount == 0) callback.Invoke(func.Invoke());
+            }; 
+            requestCB.PermissionDenied += (s) => {
+                Debug.Log("用户拒绝" + s);
+            };
+            requestCB.PermissionDeniedAndDontAskAgain += (s) => {
+                Debug.Log("用户拒绝且要求不再询问" + s);
+            };
+            Permission.RequestUserPermissions(permissions.ToArray(), requestCB);
+        } else {
+            callback?.Invoke(func.Invoke());
         }
         }
-        return address;
+        #endif
     }
     }
 }
 }

+ 11 - 10
Assets/BowArrow/Scripts/Manager/LoginMgr/RegisterView.cs

@@ -204,16 +204,17 @@ public class RegisterView : MonoBehaviour
             // };
             // };
 
 
             //2022-12-6 gps获取地理位置
             //2022-12-6 gps获取地理位置
-            string[] address = GPSTool.GetAddress();
-            if (address != null) {
-                countryCode = address[0];
-                stateCode = address[1];
-                cityCode = address[2];
-                GetInputField(registerInLocation).text = 
-                    countryCode + " " + 
-                    stateCode + " " + 
-                    cityCode;
-            }
+            GPSTool.GetAddress((address) => {
+                if (address != null) {
+                    countryCode = address[0];
+                    stateCode = address[1];
+                    cityCode = address[2];
+                    GetInputField(registerInLocation).text = 
+                        countryCode + " " + 
+                        stateCode + " " + 
+                        cityCode;
+                }
+            });
         }
         }
     #endregion
     #endregion
 }
 }

+ 13 - 0
Assets/BowArrow/Scripts/Network/UserPlayer.cs

@@ -105,6 +105,19 @@ public class UserPlayer : JCEntity
                         }
                         }
                     }
                     }
                 }
                 }
+                GPSTool.GetAddress((address) => {
+                    if (address != null) {
+                        Debug.Log("登陆时获取地理位置成功:" + string.Join(" ", address));
+                        if (LoginMgr.myUserInfo.country == address[0]
+                            && LoginMgr.myUserInfo.state == address[1]
+                            && LoginMgr.myUserInfo.city == address[2]
+                        ) return;
+                        LoginMgr.myUserInfo.country = address[0];
+                        LoginMgr.myUserInfo.state = address[1];
+                        LoginMgr.myUserInfo.city = address[2];
+                        LoginMgr.myUserInfo.Save();
+                    }
+                });
             });            
             });            
         } else {
         } else {
             handleAuthExpire();
             handleAuthExpire();

+ 11 - 10
Assets/BowArrow/Scripts/View/MeView.cs

@@ -140,16 +140,17 @@ public class MeView : MonoBehaviour, MenuBackInterface
             // };
             // };
 
 
             //2022-12-6 gps获取地理位置
             //2022-12-6 gps获取地理位置
-            string[] address = GPSTool.GetAddress();
-            if (address != null) {
-                countryCode = address[0];
-                stateCode = address[1];
-                cityCode = address[2];
-                GetInputField(inputs.transform.GetChild(6)).text = 
-                    countryCode + " " + 
-                    stateCode + " " + 
-                    cityCode;
-            }
+            GPSTool.GetAddress((address) => {
+                if (address != null) {
+                    countryCode = address[0];
+                    stateCode = address[1];
+                    cityCode = address[2];
+                    GetInputField(inputs.transform.GetChild(6)).text = 
+                        countryCode + " " + 
+                        stateCode + " " + 
+                        cityCode;
+                }
+            });
         }
         }
     #endregion
     #endregion