In the realm of augmented reality, the journey of transforming an idea into a flagship feature can be both exhilarating and daunting. Initially, the integration of ARCore using Kotlin seemed straightforward, but as the project matured, the need for a more robust solution became apparent. The quest for a more efficient approach led to the discovery that Unity could facilitate the same feature with less complexity, albeit with a learning curve. This revelation sparked a renewed interest in exploring the potential of Unity for the project.
As I embark on this journey to integrate AR capabilities into the app, it’s essential to document the challenges faced along the way. This will serve as a guide for others who may venture into similar territory.
Project creation
The first step involves creating a Geospatial sample project within Unity. While numerous tutorials are available online, I’ll highlight some common pitfalls encountered during the setup process. Before diving into the videos, it’s advisable to familiarize yourself with the following steps to streamline your experience:
- Start your project as a 3D (Built-In Renderer Pipeline) to avoid complications like a black screen after building.
- Switch the platform to Android by navigating to File > Build Settings and selecting Android.
After adjusting the platform settings, follow the video tutorial until you reach the point of adding a 3D object. Here’s a quick checklist to ensure a smooth start:
- Download and import the Cesium and ARCore Extensions packages.
- Import the Geospatial sample.
- Set Project Settings (Android Version — API level 28, Scripting Backend — IL2CPP, uncheck ARMv7, check ARM64, uncheck Auto Graphics, and remove Vulkan).
- In XR Plug-in Management, enable Google ARCore.
- Enable Geospatial and Geospatial Creator in ARCore Extensions.
- Activate ARCore API and Map Tiles API in the Google Cloud console, and paste the new API key in Unity ARCore Extensions.
- Add a sample scene, such as Geospatial Arf 4, to the Hierarchy and remove any other objects.
- Add AR Geospatial Creator Origin and set the API Key.
- Identify the location for your 3D object.
With these steps completed, you can proceed with the tutorial and visualize your chosen point on the map.
3D Model integration
Next, it’s time to select a 3D model. The Unity Store offers a plethora of options, and for this example, a free skeleton model will suffice. After downloading, you can easily import it into your project.
Once the model is in your Project structure, drag it into the Hierarchy. Initially, the model may not appear on the map, so you’ll need to position it correctly:
- Select the 3D model in the Hierarchy and click on Add Component.
- Choose AR Geospatial Creator Anchor to anchor your model.
- Adjust the position and scale, setting the altitude type to Terrain and inputting the longitude and latitude.
- Set Altitude relative to terrain to zero and experiment with Override terrain altitude to ensure visibility.
With adjustments made, you should see your model positioned accurately on the map.
Add Animation
To breathe life into your model, adding animation is the next step. If your project lacks an Animation Controller, create one by right-clicking within the folder containing your animations and selecting Create > Animator Controller.
Once created, you can add animations to the Base Layer and establish transitions between them. In the Inspector window, assign the new Animation Controller to your 3D model’s Animator component.
After setting up the animations, it’s time to run the build. However, be prepared for potential hiccups. If the build hangs on “Scene 0…”, a workaround involves adjusting preferences in Unity to ensure dependencies are recognized. Following this, you can proceed to build your .apk and install it on a test device to witness the culmination of your efforts.
Importing Unity as a library to Android project
Creating a new Android project is the next phase, where you’ll import your Unity project into a designated folder. Ensure the minSdk version aligns with your Unity project settings.
A critical step involves navigating to Unity’s Assets > External Dependency Manager > Android Resolver and deleting resolved libraries. This prevents unnecessary dependencies from complicating your integration.
Once the Unity project is exported, you can close Unity and shift your focus to Android Studio. Include the unityLibrary in your project’s settings.gradle file and make necessary adjustments in the build.gradle files to ensure compatibility.
As you progress, you may encounter issues related to NDK visibility. Modifying the build.gradle file in the unityLibrary folder to specify the direct path to the Unity pre-installed NDK can resolve this.
With the app now set to launch the Unity activity, you may face a crash due to a missing string resource. Adding a string to strings.xml will rectify this issue, allowing you to run the app without a black screen.
With perseverance and attention to detail, the integration of Unity into your Android project can lead to a successful outcome, opening doors to new possibilities in augmented reality development.