標籤:style blog color 使用 strong 檔案
(1)unity3d 中如何調用IOS的Api
1、在C# 指令碼中使用
(1) 需要匯入檔案 using System.Runtime.InteropServices;
(2) [System.Runtime.InteropServices.DllImport("__Internal")]
extern static public int AwesomeFunction(int awesomeParameter);
2、在unity匯出的xcode工程中的C/C++/objective-C 檔案中的任意位置, 建立一個類 unityplugin.h, unityplugin.mm
標頭檔中:
extern "C" void AwesomeFunction();
類體中:
int AwesomeFunction(int awesomeParameter)
{
return somethingAwesome;
}
具體的:
Unity C# 代碼
using UnityEngine;using System.Collections;using System.Runtime.InteropServices;public class testFeil : MonoBehaviour { [DllImport("__Internal")] private static extern void c_ctest(); void Start () { } void Update () { } void OnGUI() { //開始按鈕 建立了一個按鈕。 if(GUI.Button(new Rect(20,100,200,50),"qingyun ")) { c_ctest(); } }
// 這個是C#裡的一個回調。OC處理完成後 調用Unity方法 void testFaildResult (string msg) { Debug.Log("testResult:" +msg); } //這個是C#裡的一個回調。這裡的作用就是OC裡的回呼函數。 void testSuccessResult (string msg) { Debug.Log ("btnPressSuccess:"+msg); }}
Object-C .mm 檔案裡的OC 與 C 介面
MeiyuPushSdk.h
#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@protocol MeiyuPushSdkDelegate;
@interface MeiyuPushSdk : NSObject@property (nonatomic, assign)id<MeiyuPushSdkDelegate> delegate;//初始化+(MeiyuPushSdk*)my_initPush;+ (void)my_setupWithOption:(NSDictionary *)launchingOption registerForRemoteNotificationTypes:(int)types ; // 註冊APNS類型+ (void)my_registerDeviceToken:(NSData *)deviceToken; // 向伺服器上傳Device Token+ (void)my_receiveRemoteNotification:(NSDictionary *)remoteInfo; // 處理收到的APNS訊息,向伺服器上傳收到APNS訊息-(void)cTest;@end@protocol MeiyuPushSdkDelegate <NSObject>@optional-(void)cTestResult;@end
MeiyuPushSdkC.h
#import <Foundation/Foundation.h>#ifdef __cplusplusextern "C"{#endif void c_ctest(); #ifdef __cplusplus}#endif
MeiyuPushSdkC.mm
#import "MeiyuPushSdkC.h"#import "MeiyuPushSdk.h"@interface MeiyuPushSdkC : NSObject<MeiyuPushSdkDelegate>@end@implementation MeiyuPushSdkC- (id)init{ id object = [super init]; [[MeiyuPushSdk my_initPush]setDelegate:self]; NSLog(@"initMeiyuPushSdkC"); return object;}-(void)cTestResult{ UnitySendMessage("Main Camera", "testSuceessResult", "成功");}@endMeiyuPushSdkC *m_pushSdk = NULL;#if defined (__cplusplus)extern "C"{#endif void c_ctest() { if(m_pushSdk == NULL) { m_pushSdk = [[MeiyuPushSdkC alloc]init]; } MeiyuPushSdk *m = [MeiyuPushSdk my_initPush]; [m cTest];
UnitySendMessage("Main Camera", "testFaildResult", "介面調用失敗"); } #if defined (__cplusplus)}#endif
這樣就把Unity3d 與OC 連到了一起。