[Unity]unity与调用objective-c

  • 定义oc头文件
#ifndef XXSdk_h//头文件名
#define XXSdk_h
#import <Foundation/Foundation.h>
    @interface XXSdk :NSObject
    +(void)initSdk:(NSString*)string;//定义方法
    @end
#endif /* XXSdk_h */
  • 编写oc .mm文件
//
//  XXSdk.mm
//  XXSdkDemo
//  Copyright © 2017年 Tencent. All rights reserved.
//
#import "XXSdk.h"
@implementation XXSdk
+(void)initSdk:(NSString*)string{
//TODO   
}
@end
extern "C"{
    //字符串转化的工具函数
    NSString* _CreateNSString (const char* string)
    {
        if (string)
            return [NSString stringWithUTF8String: string];
        else
            return [NSString stringWithUTF8String: ""];
    }
    void _InitSdk(const char* string){
        NSString* appId = _CreateNSString(string);
        [BuglyIosSdk iniTSdk:appId];
    }
}

将编写好的代码导入到Plugins/ios目录下(自动导出到Xcode)

  • 编写C#代码
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class XXSdk{
    public static void InitSdk(string appId){
        #if UNITY_IOS && !UNITY_EDITOR
        _InitSdk(appId);
        #endif
    }

    #if UNITY_IOS && !UNITY_EDITOR
    [DllImport("__Internal")]
    private  static extern void _InitSdk(string appId);
    #endif
}

完成!

    原文作者:阿飞咯
    原文地址: https://www.jianshu.com/p/e6fa51b5692d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