[Unity]unity与调用objective-c
2017-03-02 本文已影响36人
阿飞咯
- 定义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
}
完成!