This is a project sample of how to embed a Unity3D project in a native iOS or Android applications.
These instructions will get you to run a sample application with navigation between iOS/Android and Unity3D
- Unity3D 2019.2.0f1
or newerDon't use newer!! - For Android: Android studio (Latest recommended) and android sdk, Java 8
- For iOS: Xcode
- Build Unity3D project with the Export option. This will export a gradle project.
- In the Build.gradle file, change
apply plugin: 'com.android.application'
to
apply plugin: 'com.android.library'
- Remove
applicationId 'com.company.yourapp'
underdefaultConfig
- Remove
bundle
block - In
AndroidManifest.xml
remove the intent filter of the unity activity - Build the AAR file (Build -> Make project). The AAR file is under
\build\outputs\aar
- In the app - Import the AAR file we created to your app (File -> New -> New Module... and select Import .JAR/.AAR Package).
- To avoid manifest merging issues, add to
AndroidManifext.xml
under the application tag:tools:replace="android:icon,android:theme"
. You might need to addxmlns:tools="http://schemas.android.com/tools"
under the manifest tag. - In the
build.gradle
file of your app module, make sure to add the library as a dependency. Addimplementation project(":libraryName")
under dependencies. - Run the app into a device.
- Build Unity3D project with the 'export' option. This will create an xcode project.
- as Target Folder, make sure you save it to /iOS/UnityBuild
- Open
Application.xcworkspace
. Notice you have 2 project inside: the main application ('Example') and a Launcher ('UnityLauncher'). - Remove the placeholder
Data
from 'Example' ('remove reference') - From the 'UnityBuild' folder add
Data
folder to the Example application project as a reference (blue icon). - From the 'UnityLauncher' project, remove the current placeholder
Classes
andLibraries
(reference only, not 'delete') - Add
Library
andClasses
from theUnityBuild
folder to the UnityLauncherLauncher project as groups (yellow icon). - From
Library
we Added, removelibil2cpp
folder reference (Do not delete the folder, just the reference!) - From
Classes
removeDynamicLibEngineAPI.mm
andDynamicLibEngineAPI-functions.h
references. - From
Classes
openCrashReporter.mm
. Replace:
static NSUncaughtExceptionHandler* gsCrashReporterUEHandler = NULL;
extern "C" uint8_t* UnityGetAppLoadAddress()
{
// _mh_execute_header points to a mach header, and is located right at the address of where the
// app is loaded.
return (uint8_t*)&_mh_execute_header;
}
extern "C" const uint8_t * UnityGetAppLoadCommandAddress()
{
return (const uint8_t*)(&_mh_execute_header + 1);
}
extern "C" int UnityGetAppLoadCommandCount()
{
return _mh_execute_header.ncmds;
}
to
static NSUncaughtExceptionHandler* gsCrashReporterUEHandler = NULL;
static uint8_t auint8;
extern "C" uint8_t* UnityGetAppLoadAddress()
{
// _mh_execute_header points to a mach header, and is located right at the address of where the
// app is loaded.
return (uint8_t*)&auint8;
}
extern "C" const uint8_t * UnityGetAppLoadCommandAddress()
{
return (const uint8_t*)(&auint8 + 1);
}
extern "C" int UnityGetAppLoadCommandCount()
{
return 0;
}
- Select the 'UnityLauncher' Scheme, and as destination select a physical device or 'generic iOS Device'
- Build
- From the 'Example' project, remove the
UnityLauncher.framework
- in
Products
right-click theUnityLauncher.framework
and 'show in Finder' - Add the
UnityLauncher.framework
to the example project (Copy if needed) - Make sure that in the
Example
Target, tab 'general' theUnityLauncher.framework
is added to 'Embedded Binaries' - Make sure that in the
Example
Target, tab 'Build Phases' theUnityLauncher.framework
is added to 'Linked Frameworks and Libraries' and set toOptional
- Select the 'Example' scheme and make sure that the signing is valid for 'on-device' builds.
- Build and run
Note that the project is written in Objective-C, but nowadays we moved on to Swift.
Notice that in main.m
we initialize Unity with [Launcher unityInit:argc argv:(argv)];
For Swift, however, use the following (either in your AppDelegate or class that is going to implement it:)
Launcher.unityInit(CommandLine.argc, argv: CommandLine.unsafeArgv)
Make sure this is only run ONCE during your app's lifecycle.
to launch the Unity application, use Launcher.startUnity(viewController)
where viewController
is a reference to the presenting UIViewController.
This project is licensed under the MIT License - see the LICENSE.md file for details