unity3d與Object-C互動

來源:互聯網
上載者:User

標籤: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 連到了一起。

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.