- Home /
Alternate to UnitySendMessage in iOS native
Hey Guys,
Currently I am working on creating a Plugin for Unity3D-iOS. I want to send data from iOS native to Unity3D. For function called from iOS native to Unity3D I need to use UnitySendMessage function. But the limitation of UnitySendMessage is that there should be some speciface GameObject there in Unity3D and it's name should be fixed and that name i have to pass in UnitySendMessage as a parameter. While in my case this solution is not suitable for me.
For example :
code in iOS (xCode) :
[self callThisInUnity];
code in Unity3D c# :
void callThisInUnity(){
Debug.Log("Call from iOS");
}
So if I call "callThisInUnity" function from iOS then i should be called in Unity3d.
So is there any alternate way of UnitySendMessage for sending iOS data to Unity3D ?
Thanks in advance.
What do you mean by sending data from iOS to Unity3D?
Answer by Tommynator · Nov 27, 2014 at 06:47 AM
The only alternative way I know about is to pass integers as function pointers from Unity to the native layer, recast them and store them as handles. It's pretty rough tbh, but if you're feeling so strong about not using UnitySendMessage & GameObjects - that's the way to go :)
unity code:
class MyUnityClass
{
[DllImport ("iosplugin")]
private static extern int DefinePointer(int pointer);
void TestPointer()
{
Debug.Log("hello world");
}
void InitializePointers()
{
IntPtr p = Marshal.GetFunctionPointerForDelegate(TestPointer as System.Action);
// pass the function pointer to the native layer
DefinePointer(p.ToInt32());
}
}
iosplugin code:
extern "C"
{
void DefinePointer(int pointer)
{
void (*unityfunc)() = reinterpret_cast<void(*)()>(pointer);
unityfunc();
}
}
...and the unity console prints out "hello world".
In the example above I cast the pointer right away and call the function, but you can just store them in a global member and call them whenever you like.
Hope that helps ;) Cheers
Uhm as you said that's all C#, but where is the link to iOS native code (which is probably written in objective C or C++)? AFAI$$anonymous$$ the question was about how to send text data from native code to Unity. See the plugin page.
I'm not an iOS developer so i can't say for sure, but as far as i know there's no way around using the "UnitySend$$anonymous$$essage" function. At least when it comes to "native -> Unity" communication. You might be able to call a plugin method to poll the data (so the method called simply returns the data), but as i said i don't develop for iOS.
I want to call Unity3D function call from iOS native. Please see my updated question.
@yashesh: thanks for clarifying your question. :) First of all you need to write your own native library in c/c++ or objective c and define all functions that need to interoperate with Unity inside the extern "C" namespace. A good place to start is to read the unity manual section covering native plugins for iOS: http://docs.unity3d.com/$$anonymous$$anual/PluginsForIOS.html Note: It's a Pro feature and is not available in the indie version of Unity. Cheers
@$$anonymous$$mynator I have already created iOS native plugin. I want to call $$anonymous$$y Unity Function from my iOS native plugin file.
Your answer
Follow this Question
Related Questions
Admob Plugin Unity IOS 4 Answers
Saving Unity Texture2D updated with OpenGL FBO via custom plugin 2 Answers
Plugin iOS to send Mail with MFMailComposeViewController 0 Answers
A node in a childnode? 1 Answer