In Unity, Wen can call objective-C code from C# by import the function of objective-C as extern function. But how call C# script code from objective-C ?
4 Answers
You can use
UnitySendMessage("GameObjectName1", "MethodName1", "Message to send");
in which you fill your gameobject name which will receive this message and the message name.
You can implement your own delegate/event once you receive this message from native code. The limit of UnitySendMessage is when it arrives to Unity code, it is always 1 frame after you call this in native code. And it can only take string as parameter. But most of time it is not big problem.
Comments
The method to be used for this is UnitySendMessage. Have a look at Building Plugins for iOS
Comments
Have a look at: http://www.tinytimgames.com/2010/01/10/the-unityobjective-c-divide/ The bolg post shows a technique that uses KVO (Key Value Observation) and the PlayerPrefs to pass commands from unity to objective-c.
PlayerPrefs.SetString("Commands",
String.Format("AwesomeCommand|{0}|{1}", awesome1, awesome2));
Comments
Universal plugin for unity for iOS that allow mixing C# with objective-C https://www.assetstore.unity3d.com/#/content/10310
Unity C#
var callbackClass = AKiOSMagic.CreateClass("MyCallbackClass", "NSObject");
callbackClass.AddMethod("methodWithArg:anotherArg:", (args)
{
// do something...
// args.GetObject(0);
// args.GetObject(1);
});
callbackClass.RegisterClass();
Objective-C
[[NSClassFromString(@"MyCallbackClass") new] methodWithArg:@"arg1" anotherArg:@"arg2"]