3

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 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

The method to be used for this is UnitySendMessage. Have a look at Building Plugins for iOS

Comments

1

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

0

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.