Browse Source

安卓gps优化

lvjincheng 3 năm trước cách đây
mục cha
commit
ffc9f10c0f

+ 7 - 0
Assets/BowArrow/Modules/ScreenProjection/MsgReceiver.cs

@@ -51,5 +51,12 @@ namespace SmartBowLib
         }
 
         public Action<string, string> onMessage;
+
+        public void OnLocationUpdate(string msg)
+        {
+            #if UNITY_ANDROID
+            GPSTool.OnLocationUpdate();
+            #endif
+        }
     }   
 }

+ 5 - 0
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/Resources/TextAutoLanguage2/cn.json

@@ -7,6 +7,11 @@
     "common_del": "删除",
     "common_yes": "是",
     "common_no": "否",
+
+    "gps_notopen": "您还没打开GPS开关",
+    "gps_go-to-open": "前往打开",
+    "gps_giveup-open": "不打开了",
+
     "login_placeholder_user": "请输入您的账号",
     "login_placeholder_password": "请输入您的密码",
     "login_placeholder_graphicCode": "请输入图形验证码",

+ 5 - 0
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/Resources/TextAutoLanguage2/en.json

@@ -7,6 +7,11 @@
     "common_del": "Delete",
     "common_yes": "Yes",
     "common_no": "No",
+
+    "gps_notopen": "You haven't turned on the GPS switch",
+    "gps_go-to-open": "Go to open",
+    "gps_giveup-open": "Not open",
+
     "login_placeholder_user": "Please Input Your Username",
     "login_placeholder_password": "Please Input Your Password",
     "login_placeholder_graphicCode": "Please Input GraphicCode",

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

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

+ 2 - 0
Assets/BowArrow/Scripts/GameBootListener.cs

@@ -17,6 +17,8 @@ public class GameBootListener
         Debug.Log("AudioMgr Inited");
         PersistenHandler.Init();
         Debug.Log("PersistenHandler Inited");
+        SmartBowLib.MsgReceiver.ins.ToString();
+        Debug.Log("MsgReceiver Inited");
         Debug.Log("===游戏初始化完成===");
     }
 }

+ 3 - 2
Assets/BowArrow/Scripts/View/ModalView.cs

@@ -13,6 +13,7 @@ public class ModalView : MonoBehaviour
     public string onAgreeTextKey;
     public UnityAction onReject;
     public string onRejectTextKey;
+    public bool willDestroyAfterClick = true;
 
 
     public static ModalView Show()
@@ -34,12 +35,12 @@ public class ModalView : MonoBehaviour
         Transform btnReject = transform.Find("Frame/BtnReject");
         btnReject.GetComponent<Button>().onClick.AddListener(() => {
             onReject?.Invoke();
-            Destroy(gameObject);
+            if (willDestroyAfterClick) Destroy(gameObject);
         });
         Transform btnAgree = transform.Find("Frame/BtnAgree");
         btnAgree.GetComponent<Button>().onClick.AddListener(() => {
             onAgree?.Invoke();
-            Destroy(gameObject);
+            if (willDestroyAfterClick) Destroy(gameObject);
         });
         if (!string.IsNullOrEmpty(onAgreeTextKey)) {
             btnAgree.GetComponentInChildren<TextAutoLanguage2>().SetTextKey(onAgreeTextKey);

+ 88 - 0
Assets/BowArrow/Scripts/View/OpenGPSView.cs

@@ -0,0 +1,88 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class OpenGPSView : MonoBehaviour
+{
+    private AndroidJavaClass _clsU3D;
+    private AndroidJavaObject _objCtx;
+    private AndroidJavaClass _objGPSTool;
+    public System.Action onOpened;
+    private static OpenGPSView ins;
+    public static OpenGPSView Init()
+    {
+        if (ins) return null;
+        ModalView modalView = ModalView.Show();
+        OpenGPSView openGPSView = modalView.gameObject.AddComponent<OpenGPSView>();
+        modalView.textKey = "gps_notopen";
+        modalView.onAgreeTextKey = "gps_go-to-open";
+        modalView.onRejectTextKey = "gps_giveup-open";
+        modalView.willDestroyAfterClick = false;
+        modalView.onAgree = () => openGPSView.GoToOpenGPS();
+        modalView.onReject = () => {
+            modalView.willDestroyAfterClick = true;
+        };
+        return openGPSView;
+    }
+
+    void Awake()
+    {
+        ins = this;
+    }
+
+    void Start()
+    {
+        _clsU3D = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
+        _objCtx = _clsU3D.GetStatic<AndroidJavaObject>("currentActivity");
+        _objGPSTool = new AndroidJavaClass("com.example.smartbowlib.GPSTool");
+    }
+
+    void OnDestroy()
+    {
+        if (ins == this) ins = null;
+
+        if (_objGPSTool != null)
+        {
+            _objGPSTool.Dispose();
+            _objGPSTool = null;
+        }
+        if (_objCtx != null)
+        {
+            _objCtx.Dispose();
+            _objCtx = null;
+        }
+        if (_objCtx != null)
+        {
+            _objCtx.Dispose();
+            _objCtx = null;
+        }
+
+        onOpened = null;
+    }
+
+    float time = 0;
+    void Update()
+    {
+        time += Time.deltaTime;
+        if (time > 0.1f) {
+            time = 0;
+            CheckOpenGPS();
+        }
+    }
+
+    void CheckOpenGPS()
+    {
+        if (_objGPSTool.CallStatic<bool>("hasOpenGPS", _objCtx))
+        {
+            Destroy(gameObject);
+            Debug.Log("onOpenGPS");
+            onOpened?.Invoke();
+            Debug.Log("onOpenGPS-Callback-Invoke");
+        }
+    }
+
+    void GoToOpenGPS()
+    {
+        _objGPSTool.CallStatic("goToOpenGPS", _objCtx);
+    }
+}

+ 11 - 0
Assets/BowArrow/Scripts/View/OpenGPSView.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 01e48addb5542fc40a394009f1363822
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

BIN
Assets/Plugins/Android/smartbowlib-debug.aar