IOS与Unity交互

学习了宣雨松的两篇Unity和IOS交互的文章,自己动手做了下,遇到了些问题,在此记录

先说IOS发送消息给Unity:(文章地址:http://www.xuanyusong.com/archives/517)

先在unity写好脚本,并拖到cube上,代码是给IOS来调用的,代码如下:

var vrotate : Vector3;
 
//向左旋转
function MoveLeft()
{
    var rotate : float = Time.deltaTime * 100;
    vrotate = Vector3.up * rotate;
    transform.Rotate(vrotate, Space.World);
}
 
//向右旋转
function MoveRight()
{
    var rotate : float = Time.deltaTime * 100;
    vrotate = Vector3.down* rotate;
    transform.Rotate(vrotate, Space.World);
}
 
//向上旋转
function MoveUp(){
     var rotate : float = Time.deltaTime * 100;
    vrotate = Vector3.right* rotate;
    transform.Rotate(vrotate, Space.World);
}
 
//向下旋转
function MoveDown(){
    var rotate : float = Time.deltaTime * 100;
    vrotate = Vector3.left* rotate;
    transform.Rotate(vrotate, Space.World);
}

接下来将这个Unity工程导出成Xcode项目,双击xcodeproj后缀的工程文件,默认是xcode打开此文件,

右键Classws文件夹 → New File... → Objective-C File 取名MyView,那么会添加MyView.m的文件,此时把后缀m改成mm

同样在新建个Header File,名也为MyView,那么会添加MyView.h的文件

MyView.h的代码如下:

#ifndef Unity_iPhone_MyView_h
#define Uni

#import <UIKit/UIKit.h>

@interface MyView : UIViewController
    
@end

#endif

MyView.mm的代码如下:

#import "MyView.h"
@implementation MyView

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(void)viewDidLoad{
    [super viewDidLoad];
    //创建label视图
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    //设置显示内容
    label.text = @"IOS调用Unity";
    //设置背景颜色
    label.backgroundColor = [UIColor blueColor];
    //设置文字颜色
    label.textColor = [UIColor whiteColor];
    //设置显示位置居中
    label.textAlignment = UITextAlignmentCenter;
    //设置字体大小
    label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];
    
    //创建按钮
    UIButton *button0 = [UIButton buttonWithType:1];
    //设置按钮范围
    button0.frame = CGRectMake(0, 40, 100, 30);
    //设置按钮显示内容
    [button0 setTitle:@"矩形左旋转" forState:UIControlStateNormal];
    //设置按钮改变后 绑定响应方法
    [button0 addTarget:self action:@selector(LeftButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    
    //创建按钮
    UIButton *button1 = [UIButton buttonWithType:1];
    //设置按钮范围
    button1.frame = CGRectMake(0, 100, 100, 30);
    //设置按钮显示内容
    [button1 setTitle:@"矩形右旋转" forState:UIControlStateNormal];
    //设置按钮改变后 绑定响应方法
    [button1 addTarget:self action:@selector(RightButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    
    //创建按钮
    UIButton *button2 = [UIButton buttonWithType:1];
    //设置按钮范围
    button2.frame = CGRectMake(0, 160, 100, 30);
    //设置按钮显示内容
    [button2 setTitle:@"矩形上旋转" forState:UIControlStateNormal];
    //设置按钮改变后 绑定响应方法
    [button2 addTarget:self action:@selector(UpButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    
    //创建按钮
    UIButton *button3 = [UIButton buttonWithType:1];
    //设置按钮范围
    button3.frame = CGRectMake(0, 220, 100, 30);
    //设置按钮显示内容
    [button3 setTitle:@"矩形下旋转" forState:UIControlStateNormal];
    //设置按钮改变后 绑定响应方法
    [button3 addTarget:self action:@selector(DownButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    
    //向view添加
    [self.view addSubview:label];
    [self.view addSubview:button0];
    [self.view addSubview:button1];
    [self.view addSubview:button2];
    [self.view addSubview:button3];
}

//向左按钮
-(void)LeftButtonPressed{
    UnitySendMessage("Cube","MoveLeft","");
}

//向右按钮
-(void)RightButtonPressed{
    UnitySendMessage("Cube","MoveRight","");
}
//向上按钮
-(void)UpButtonPressed{
    UnitySendMessage("Cube","MoveUp","");
}

//下按钮
-(void)DownButtonPressed{
    UnitySendMessage("Cube","MoveDown","");
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)dealloc {
    [super dealloc];
}

@end

找到UnityAppController.mm此文件:
导入#include “MyView.h”

在UnityAppController.mm文件下的这个方法(didFinishLaunchingWithOptions)里面
加入如下代码:

MyView * myView = [[MyView alloc] init];
[UnityGetGLView() addSubview:myView.view];
return YES;

大功告成!

Unity发送消息给IOS:(文章地址:http://www.xuanyusong.com/archives/521)

新建一个Unity脚本,名为SDK

using UnityEngine;
using System.Runtime.InteropServices;
 
public class SDK
{ 
     //导出按钮以后将在xcode项目中生成这个按钮的注册,
     //这样就可以在xocde代码中实现这个按钮点击后的事件。
     [DllImport("__Internal")]
     private static extern void _PressButton0 ();
 
     public static void ActivateButton0 ()
     {
 
        if (Application.platform != RuntimePlatform.OSXEditor) 
        {
            //点击按钮后调用xcode中的 _PressButton0 ()方法,
            //方法中的内容须要我们自己来添加
            _PressButton0 ();
        }
     }
 
     //和上面一样
     [DllImport("__Internal")]
     private static extern void _PressButton1 ();
 
     public static void ActivateButton1 ()
     {
         if (Application.platform != RuntimePlatform.OSXEditor) 
        {
            _PressButton1 ();
        }
     } 
}

再新建一个脚本Main.cs

using UnityEngine;
using System.Collections;
 
public class Main : MonoBehaviour {
 
//声明两个Texture变量,图片资源在外面连线赋值
public Texture Button0;
public Texture Button1;
 
    // Use this for initialization
    void Start () {
 
    }
 
    // Update is called once per frame
    void Update () {
 
    }
 
    //这个方法用于绘制
    void OnGUI() {
        //绘制两个按钮
        if(GUI.Button(new Rect(0,44,120,120),Button0))
        {
            //返回值为ture说明这个按钮被点击
            SDK.ActivateButton0();
        }    
 
        //绘制两个按钮
        if(GUI.Button(new Rect(200,44,120,120),Button1))
        {
            //返回值为ture说明这个按钮被点击
            SDK.ActivateButton1();
        }    
    }
}

将Untiy3D项目导出成Xcode项目,我们用Xcode打开它。添加Unit3D中GUI按钮点击后的响应事件。创建一个类命名为MyView.h 、MyView.mm,用它来接收Unity3D 回馈回来的消息,_PressButton0 与 _PressButton1 这两个方法在Unity3D中已经注册过,所以在这个类中我们须要对它进行Xcode中的实现。

MyView.h代码如下:

#ifndef Unity_iPhone_MyView_h
#define Unity_iPhone_MyView_h

@interface MyView : UIViewController

void _PressButton0();

void _PressButton1();

@end
#endif

MyView.mm代码如下:

关键点:unity调用Xcode封装的函数,声明时需要在自己写的MyView类的头部引用extern "C"

extern "C"
#import "MyView.h"



@implementation MyView



//接收Unity3D 传递过来的信息

void _PressButton0()
{
    UIAlertView *alert = [[UIAlertView alloc] init];
    [alert setTitle:@"测试Unity向IOS发送消息"];
    [alert setMessage:@"点击了第一个按钮"];
    [alert addButtonWithTitle:@"确定"];
    [alert  show];
    [alert release];
}

void _PressButton1()
{
    
    UIAlertView *alert = [[UIAlertView alloc] init];
    [alert setTitle:@"测试Unity向IOS发送消息"];
    [alert setMessage:@"点击了第二个按钮"];
    [alert addButtonWithTitle:@"确定"];
    [alert  show];
    [alert release];
}

@end

 

posted @ 2015-08-20 16:01  MrZivChu  阅读(1254)  评论(0编辑  收藏  举报
分享按钮