Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call UnitySendMessage method in swift?

I tried calling UnitySendMessage method in my iOS Framework project using swift language but it is showing this error-

Use of unresolved identifier 'UnitySendMessage'

Here is the code snippet for my swift file-

import Foundation

@objc public class Example : NSObject{

    @objc open static let shared = Example()

    @objc open func printMsg(){
        print("\(#function) is called with message:");
        UnitySendMessage("CallbackTarget", "OnCallFromSwift", "Hello, Unity!");
    }
}

Getting stuck here, can you please tell me what am I missing?

like image 208
Lakshya Avatar asked Oct 20 '25 04:10

Lakshya


2 Answers

check Answer here.

UnitySendMessage is in libiPhone-lib.a, and as far as I can tell, the Unity folks don't include any header that declares it.

I just added the following to my code

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

If you look at the generated AppController.mm, they basically do the same thing for the various UnitySendXXX functions, rather than including a header.

Having said that, while you will be able to build a library that calls UnitySendMessage, that library probably won't work unless it's linked into a Unity project, since UnitySendMessage is going to require more than just libiPhone-lib.a to actually work.

like image 107
Ladumor Dineshkumar Avatar answered Oct 22 '25 18:10

Ladumor Dineshkumar


Just add this code to your Bridging-Header.h file:

#import "UnityInterface.h"

Make sure that your files are in the XCode project that was build from Unity, so the actual UnityInterface file that is created by unity is present.

Worked for me :)

like image 33
NickBullseye Avatar answered Oct 22 '25 17:10

NickBullseye



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!