unity与iOS之间的简单交互

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)
}
#endif

void _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项目,就可以看到能够正常运行了,具体代码不写了,这个不难理解。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值