[Music]
hello and welcome to unity as a library
insert unity-powered features directly
into your app
my name is dan miller and i'm a senior
developer advocate focused on xr
which mostly means that i focus on
augmented and virtual reality content
today i'm going to be talking about
unity as a library
and i'll be sure to highlight some
different features and how you can
enable
augmented reality inside your app
and when we talk about apps we're really
talking about native applications here
initially on ios and android but we've
also expanded it to native windows apps
as well
with the power of unity you're able to
add augmented reality
2d 3d real-time features and rendering
and more this can really add another
layer of depth
as well as enable all the different
tools and features we have in unity
directly into a native app
let's go over the agenda first we'll
talk a little bit about what is unity as
a library
next we'll dive into getting started
with unity and today we're mostly just
going to be focused on the mobile side
first highlighting integration with ios
then
integration with android finally we'll
end with some things to consider
as well as some additional links where
you can find out more
as well as get access to some sample
projects
let's talk a little bit about what is
unity as a library
its core it's the ability to bundle
unity and reference it in native
platforms
as a library it's currently supported on
ios
android windows and the universal
windows platform
a key feature is being able to pass data
between unity
and the native app on the unity side
you communicate with native applications
or native apis
just as you would during any other app
development
on the native side you're able to pass
in and feed data to unity
using some built-in and custom apis that
we've enabled when linking unity
through a library some limitations and
things to consider
currently unity as a library is only
supported for full screen rendering on
mobile
this means that your application will
take up the entire screen
also you can only load one instance of
unity at a time
now unity does support things like
multi-scene and you can bundle and load
that up together
but keep it in mind that you can only
ever have one instance of unity loaded
as a library in an app at a time
and lastly when unity is in an unloaded
state
after calling application.unload it does
retain a little bit of
memory and that's something to keep in
mind as you continue to
load or unload unity and consider the
app
flow and how it works linking in unity
as a library
now let's go ahead and start talking
about getting started with unity
and enabling your project to build as a
native application and be referenced as
a library
i'm not going to cover the specifics of
setting up different build environments
since we've already added the specific
modules as well as enabled the
development environment here
for native development here we're just
going to be focusing on the different
settings that you'll be adjusting with
unity on the android side
you'll want to enable the checkbox here
in the build settings
that allows unity to export as a gradle
project
this is what you'll be linking into your
native android application
once it's built and compiled here we're
naming the project
android build and putting it inside the
unity project
this is something to keep in mind as
we'll be using some of these file paths
to directly link in the android build
inside our native android application
here we are inside android studio we've
loaded up a native android app
the first thing we need to do is adjust
a couple of the settings on the gradle
scripts
in order to properly link in our
pre-built unity android gradle project
first we'll go to the settings.gradle
file
here we're going to include unity
library as well as the project unity
library
passing in the project directory to the
file path next
we'll open the build.gradle module
here we're going to add some additional
dependencies in the first
upper block first the implementation
project
unity library next the implementation
file tree which again has the directory
the project unity library
and passing in the libs folder
to include the jar file types
finally we've opened up the build.gradle
project
next we'll add a directory to the
repositories
here we're adding the unity library and
then the project directory
libs
finally we'll hit the sync button which
will then sync all our additional gradle
script changes
and allow us to build and compile this
application
here you can see the native application
that we've built there's two buttons for
showing unity and finishing
when clicking the show unity button
you'll see an instance of unity launch
here we can see the default unity splash
screen and now some additional buttons
both on the native side as well as
within the unity application itself
notice how the application is running
full screen we can select different
buttons to change the color of the cube
as well as send a message back to the
native side using the send message
button
we can also unload the application which
allows us to re-initialize or
reload unity and here's the different
functionality available
in this default unity as a library
example app
next i want to briefly talk about the
interop
and communicating with a native app
inside a unity
first you'll need to put your native
files directly in the plugins folder
within the assets directory here you can
see we have one for ios as well as
android
on the android side we have a override
unity activity java file
here in the java file you can see that
we have a reference to the override
unity activity instance
as well as some abstract protected
methods for update unity shop item
and item placed in ar here we also have
some overrides
for the protected void oncreate as well
as on destroy methods
this script is pulled from the ar sample
for unity as the library on the c sharp
side
here we are in our product manager class
within the on placed method we have a
try catch block here
that's if deft for unity android
specifically
here we're getting a reference to that
override activity
android java class then getting a
reference to the instance
next we use the overrideactivity.call
method
and pass in the method name in this case
itemplacednar
next in the catch we catch the exception
and log an error
if an exception occurs similarly
in the update shop item method we have
another if-def android block
and another try catch doing a similar
call here
to get the android java class overriding
the activity
and using overrideactivity.com for
update ud shop item
and finally catching the exception and
logging the air
now let's shift focus a bit and talk
about ios
first we'll start off similarly building
the xcode project
inside the unity project
here we'll open up a native ios project
and create what we call
a workspace we'll name that workspace
both
and we'll add the native ios project
as well as the built unity project to
that workspace
next on the native ios app we'll go to
the general settings
and the frameworks library and embedded
content
here we can hit the plus button and add
the unity framework
next we'll go to the build phases and
under the link binary with libraries
we'll actually remove
the unityframework.framework
finally we'll hop into the unity project
and adjust
some settings on the target membership
of a couple files
here we'll first select the
nativecallproxy.h
these are the files that we put in our
plugins folder when building this
unity app we'll want to make sure that
the target membership is the unity
framework and that it's set to public
next we can select the data folder we'll
also want to make sure this target
membership is on unity framework if it's
not already selected
we'll also want to uncheck unity-iphone
so just the unity framework is in the
target membership of the data folder
next if we've correctly signed both
applications and projects we should be
able to select the target device
now we can build and run this native
project linking in
unity as a library on ios
let's take a look at how to interact
between native code in unity
and the ios platform here we have a
header
file that we start off by first defining
a protocol
as well as our required methods for the
update unity shop item
and item placed in ar finally we have an
interface at the bottom
to register api for native calls
here in the implementation file you see
we have the
id for the api as well as the
definitions for registering the api
calls and the extern c for the update
unity shop items and item placed in ar
here in c-sharp we first need to make
sure that we're using the system runtime
interop
services namespace here we've if-deft it
so it's only included for ios
we also have a public class for native
api where we use the dll
import attribute and define the public
static extern
void methods for the update unity shop
item
and the item placed in ar finally at the
bottom
we have else if deft for unity ios
and here we're simply calling native api
dot item placed in ar
and in the next method update shop item
similar if theft or else if theft
nativeapi.unity.updateunityshopitem
when getting started with ios there's a
couple things to consider
right now this is only supported on
xcode 9.4
when thinking about unity we suggest
using the latest lts
or the latest stable release so 2019.4
or 2020.1 now let's talk about some of
the requirements for android here we
have
again some requirements for the android
studio version
3.4.2 and similarly
recommend using the unity lts 2019.4
a couple things to consider when
developing unity as a library
is how complex your unity application is
if you're using
lots of third-party or native plugins
you might have to adapt them both native
and managed
to work with the unity runtime lastly i
wanted to end with a couple sample
applications
here we have a unity as a library
example
just a base implementation it has some
documentation for the different steps of
linking unity as a library for both ios
and
android and has a sample app that's
built
on the native platforms for both ios and
android as well
next is the ual or unity as a library
ar example this is a bit more complex
example
that allows the user to interface with a
native application
for shopping and then visualize products
in
augmented reality here you can select
different colors do some customizations
and transfer data and information from
the native side
back to the unity side
thank you for attending this unite now
session about unity as a library
i hope you've learned a lot and help
build your unity skills and advance your
projects
finally be sure to check out the unite
now page for other sessions
that are sure to inform and inspire you
you