简体   繁体   中英

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?

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.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM