Unity native GPS plugin (iOS and Android)

Versions:

Agenda:

  1. Prepare Unity project,
  2. Make iOS plugin,
  3. Implement iOS plugin in Unity,
  4. Test iOS plugin,
  5. Make Android plugin,
  6. Implement Android plugin in Unity,
  7. Test Android plugin.

Prepare Unity project

  1. Create folder and name it unity-native-gps-plugin,
  2. In this folder create Unity project and name it ‘Unity’,
  3. Open Unity project and rename main scene to NativeGPS,
  4. Create two folders in Assets. Plugins/Android and Plugin/iOS,
  5. You can change game resolution to something more like a phone display,
  6. On the scene, create Canvas with the image object stretched across the entire view. This will be the background. And a text object where all the information will be displayed,
  7. Create Scripts folder in Assets and two scripts. NativeGPSPlugin.cs and NativeGPSUI.cs. NativeGPSUI.cs add as a component to Canvas.
Point 5
Point 6 and 7

Make iOS Plugin

  1. Fist of all, change platform to iOS,
  2. In PlayerSettings add descriptions for usages,
  3. In this story i’ll show you how to test an iOS app on a simulator. Therefor, change the Target SDK to Simulation SDK. But you can check how it works on real device and leave Device SDK,
  4. In Assets/Plugins/iOS create LocaleTools.mm and LocaleTools.h.
Pont 1
Point 2
Point 3
Point 4

Implement iOS plugin in Unity

Test iOS plugin

  1. Add NativeGPS scene to build scene list,
  2. Build project,
  3. Run on device,
  4. Allow access to location.
Point 1
Point 4

Make Android plugin

  1. Create a new project with or without activity,
  2. Set the package name and project location. I recommend to naming project Android. The minimum SKD is important. If you set it to API 27, your plugin will only work with device with API 27 or higher,
  3. Change view from Android to Project,
  4. Add new module (our plugin),
  5. Create the module as an Android Library. The package name is important as you will be use them in Unity,
  6. Change your view to Android and check if your hierarchy looks the same.
Point 2
Point 3
Point 4
Point 5
Point 6
  1. Copy classes.jar into LocaleTools/libs. The libs folder is only displayed in project view,
  2. Load library,
  3. Add to LocaleTools,
  4. Check if classes.jar is unpacked. Compare with the photo below.
Point 1
Point 2
Point 3
Point 4
Main.java
Location.java

Implement Android plugin in Unity

  1. In the singleton init method, you should construct the AndroidJavaClass class with a parameter pointing to the Main class from the LocaleTools plugin,
  2. In the StartLocation method, add a connection to android plugin startLocation method,
  3. Make a handy Get method for all getters,
  4. Call get method in GetLatitude.
Point 1
Point 2
Point 3
Point 4
  1. Add using for Android,
  2. Add two properties,
  3. Update start method. Now it should be able to check if the location permission is true and if so, call StartLocation. There you will find a recommendation from Unity https://docs.unity3d.com/Manual/android-RequestingPermissions.html ,
  4. Create PermissionsRationaleDialog.cs,
  5. In NativeGPSUI create onGUI method for PermissionsRationaleDialog.cs,
  6. Finally you need to change API level like in plugin and you can build.
Point 1
Point 2
Point 3
Point 4
Point 5
Point 6

Test Android plugin

--

--

--

I’m AR passionate

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Clean Code Basics — Avoid comments and choose better names

Git: RPC failed Error 413

Implementing Beacon on Apple Watch

Level-Up Your Deployment Strategy with Canarying in Kubernetes

How to quickly merge current branch into master using cmd

Magneto 2: Trying to build Split Database Master and Slave for Magento2 CE (Part 1)

Lessons learned: Moving from the Surface Duo emulator to an actual device

My journey with software development.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store

Marcin Kulwicki

I’m AR passionate

More from Medium

iOS vs Android Development for Beginners

What Are The Top Trends of Android Application Development?

Symmetric cryptography differences between iOS and Android

Why column wrapped inside the center is not centered vertically in flutter?