c# - Unity receive event from object c -


i want create ios plugin unity. function send textmessage using sdk. well, here object c:

-(textmessage*) csendtext:(nsstring *)number cmsgcontent:(nsstring *)msg{     return [messagingapi sendtext:(nsstring*) number msgcontent:(nsstring *) msg] } 

and here 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 return textmessage, event not char,, how can pass event unity,

my c# code 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);         }      }        

that's not way working. cannot return in c , expect appear in unity / c#. achieve "two way communication" you'll need following.


unity => ios

c# code

[dllimport ("__internal")] private static extern void _somecmethod(string parameter); 

(objective-) c code

void _somecmethod(const char *parameter)  { } 

when invoke _somemethod in c#, _somemethod in (objective-) c code gets called.

ios => unity

objective-c code

- (void)callsomecsharpmethod     unitysendmessage("go", "somecsharpmethod", "parameter"); } 

c# code (monobehaviour script attached go)

void somecsharpmethod(string parameter) { } 

when invoke callsomecsharpmethod in objective-c, somecsharpmethod in c# code gets called.


converting strings

you'll have convert nsstrings c strings (const char *) , vice versa.

const char * => nsstring

[nsstring stringwithcstring:string encoding:nsutf8stringencoding]; 

nsstring => const char *

[string cstringusingencoding:nsutf8stringencoding]; 

note: simple example on how achieve 2 way communication channel between c# , native ios code. of course don't want hardcode e.g. gameobject's name in code, can pass name (for callbacks objective-c) native ios code @ beginning.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -