Slide 1

Slide 1 text

Game Development with Unity from an Android Point of View Place your screenshot here Julien Salvi

Slide 2

Slide 2 text

Android addict since Froyo Punk rock & IPA! You can find me at @JulienSalvi Julien Salvi Senior Android Engineer @ Innovorder

Slide 3

Slide 3 text

× Game Development on Android × Building Unity Plugins × Unity from Android Studio × What’s next?

Slide 4

Slide 4 text

1. Game Development on Android A global overview

Slide 5

Slide 5 text

Place your screenshot here Major Game engines for Android

Slide 6

Slide 6 text

“More than 50% of new mobile games are made with unity!”

Slide 7

Slide 7 text

Place your screenshot here Trusted by many studios

Slide 8

Slide 8 text

× Available on Windows and Mac (soon on Linux) × 2D, 3D games or AR/VR apps × Fast prototyping, fast scripting × Supporting most of the 3D and 2D formats Great Asset Store! Game development on Android Building Android apps with Unity

Slide 9

Slide 9 text

× OpenGL and Vulkan support × Bridge to native platforms × Android Studio project export × Java and Kotlin support Game development on Android Building Android apps with Unity

Slide 10

Slide 10 text

2. Building Unity Plugins Android to the rescue!

Slide 11

Slide 11 text

Building Unity plugins Game Developers with Unity × Build apps with C# scripts × Control the rendering pipeline × Cross-platform shader compilation Bring the best experience at a high frame rate!

Slide 12

Slide 12 text

For our fellow Unity developers

Slide 13

Slide 13 text

“Building Plugins for Unity” 13

Slide 14

Slide 14 text

× Build your plugin using Java/Kotlin or C/C++ × Android plugins might be: .jar, .aar or .so × Add Java or Kotlin classes directly into your Unity Project Build an Android Plugin for Unity

Slide 15

Slide 15 text

Android Plugin for Unity For example let us build a player plugin for Unity Call player methods from Unity

Slide 16

Slide 16 text

Unity They are best friends! Let’s eat some C# and C++! 16

Slide 17

Slide 17 text

J N I Java / Kotlin code C/C++ code Plugin

Slide 18

Slide 18 text

public class PlayerBridge { private Context ctx; public PlayerBridge(Context ctx) {} public void addSubtitles(String path) {} public void doOnMainThread(boolean playWhenReady) {} public int getTrackCount() {} } Android Plugin for Unity

Slide 19

Slide 19 text

