NativeBindings.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. #if (UNITY_IOS || PLATFORM_ANDROID) && !UNITY_EDITOR
  9. using System.Runtime.InteropServices;
  10. using UnityEngine;
  11. namespace Immersal
  12. {
  13. public class NativeBindings
  14. {
  15. #if UNITY_IOS
  16. [DllImport("__Internal")]
  17. public static extern void startLocation();
  18. [DllImport("__Internal")]
  19. public static extern void stopLocation();
  20. [DllImport("__Internal")]
  21. public static extern double getLatitude();
  22. [DllImport("__Internal")]
  23. public static extern double getLongitude();
  24. [DllImport("__Internal")]
  25. public static extern double getAltitude();
  26. [DllImport("__Internal")]
  27. public static extern double getHorizontalAccuracy();
  28. [DllImport("__Internal")]
  29. public static extern double getVerticalAccuracy();
  30. [DllImport("__Internal")]
  31. public static extern bool locationServicesEnabled();
  32. #elif PLATFORM_ANDROID
  33. static AndroidJavaClass obj = new AndroidJavaClass("com.immersal.nativebindings.Main");
  34. #endif
  35. public static bool StartLocation()
  36. {
  37. if (!Input.location.isEnabledByUser)
  38. {
  39. return false;
  40. }
  41. #if UNITY_IOS
  42. startLocation();
  43. #elif PLATFORM_ANDROID
  44. obj.CallStatic("startLocation");
  45. #endif
  46. return true;
  47. }
  48. public static void StopLocation()
  49. {
  50. #if UNITY_IOS
  51. stopLocation();
  52. #elif PLATFORM_ANDROID
  53. obj.CallStatic("stopLocation");
  54. #endif
  55. }
  56. public static double GetLatitude()
  57. {
  58. #if UNITY_IOS
  59. return getLatitude();
  60. #elif PLATFORM_ANDROID
  61. return obj.CallStatic<double>("getLatitude");
  62. #endif
  63. }
  64. public static double GetLongitude()
  65. {
  66. #if UNITY_IOS
  67. return getLongitude();
  68. #elif PLATFORM_ANDROID
  69. return obj.CallStatic<double>("getLongitude");
  70. #endif
  71. }
  72. public static double GetAltitude()
  73. {
  74. #if UNITY_IOS
  75. return getAltitude();
  76. #elif PLATFORM_ANDROID
  77. return obj.CallStatic<double>("getAltitude");
  78. #endif
  79. }
  80. public static double GetHorizontalAccuracy()
  81. {
  82. #if UNITY_IOS
  83. return getHorizontalAccuracy();
  84. #elif PLATFORM_ANDROID
  85. return obj.CallStatic<double>("getHorizontalAccuracy");
  86. #endif
  87. }
  88. public static double GetVerticalAccuracy()
  89. {
  90. #if UNITY_IOS
  91. return getVerticalAccuracy();
  92. #elif PLATFORM_ANDROID
  93. return obj.CallStatic<double>("getVerticalAccuracy");
  94. #endif
  95. }
  96. public static bool LocationServicesEnabled()
  97. {
  98. #if UNITY_IOS
  99. return locationServicesEnabled();
  100. #elif PLATFORM_ANDROID
  101. return obj.CallStatic<bool>("locationServicesEnabled");
  102. #endif
  103. }
  104. }
  105. }
  106. #endif