This article belongs to "unity and iOS, Android platform integration" series of articles , reproduced please indicate the source .
This article focuses on the theoretical basis for unity's interaction with iOS and Android platforms.
0. Preface
C # is used on the unity side
Object-c is used on the iOS side
Java is used on the Android side
You're going to ask me why?
Because I'm happy.
First, the interaction needs
The interaction requirements between code and code are abstracted from the two most basic requirements:
1. Calling functions
2. Passing Data
So we just need to implement these two basic requirements between unity and iOS and Android.
Second, the principle of interaction
First, can we achieve the cornerstone of interaction-call each other?
Fortunately, Unity's active call to iOS and Android is doable, and iOS and Android actively invoke unity as well.
1.Unity active call to iOS, Android
IOS with Unity
Implementation Solutions Unity's official documentation has been given
I made some summaries myself:
First, Unity supports C # by invoking a DLL to invoke C + + 's
Second, iOS is supported by C + + and object-c, so C + + can naturally call Object-c
So, our implementation scenario is C #-C + +-object-c
Android, Unity
Similarly, the official documentation of Unity for the implementation of the solution has been given
Two methods are given in the documentation:
Plan A: How to use the Android NDK
Plan B: How to use Java Native Interface (JNI)
Then I'll explain the JNI approach in Plan B, and why not talk about plan A's way ... Because I'm not happy ...
On the way to the JNI official gave a detailed example, I have also done some summary:
First, Unity provides Unityengine.androidjni, which enables you to create Java objects in C # and Invoke functions
Second, do native Java calls on Android, Java implementation can not be self-hanging southeast branch
So, our implementation scenario is C #-Java
2.iOS, Android active call Unity
Unity, Android, IOS
Unity has only provided us with an official approach.
Unitysendmessage ("GameObjectName1", "MethodName1", "Message to send");
Explain this function:
return value: None
function type: static function, direct call (included in Com.unity3d.player.UnityPlayer in Java)
Parameter 1: Name of a Gameobject object in the scene
Parameter 2: The function name that is mounted on the component on the Gameobject object
Parameter 3: Parameters passed in when the function is called
So, our implementation scenario is java/object-c C #
Second, can you implement parameter passing?
Don't worry, the methods mentioned above can be implemented.
1.Unity pass parameters to iOS and Android
A. Number of parameters
About the number, I have passed the maximum of 9, the upper limit has not been tried, but I also do not recommend too many parameters, simple point good.
B. Types that can be transmitted directly
I did not try all the types, only summed up a few commonly used, if you are interested can try it yourself.
√:bool, int, float, string
x:class, struct
C. Return values after a call
Again, I've only tried some of the usual types.
√:bool, int, float, string
x:class, struct
2.iOS, Android Pass parameters to unity
A. Number of parameters
Only 1
B. Types that can be transmitted directly
can only pass string
C. Return values after a call
No return value
Q: Horizontal slot, how to do this, IOS, Android to Unity can only pass a string Ah!!!!
A: Panic What, Sao years, know what is called JSON?
Yes, all data structures, we can use the JSON format to encapsulate a string into C # and then parse it out.
Q: What if there is no return value, I want to return the value Ah!!!
A: callback within the called C # function to pass a parameter is not good ~
Yes, the return value can be given in the form of an immediate callback, as long as it is possible to implement mutual invocation of the arguments, although it is a bit troublesome.
Iii. Summary
As you can see from the above theory, there is a need for interaction between unity and iOS and Android platforms:
1. Calling functions
2. Passing Data
So, the interaction between Unity and iOS and Android is something you can do.
At the same time, the theory of the implementation of the program in the official documents have been given, I hope you have time to try to achieve their own.
The entire interactive implementation process and the source code, I will explain in the next article and provide download, please look forward to ~
"unity" integration with iOS and Android: 4. Interactive theory with iOS and Android