Calling a JAVA/Kotlin object from Unity AndroidJavaObject localMediaPlayer = null; using (AndroidJavaClass javaUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { using (currentActivity = javaUnityPlayer.GetStatic("currentActivity")) { localMediaPlayer = new AndroidJavaObject("my/plugin/vr/PlayerBridge", currentActivity); if (localMediaPlayer != null) { // Do some work with your java object outside the Android UI thread localMediaPlayer.Call("addSubtitles", subtitleURL); int count = localMediaPlayer.Call("getTrackCount"); currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(() => { // Do some work on Android UI thread localMediaPlayer.Call("doOnMainThread", true); })); } } }

Slide 20

Slide 20 text

Function inside your plugin Define the plugin function in your Unity script extern "C" { float FooPluginFunction (); } C/C++ Plugin [DllImport ("PluginName")] private static extern float FooPluginFunction ();

Slide 21

Slide 21 text

Better performances when you call your Java plugin through the JNI Important: JVM loading jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* jni_env = 0; vm->AttachCurrentThread(&jni_env, 0); return JNI_VERSION_1_6; } Java and C/C++ Plugin

Slide 22

Slide 22 text

JNI method for calling a method in a Java class JNIEXPORT jobject JNICALL Java_com_example_NativeClass_nativeMethod(JNIEnv* env) { jclass cls_JavaClass = env->FindClass("com/your/java/Class"); // find class definition jmethodID mid_JavaClass = env->GetMethodID (cls_JavaClass, "", "()V"); // find constructor method jobject obj_JavaClass = env->NewObject(cls_JavaClass, mid_JavaClass); // create object instance return env->NewGlobalRef(obj_JavaClass); // return object with a global reference } Java and C/C++ Plugin

Slide 23

Slide 23 text

static void UNITY_INTERFACE_API OnRenderEvent(int eventID) { switch((GLEvent)eventID) { case Initialize : InitMediaSurface(); break; case Shutdown : mediaSurface->Shutdown(); mediaSurface = NULL; break; case Update: mediaSurface->Update(); break; case Resize: mediaSurface->ResizeTexture(); break; } } Java and C/C++ Plugin static void UNITY_INTERFACE_API OnRenderEvent(int eventID); enum GLEvent { Initialize = 0, Shutdown = 1, Update = 2, Resize = 3 }; Sending event to the native surface for low-level rendering

Slide 24

Slide 24 text

Set a native callback and dispatch an event to the native plugin from Unity // Set the native callback from Unity _inptr_native_callback_delegate = Marshal.GetFunctionPointerForDelegate(NativeCallback); // Native method from the plugin CINEVR_AndroidNative_SetNativeCallback(_inptr_native_callback_delegate); Java and C/C++ Plugin // Get native callback from the plugin to queue for Unity's renderer to invoke IntPtr _renderFunc = CINEVR_AndroidNative_GetRenderEventFunc(); // Dispatch the event to native plugin GL.IssuePluginEvent(_renderFunc, GLEventType.Initialize);

Slide 25

Slide 25 text

Your C++ method in your JNI plugin JNIEXPORT jstring JNICALL Java_js_NativePlugin_getHiString(JNIEnv * env, jobject obj) { return env->NewStringUTF("Bonjour DevFest Nantes ! Hope you have fun"); } C/C++ from Java or Kotlin

Slide 26

Slide 26 text

From Kotlin C/C++ from Java or Kotlin external fun getHiString(): String From Java public native String getHiString(); From Kotlin init { System.loadLibrary("sample-jni") } From Java static { System.loadLibrary("sample-jni"); }

Slide 27

Slide 27 text

× Support available from 2018.2 × Add your files in a dedicated folder in the Asset Folder × Build with Gradle only × Use the AndroidJavaObject class to call methods in your plugin Building Unity Plugins Using Java or Kotlin source files as plugins

Slide 28

Slide 28 text

× Enable Custom Gradle Templates property on the Player window × Override the generated mainTemplate.gradle file × You can also provide your own settings.gradle file Building Unity Plugins Add Gradle dependencies to your Unity project

Slide 29

Slide 29 text

3. Unity from Android Studio Your turn Android devs!

Slide 30

Slide 30 text

That’s what we want!

Slide 31

Slide 31 text

× Export your Unity project with Gradle or IL2CPP as Build System × Enable Symlink Sources if you have Java or Kotlin source files Unity from Android Studio Exporting a Unity Project

Slide 32

Slide 32 text

× Have a full control of the build workflow × Easily add Android code × Easy library management × Modify generated code Unity from Android Studio Exporting a Unity Project

Slide 33

Slide 33 text

UnityPlayerActivity overview public class UnityPlayerActivity extends Activity { protected UnityPlayer mUnityPlayer; // don't change it, referenced from native code @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); getWindow().setFormat(PixelFormat.RGBX_8888); // <-- This makes xperia play happy mUnityPlayer = new UnityPlayer(this); setContentView(mUnityPlayer); mUnityPlayer.requestFocus(); } }

Slide 34

Slide 34 text

Override the UnityPlayerActivity public class OverrideExample extends UnityPlayerActivity { @Override protected String updateUnityCommandLineArguments(String cmdLine) { if (Utils.preferVulkan()) return Utils.appendCommandLineArgument(cmdLine, "-force-vulkan"); else if (Utils.preferES2()) return Utils.appendCommandLineArgument(cmdLine, "-force-gles20"); else return cmdLine; // let Unity pick the Graphics API based on PlayerSettings } }

Slide 35

Slide 35 text

4. What’s Next? \ (•◡•) /

Slide 36

Slide 36 text

What’s next? Linux support is coming soon Better Gradle integration Better performances

Slide 37

Slide 37 text

And that’s it?

Slide 38

Slide 38 text

Of course not!

Slide 39

Slide 39 text

Unity as a LiBrary!

Slide 40

Slide 40 text

What’s next? Unity as a Library × Available with Unity 2019.3 × Add Unity content (AR/VR, 2D/3D games) directly to your Android apps × Proper library integration

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

public class MainUnityActivity extends OverrideUnityActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); goBackBtn.setOnClickListener(v -> { showMainActivity(); }); sendMsgBtn.setOnClickListener(v -> { UnitySendMessage("Cube", "ChangeColor", "yellow"); }); finishBtn.setOnClickListener(v -> { finish(); }); } } Override the OverrideUnityActivity

Slide 43

Slide 43 text

What’s next? Unity as a Library limitations × Fullscreen rendering only × Only one instance of the Unity runtime is supported × You may need to adapt your plugins to work properly

Slide 44

Slide 44 text

What’s next? Unity as a library 01 02 03 https://blogs.unity3d.com/2019/06/17/add-features-powered-by -unity-to-native-mobile-apps Unity as a Library https://forum.unity.com/threads/using-unity-as-a-library-in-nat ive-ios-android-apps.685195/ Using Unity as a library in native iOS/Android apps https://forum.unity.com/threads/integration-unity-as-a-library- in-native-android-app.685240/ Integration Unity as a library in native Android app

Slide 45

Slide 45 text

Eh! What about Android from a Unity Point of view? random person in the audience - right now

Slide 46

Slide 46 text

I was about to tell them!

Slide 47

Slide 47 text

What’s next? Android from a Unity Point of View × Easy to build on Android × Graphic libs support (OpenGL/Vulkan) Easy compat with ShaderLab × No need to dig into the Android documentation

Slide 48

Slide 48 text

What’s next? Android from a Unity Point of View × How it is implemented under the hood × Building UI components × Integrating Unity games into Android apps… but Unity as a library is coming

Slide 49

Slide 49 text

Want to know more? https://developer.android.com/game Android Game Development https://medium.com/@cinemur/73ec7e83dd5c ExoPlayer for building powerful VR players on Cardboard and GearVR https://docs.unity3d.com/Manual/PluginsForAndroid.html Building Unity Plugins for Android

Slide 50

Slide 50 text

THANKS! Have fun with Android and Unity! Any questions?