Pro Yearly is on sale from $80 to $50! »

Reach 4 million Unity developers with your Android library

Reach 4 million Unity developers with your Android library

Recording: http://www.butterflytv.net/player.html?videoId=from_pi21371431281378228

Reach 4 million Unity developers with your Android library With the 45% market share, Unity is the most popular game engine in the world. Featuring reach API exposed in C# and JavaScript, a tremendous amount of assets and libraries available and Asset Store - an open platform to sell or share them. While talk will discuss why it is time to make your Android library or SDK available to Unity developers. How to expose it to C# using Unity API, JNI and a bit of magic. What problems you'll face with while porting. And last but not least, how to publish your work to Unity Asset Store.

F456ed67b75e58e533d11b301f5f62b5?s=128

Vitaliy Zasadnyy

May 11, 2015
Tweet

Transcript

  1. Reach 4 million Unity developers with your Android library Vitaliy

    Zasadnyy Senior Unity Dev @ GetSocial Manager @ GDG Lviv Ankara Android Dev Days May 11-12 , 2015
  2. Why Unity?

  3. #AndroidDevDays

  4. #AndroidDevDays Daily Users 0 225 450 675 900 Source: Unreleased

    McKinsey report; Facebook http://newsroom.fb.com/company-info/ M M M M
  5. #AndroidDevDays 0 225 450 675 900 Source: Unreleased McKinsey report;

    Facebook http://newsroom.fb.com/company-info/ Daily Users M M M M
  6. #AndroidDevDays Unity Developers Unity 4M Source: Unity Technologies

  7. #AndroidDevDays Unity Developers Unity 4M 18M Worldwide Source: Global Developer

    Population and Demographic Study 2014
  8. #AndroidDevDays Unity Developers Source: Unity Technologies

  9. #AndroidDevDays Unity is Android in game engine world

  10. #AndroidDevDays

  11. #AndroidDevDays GetSocial Unity SDK Architecture Android SDK iOS SDK iOS

    Bridge Android Bridge C# API Unity SDK Java Objective C C#
  12. #AndroidDevDays During Android Build Android SDK iOS SDK iOS Bridge

    Android Bridge C# API Unity SDK
  13. #AndroidDevDays Unity Game Structure Android Mono VM Unity Core Unity

    C# Scripts OS Apk
  14. #AndroidDevDays Android Mono VM Unity Core Unity C# Scripts Java

    Code ? Unity Game Structure OS Apk
  15. #AndroidDevDays Android / Java Unity / C# primitive types primitive

    types java.lang.String string java.lang.Object AndroidJavaObject java.lang.Class AndroidJavaClass java.lang.Runnable AndroidJavaRunnable java.lang.Proxy AndroidJavaProxy Provided Wrappers
  16. #AndroidDevDays Android / Java Unity / C# primitive types primitive

    types java.lang.String string java.lang.Object AndroidJavaObject java.lang.Class AndroidJavaClass java.lang.Runnable AndroidJavaRunnable java.lang.Proxy AndroidJavaProxy Provided Wrappers
  17. Two-way communication C# ↔ Java

  18. #AndroidDevDays C# → Java • AndroidJavaObject • Pure JNI (Java

    Native Interface) Java → C# • Unity Messaging • AndroidJavaProxy Two Way Communication
  19. #AndroidDevDays Sample Java class public class Speaker { private String

    name; public Speaker(String name) { … } public static Speaker copy(Speaker speaker) { … } public String askQuestion(String question) { … } } Java
  20. #AndroidDevDays C# var jSpeaker = new AndroidJavaObject( “com.zasadnyy.Speaker”, “Vitaliy”); var

    jClone = jSpeaker.CallStatic<AndroidJavaObject>( “copy”, jSpeaker); var answer = jSpeaker.Call<string>(“askQuestion”, “HDYD?”); AndroidJavaObject • Wrapper around java.lang.Object
  21. #AndroidDevDays C# // var jSpeaker = new AndroidJavaObject( // “com.zasadnyy.Speaker”,

    “Vitaliy”); IntPtr classID = jni.FindClass("com.zasadnyy.Speaker"); IntPtr constructorID = jni.GetMethodID(classID, "<init>", "(Ljava/lang/String;)V"); IntPtr javaString = jni.NewStringUTF("Vitaliy"); IntPtr objectID = jni.NewObject(classID, constructorID, javaString); "(Ljava/lang/String;)V" Pure JNI
  22. #AndroidDevDays B byte S short C char V void F

    float Z boolean I int Lclass; class J long [type> array of type JNI Cheatsheet
  23. #AndroidDevDays B byte S short C char V void F

    float Z boolean I int Lclass; class J long [type> array of type long myMethod(int n, String s, int[] arr) (ILjava/lang/String;[I>)J JNI Cheatsheet / Example
  24. #AndroidDevDays JNI is slooooow! Respect 16ms rule

  25. #AndroidDevDays Conclusion Use AndroidJavaObject: • Less boilerplate • Less error

    prone • Faster (caching) But: • JNI is more flexible • AndroidJavaObject has bugs
  26. #AndroidDevDays AndroidJavaObject Bugs * var result = javaObject.Call<TResult>(TParam param); C#

    • we can’t pass string[] as TParam (NoSuchMethodException) • we can’t return null objects from Java (Exception: JNI: Init'd AndroidJavaObject with null ptr!) * Tested on Unity 5.0.1f1
  27. #AndroidDevDays C# • AndroidJavaObject • Pure JNI (Java Native Interface)

    Java → C# • Unity Messaging • AndroidJavaProxy Two Way Communication
  28. #AndroidDevDays Unity Messaging

  29. #AndroidDevDays UnityPlayer.UnitySendMessage( "GameController", "OnPushMessage", "New gift"); Java Unity Messaging

  30. #AndroidDevDays Pros: • Quick to implement Cons: • Always 1

    frame late • Only string parameters • Requires GameObject • Requires unity-classes.jar in classpath Unity Messaging
  31. #AndroidDevDays AndroidJavaProxy Java public interface IPushMessageListener { void onPushMessage(String message);

    } • Wrapper around java.lang.Proxy
  32. Java public interface IPushMessageListener { void onPushMessage(String message); } C#

    public class PushMessageListener : AndroidJavaProxy { public PushMessageListener() : base("com.zasadnyy.IPushMessageListener") { … } void onPushMessage(string message) { … } } AndroidJavaProxy
  33. #AndroidDevDays Pros: • Immediate result • No type restriction on

    parameters • No extra dependencies on Java side Cons: • Bug with null string parameter • Local reference table is limited to 512 entries AndroidJavaProxy
  34. Devil is in the details

  35. #AndroidDevDays Details • Unity is single threaded • Unity main

    thread != Android main thread • Implement equals() in all AndroidJavaProxy’ies • Differences between GameObject and Activity lifecycle • Keep calm and signal 11
  36. #AndroidDevDays Thank you! Presentation will be available at: v.zasadnyy.com/slides/ Vitaliy

    Zasadnyy @zasadnyy