Getting Started
Introduction⌗
When working with Unity on Android, it’s sometimes desirable to access Android features from within the Unity application. Fortunately, Unity provides a system for C#<->Java interop so that developers can interact with AAR libraries as Android Plugins. In order to create easily reusable plugins, it is sensible to package a plugin, along with its C# wrapper code, in a Unity package. This series will walk through all the steps necessary to create a Unity package with an integrated Android plugin, and build and publish it with Github Actions. The source code for the example package is available here.
Prerequisites⌗
This series assumes a basic understanding of C# and Java, as well as the following tools and accounts:
- Unity (with an existing project targeting Android)
- An IDE compatible with Unity, e.g. Visual Studio, Jetbrains Rider, or Visual Studio Code
- Android Studio
- Git
- Github account
- NPM account
Project Layout⌗
Before getting into the details of the package, let’s layout the project directories and do some housekeeping.
First, create a directory for the package. I tend to follow the reverse domain naming structure similar to Java or built-in Unity packages, i.e. com.{organization}.{packageName}
. Using this naming structure has the added benefit of being able to scope registries within the Unity Package Manager.
Inside this directory, you’ll need to create a directory for the Android plugin project, which should be named android~
. Unity ignores all directories containing the ~
symbol by default, so this will help to keep the directory structure inside Unity clean and organized. You’ll also need a Runtime/Scripts
directory for the Unity scripts, and Plugins/Android
for the compiled Android plugin. For Github Actions support, create a .github/workflows
directory.
com.dcgoodnow.AndroidUnityPackage/
├── android~/
├── Plugins/
│ └── Android/
├── Runtime/
│ └── Scripts/
└── .github/
└── workflows/
Package Manifest⌗
At this point, you can create a few files needed for publishing the package with NPM. First, create the manifest, or package.json
, that will describe the package. Feel free to use the example below as a template, but be sure to update it with your own package name and author information.
{
"name": "com.dcgoodnow.androidunitypackage",
"version": "0.0.0",
"displayName": "Android Unity Package",
"description": "This is an example of a package containing an Android plugin.",
"unity": "2022.2",
"dependencies": {
},
"keywords": [
"Unity",
"Android"
],
"author": {
"name": "Daniel Goodnow",
"email": "danielgoodnow@gmail.com",
"url": "https://www.danielgoodnow.com"
}
}
Ignore Files⌗
Additionally, you should configure a .npmignore
file to exclude the Android source files and Github Actions from the published package.
android~/
.github/workflows/
.gitignore
file, use the base Unity .gitignore with the following modifications:
Plugins/Android/*.aar
Plugins/Android/*-debug.aar.meta
{pluginName}-{buildVariant}.aar
, and it’s important to keep the .meta file generated by Unity for the release variant of the plugin. To achieve this, only the debug variant .meta file is ignored in git.Android Studio Setup⌗
Lastly, you’ll need to configure Android Studio to recognize the android~
folder, as it ignores files and folders with ~
by default. Open Android Studio and navigate to File -> Settings -> Editor -> Filetypes, then remove the *~
line in the ‘Ignored Files and Folders’ section if present.
In the next article, I’ll explain how to create the Android plugin.