Unity accepts event from object c

I want to create an ios plugin for unity. The function sends a text message using some sdk. Well here is my c object:

-(TextMessage*) CsendText:(NSString *)number CmsgContent:(NSString *)msg{ return [MessagingApi sendText:(NSString*) number msgContent:(NSString *) msg] } 

and here is my c wrap code:

 TextMessage* SendMessage(const char* contactNumber,const char* content){ Messaging* msg = [[Messaging alloc] init]; NSString* nr = [NSString stringWithUTF8String:contactNumber]; NSString* contentText = [NSString stringWithUTF8String:content]; TextMessage* newText = [msg CsendText:nr CmsgContent:contentText]; return newText; } 

you can see that I am returning a text message, this event is not a char, how can I pass the event to unity,

my C # code is here:

 #if UNITY_IPHONE [DllImport("__Internal")] private static extern string SendMessage (string contactNumber,string content); #endif string phoneNumber=""; string content=""; void OnGUI () { phoneNumber = GUI.TextField ( new Rect (250, 125, 250, 25), phoneNumber, 40); content = GUI.TextField (new Rect (250, 157, 250, 25), content, 40); if (GUI.Button(new Rect (250, 250, 100, 30),"click me")) { SendMessage(phoneNumber,content); } } 
+5
source share
1 answer

This is not how it works. You cannot just return something to C and expect it to appear in Unity / C #. To achieve two-way communication, you will need to do the following.


Unity => iOS

C # code

 [DllImport ("__Internal")] private static extern void _SomeCMethod(string parameter); 

(Objective-) C code

 void _SomeCMethod(const char *parameter) { } 

When you call _SomeMethod in C #, _SomeMethod in your (Objective-) C code is called.

iOS => Unity

Objective-C code

 - (void)callSomeCSharpMethod UnitySendMessage("GO", "SomeCSharpMethod", "parameter"); } 

C # code ( MonoBehaviour script attached to GO )

 void SomeCSharpMethod(string parameter) { } 

When you call callSomeCSharpMethod in Objective-C, SomeCSharpMethod is SomeCSharpMethod in C # code.


String conversion

You will need to convert the NSString strings to c ( const char * ) and vice versa.

const char * => NSString

 [NSString stringWithCString:string encoding:NSUTF8StringEncoding]; 

NSString => const char *

 [string cStringUsingEncoding:NSUTF8StringEncoding]; 

Note: This is a simple example of how to achieve a two-way communication channel between C # and native iOS code. Of course, you do not want to hard code, for example. The name is GameObject in your code, so you can just pass that name (for callbacks from Objective-C) to your native iOS code at the very beginning.

+6
source

All Articles