unity与iOS之间的简单交互

来源:互联网 发布:ar技术应用于淘宝 编辑:程序博客网 时间:2024/06/06 07:20

unity与iOS的交互要比Android之间的简单,

unity会编译成xcode的项目以供我们进行二次的开发。

目前我所知道的交互有,unity调用iOS的方法,iOS传递字符串给unity两种

1

随意创建xcode的项目,

创建一个NSObject类文件,在.h文件

#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface AlertView : NSObject#if defined(__cplusplus)extern"C"{#endif    extern void UnitySendMessage(const char *,const char *, const char *);#if defined(__cplusplus)}#endifvoid _showTestView(char *msg);void _showCompressView (void);@end

以上的_showTestView(含参数),showCompressView(无参数)是unity调用的方法

在.m文件中,要先实例

static AlertView* alert=nil;+ (AlertView *) instance{        if (alert==nil)    {        alert=[[AlertView alloc]init];            }        return alert;}

然后通过alert调用具体的方法

#if defined(__cplusplus)extern"C"{#endif       void _showTestView(char *msg){                    [AlertView instance];                NSString * stringMsg = [NSString stringWithUTF8String:msg];                [alert showTestView:stringMsg];    }        void _showCompressView (void){            [AlertView instance];                [alert showCompress];            }    #if defined(__cplusplus)}#endif

在具体实现_showTestView(含参数),showCompressView(无参数)的方法

- (void)showCompress{    UIWindow *window = [UIApplication sharedApplication].keyWindow;        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, window.frame.size.width - 150, 50)];    lab.text = @"实现showCompress方法";    lab.textAlignment = 1;    lab.center = CGPointMake(window.frame.size.width / 2 , window.frame.size.height - 40);    [lab setFont:[UIFont systemFontOfSize:20]];    lab.textColor = [UIColor whiteColor];    lab.numberOfLines = 0;    lab.backgroundColor = [UIColor lightGrayColor];    lab.alpha = 0.7;    lab.layer.masksToBounds = YES;    lab.layer.cornerRadius = 10;        [window addSubview:lab];}

- (void)showTestView:(NSString *)msg{    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil];    [alert show];    //这是将iOS的字符串传递到unity中的方法    /*     参数一 unity中的代码所挂载的物体     参数二 unity中代码里面的方法     参数三 所需传递的字符串     */    UnitySendMessage([@"Main Camera" UTF8String],[@"iOS" UTF8String],[@"hello world" UTF8String]); }

这样,在iOS中需要做的工作就完成了


将这个类文件复制到unity的文件夹中,一般路径是Plugins/iOS文件夹中

然后在该文件夹中创建C#代码,

using UnityEngine;  using System.Collections;  using System.Runtime.InteropServices;  public static class IOS {  [DllImport ("__Internal")]  private static extern void _showTestView(string msg);        [DllImport ("__Internal")]  private static extern void _showCompressView();public static void showTestView (string msg){_showTestView(msg);}public static void showCompressView (){_showCompressView();}} 

创建一个C#脚本,挂载到物体上,例如Main Camera相机上,

这样当我们在脚本中调用IOS.showTestView方法,和IOS.showCompressView方法时,就会自动调用到NSObject类中写好的方法,

同时,在unitySendMessage方法会返回一个“hello world”字符串到Main Camera物体上的iOS方法,所以我们要在脚本中创建一个方法

void iOS (string msg){Debug.Log (msg);}

然后打包成xcode项目,就可以看到能够正常运行了,具体代码不写了,这个不难理解。




0 0