Unity IOS SDK 集成 常用功能

xzhuan原创稿件,转载请注明出处!

1.复制到粘贴板

C#部分略
IOS部分
extern "C" {
   void Past(char* str);
}
-(void) Past:(char*) str
{
   NSString* temp = [NSString stringWithUTF8String:str];
   UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
   pasteboard.string = temp ;
}

2.监听网络状态(这里采用官方案例 Reachability)
(ios官方)网络状态监听 Reachability
密码 fh34

unity实现如下
2.1 拷贝下载的xcode工程内 Reachability.h 和 Reachability.m 到 Plugins/ios目录下
2.2 unity正常打包xcode工程, mac - xcode中打开
2.3 找到 UnityAppController.mm文件 打开, 添加如下代码
//导入头文件 
#import "Libraries/Plugins/iOS/Reachability.h"

//网络状态 监听模块
@interface UnityAppController ()
  @property (nonatomic, strong) Reachability *hostReachability;
  @property (nonatomic, strong) Reachability *interNetReachability;
@end

//初始化 网络监听状态
- (void) ListenerNetworkStatusInit
{
   // KVO监听,监听kReachabilityChangedNotification的变化
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
   
   // 初始化 Reachability 当前网络环境
   self.interNetReachability = [Reachability reachabilityForInternetConnection];
   // 开始监听
   [self.interNetReachability startNotifier];
   
   
   //    /** 或者你也可以监听某一个站点的网络连接情况 */
   //    NSString *remoteHostName = @"www.apple.com";
   //    self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
   //    [self.hostReachability startNotifier];
}

/** 网络环境改变时实现的方法 */
- (void) reachabilityChanged:(NSNotification *)note {
   
   // 当前发送通知的 reachability
   Reachability *reachability = [note object];
   
   // 当前网络环境(在其它需要获取网络连接状态的地方调用 currentReachabilityStatus 方法)
   NetworkStatus netStatus = [reachability currentReachabilityStatus];
   
   // 断言 如果出错则发送错误信息
   NSParameterAssert([reachability isKindOfClass:[Reachability class]]);
   
   // 不同网络的处理方法
   switch (netStatus) {
       case NotReachable:
           NSLog(@"没有网络连接");
//注意: ObjName是unity场景下GameObject对象名, FunName是挂载于该对象上代码中的公开函数
           UnitySendMessage("ObjName", "FunName", "0");
           break;
       case ReachableViaWiFi:
           NSLog(@"已连接Wi-Fi");
           UnitySendMessage("ObjName", "FunName", "1");
           break;
       case ReachableViaWWAN:
           NSLog(@"已连接蜂窝网络");
           UnitySendMessage("ObjName", "FunName", "2");
           break;
       default:
           break;
   }
}

/**  移除监听,防止内存泄露 */
- (void)dealloc {
   
   // Reachability停止监听网络, 苹果官方文档上没有实现,所以不一定要实现该方法
   [self.hostReachability stopNotifier];
   
   // 移除Reachability的NSNotificationCenter监听
   [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}

//注意: 在初始化的地方调用 如下
- (void)startUnity:(UIApplication*)application
{
   NSAssert(_unityAppReady == NO, @"[UnityAppController startUnity:] called after Unity has been initialized");
   
   UnityInitApplicationGraphics();
   
   // we make sure that first level gets correct display list and orientation
   [[DisplayManager Instance] updateDisplayListCacheInUnity];
   
   UnityLoadApplication();
   Profiler_InitProfiler();
   
   [self showGameUI];
   [self createDisplayLink];
   
   UnitySetPlayerFocus(1);
   
   //初始化 网络监听状态
   [self ListenerNetworkStatusInit];
}

2.4 自行实现C# FunName回调函数即可

你可能感兴趣的:(Unity IOS SDK 集成 常用功能)