MsgReceiver.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace SmartBowLib
  6. {
  7. /*
  8. * 接收来自安卓层的信息
  9. */
  10. public class MsgReceiver : MonoBehaviour
  11. {
  12. private static MsgReceiver _ins;
  13. public static MsgReceiver ins {
  14. get {
  15. if (!_ins) {
  16. GameObject o = new GameObject("MsgReceiver");
  17. _ins = o.AddComponent<MsgReceiver>();
  18. DontDestroyOnLoad(o);
  19. }
  20. return _ins;
  21. }
  22. }
  23. public void Log(string info)
  24. {
  25. Debug.Log("[MsgReceiver] " + info);
  26. }
  27. public void Warn(string info)
  28. {
  29. Debug.LogWarning("[MsgReceiver] " + info);
  30. }
  31. public void Error(string info)
  32. {
  33. Debug.LogError("[MsgReceiver] " + info);
  34. }
  35. public void OnMessage(string msg)
  36. {
  37. Debug.Log("[MsgReceiver] OnMessage: " + msg);
  38. int startIndex = 0;
  39. int len = msg.IndexOf("|");
  40. string tag = msg.Substring(startIndex, len);
  41. startIndex += len + 1;
  42. len = msg.Length - startIndex;
  43. string content = "";
  44. if (len > 0) content = msg.Substring(startIndex, len);
  45. onMessage?.Invoke(tag, content);
  46. }
  47. public Action<string, string> onMessage;
  48. public void OnLocationUpdate(string msg)
  49. {
  50. #if UNITY_ANDROID
  51. GPSTool.OnLocationUpdate();
  52. #endif
  53. }
  54. }
  55. }