Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UnitySendMessage call parameter

I am writing the Objective-C part of a Unity project. In AppController.mm, I declared

extern void UnitySendMessage(const char *, const char *, const char *);

And I am calling this like,

- (void)callUnityObject:(const char*)object Method:(const char*)method Parameter:(const char*)parameter
{
    UnitySendMessage(object, method, parameter);
}

But I have a Unity function that has to receive an int parameter. So, if I call like this:

[self callUnityObject:"_iosManager" Method:"GiveDynamite" Parameter:"50"];

The app doesn't crash, but the call doesn't work and I am getting an output like this:

The best match for method GiveDynamite has some invalid parameter.

If I call like this:

[self callUnityObject:"_iosManager" Method:"GiveDynamite" Parameter:50];

The App is crashing.

How can I send this message from Objective-c to Unity?

I tried declaring a new method like this:

extern void UnitySendMessage(const char *, const char *, int);

But the app crashed and said that unity doesn't have a function declaration like that.

Thanks in advance.

like image 404
alper_k Avatar asked Apr 17 '26 14:04

alper_k


1 Answers

According to:

void UnitySendMessage( const char * className, const char * methodName, const char * param )

You should pass char*:

[self callUnityObject:"_iosManager" Method:"GiveDynamite" Parameter:"50"];

On Unity class you should receive "string" param

void GiveDynamite(string dinamite) {
...
}

and then parse it to integer value, f.e:

dinamiteAmount = int.Parse(dinamite);
like image 177
Injectios Avatar answered Apr 20 '26 03:04

Injectios