Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Reach 4 million Unity developers with your Andr...

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.

Vitaliy Zasadnyy

May 11, 2015
Tweet

More Decks by Vitaliy Zasadnyy

Other Decks in Programming

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. #AndroidDevDays Daily Users 0 225 450 675 900 Source: Unreleased

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

    Facebook http://newsroom.fb.com/company-info/ Daily Users M M M M
  4. #AndroidDevDays GetSocial Unity SDK Architecture Android SDK iOS SDK iOS

    Bridge Android Bridge C# API Unity SDK Java Objective C C#
  5. #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. #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. #AndroidDevDays C# → Java • AndroidJavaObject • Pure JNI (Java

    Native Interface) Java → C# • Unity Messaging • AndroidJavaProxy Two Way Communication
  8. #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. #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. #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. #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. #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. #AndroidDevDays Conclusion Use AndroidJavaObject: • Less boilerplate • Less error

    prone • Faster (caching) But: • JNI is more flexible • AndroidJavaObject has bugs
  14. #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. #AndroidDevDays C# • AndroidJavaObject • Pure JNI (Java Native Interface)

    Java → C# • Unity Messaging • AndroidJavaProxy Two Way Communication
  16. #AndroidDevDays Pros: • Quick to implement Cons: • Always 1

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