reach 4 million unity developers
play

Reach 4 million Unity developers with your Android library Vitaliy - PowerPoint PPT Presentation

Reach 4 million Unity developers with your Android library Vitaliy Zasadnyy Ankara Android Dev Days Senior Unity Dev @ GetSocial May 11-12 , 2015 Manager @ GDG Lviv Why Unity? #AndroidDevDays Daily Users 0 225 450 675 900 M M M M


  1. Reach 4 million Unity developers with your Android library Vitaliy Zasadnyy Ankara Android Dev Days Senior Unity Dev @ GetSocial May 11-12 , 2015 Manager @ GDG Lviv

  2. Why Unity?

  3. #AndroidDevDays

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

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

  6. Unity Developers 4M Unity Source: Unity Technologies #AndroidDevDays

  7. Unity Developers 18M Worldwide 4M Unity Source: Global Developer Population and Demographic Study 2014 #AndroidDevDays

  8. Unity Developers Source: Unity Technologies #AndroidDevDays

  9. Unity is Android in game engine world #AndroidDevDays

  10. #AndroidDevDays

  11. GetSocial Unity SDK Architecture Unity SDK C# API iOS Bridge Android Bridge iOS Android SDK SDK Java Objective C C# #AndroidDevDays

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

  13. Unity Game Structure C# Scripts Unity Mono VM Unity Core Apk OS Android #AndroidDevDays

  14. Unity Game Structure ? C# Scripts Unity Java Code Mono VM Unity Core Apk OS Android #AndroidDevDays

  15. Provided Wrappers 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 #AndroidDevDays

  16. Provided Wrappers 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 #AndroidDevDays

  17. Two-way communication C# ↔ Java

  18. Two Way Communication C# → Java • AndroidJavaObject • Pure JNI (Java Native Interface) Java → C# • Unity Messaging • AndroidJavaProxy #AndroidDevDays

  19. Sample Java class Java public class Speaker { private String name; public Speaker(String name) { … } public static Speaker copy(Speaker speaker) { … } public String askQuestion(String question) { … } } #AndroidDevDays

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

  21. Pure JNI 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"); "(Ljava/lang/String;)V" IntPtr javaString = jni.NewStringUTF("Vitaliy"); IntPtr objectID = jni.NewObject(classID, constructorID, javaString); #AndroidDevDays

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

  23. JNI Cheatsheet / Example long myMethod(int n, String s, int[] arr) (ILjava/lang/String;[I>)J B S byte short C V char void F Z float boolean I Lclass; class int J [type> long array of type #AndroidDevDays

  24. JNI is slooooow! Respect 16ms rule #AndroidDevDays

  25. Conclusion Use AndroidJavaObject : • Less boilerplate • Less error prone • Faster (caching) But: • JNI is more flexible • AndroidJavaObject has bugs #AndroidDevDays

  26. AndroidJavaObject Bugs * C# var result = javaObject.Call<TResult>(TParam param); • 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 #AndroidDevDays

  27. Two Way Communication C# • AndroidJavaObject • Pure JNI (Java Native Interface) Java → C# • Unity Messaging • AndroidJavaProxy #AndroidDevDays

  28. Unity Messaging #AndroidDevDays

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

  30. Unity Messaging Pros: • Quick to implement Cons: • Always 1 frame late • Only string parameters • Requires GameObject • Requires unity-classes.jar in classpath #AndroidDevDays

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

  32. AndroidJavaProxy Java public interface IPushMessageListener { void onPushMessage(String message); } C# public class PushMessageListener : AndroidJavaProxy { public PushMessageListener() : base("com.zasadnyy.IPushMessageListener") { … } void onPushMessage(string message) { … } }

  33. AndroidJavaProxy 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 #AndroidDevDays

  34. Devil is in the details

  35. 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 #AndroidDevDays

  36. Thank you! Vitaliy Zasadnyy Presentation will be available at: @zasadnyy v.zasadnyy.com/slides/ #AndroidDevDays

Recommend


More recommend