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. 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. 4.

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

    McKinsey report; Facebook http://newsroom.fb.com/company-info/ M M M M
  3. 5.

    #AndroidDevDays 0 225 450 675 900 Source: Unreleased McKinsey report;

    Facebook http://newsroom.fb.com/company-info/ Daily Users M M M M
  4. 11.

    #AndroidDevDays GetSocial Unity SDK Architecture Android SDK iOS SDK iOS

    Bridge Android Bridge C# API Unity SDK Java Objective C C#
  5. 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
  6. 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
  7. 18.

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

    Native Interface) Java → C# • Unity Messaging • AndroidJavaProxy Two Way Communication
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 25.

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

    prone • Faster (caching) But: • JNI is more flexible • AndroidJavaObject has bugs
  14. 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
  15. 27.

    #AndroidDevDays C# • AndroidJavaObject • Pure JNI (Java Native Interface)

    Java → C# • Unity Messaging • AndroidJavaProxy Two Way Communication
  16. 30.

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

    frame late • Only string parameters • Requires GameObject • Requires unity-classes.jar in classpath Unity Messaging
  17. 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
  18. 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
  19. 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