第一部分:
第一部分:iOS传消息到Unity3d中。
1.
首先用Unity3d创建一个Plain,并调整好摄像机的角度以及光源的位置,如下所示:

2.
然后我们创建一个Cube,我们会在iOS中用Objective-C代码来控制它旋转:

3.
然后我们创建一个Rotate.js的脚本并把它关联到Cube上:
-
var vrotate : Vector3;
-
-
//Rotate Left
-
function RotateLeft()
-
{
-
print("Rotate Left");
-
transform.Rotate(Vector3.up * Time.deltaTime * 100, Space.World);
-
}
-
-
//Rotate Right
-
function RotateRight()
-
{
-
print("Rotate Right");
-
transform.Rotate(Vector3.down * Time.deltaTime * 100, Space.World);
-
}
-
-
//Rotate Up
-
function RotateUp()
-
{
-
print("Rotate Up");
-
transform.Rotate(Vector3.right * Time.deltaTime * 100, Space.World);
-
}
-
-
//Rotate Down
-
function RotateDown()
-
{
-
print("Rotate Down");
-
transform.Rotate(Vector3.left * Time.deltaTime * 100, Space.World);
-
}
上面的四个函数分别朝不同角度选择Cube。我们会在iOS程序中调用这几个Unity3d中的函数。
4.
然后在Unity3d中Build为XCode项目,打开该项目。首先创建一个基于NSObject的类,名为MyViewInit,我们会将我们自己的界面初始化代码都放在其中。修改MyViewInit.h如下:
-
//
-
// MyViewInit.h
-
// Unity-iPhone
-
//
-
// Created by tao hu on 9/15/12.
-
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
-
//
-
-
#import <UIKit/UIKit.h>
-
-
@interface MyViewInit : NSObject
-
-
-
+(void)CreateButton:title rect:(CGRect)rect action:(SEL)action controller:(UIViewController *)controller;
-
+(void)CreateTitle:(UIViewController *)controller;
-
+(void)LeftButtonPressed;
-
+(void)RightButtonPressed;
-
+(void)UpButtonPressed;
-
+(void)DownButtonPressed;
-
+(void)Init:(UIViewController *)controller;
-
-
-
@end
其中的方法都是类方法。在Init函数中我们完成了所有我们自定义界面的绘制(添加了四个UIButton和一个UILabel)。
5.
修改MyViewInit.m如下:
-
//
-
// MyViewInit.m
-
// Unity-iPhone
-
//
-
// Created by tao hu on 9/15/12.
-
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
-
//
-
-
#import "MyViewInit.h"
-
-
@interface MyViewInit ()
-
-
@end
-
-
@implementation MyViewInit
-
-
-
//创建按钮
-
+(void)CreateButton:title rect:(CGRect)rect action:(SEL)action controller:(UIViewController *)controller
-
{
-
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
-
-
//设置按钮范围
-
btn.frame = rect;
-
-
//设置按钮显示内容
-
[btn setTitle:title forState:UIControlStateNormal];
-
-
//设置按钮改变后绑定的响应方法
-
[btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
-
-
//加入View中
-
[controller.view addSubview:btn];
-
}
-
-
-
//创建标签
-
+(void)CreateTitle:(UIViewController *)controller
-
{
-
//创建label视图
-
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
-
//设置显示内容
-
label.text = @"旋转控制";
-
//设置背景颜色
-
label.backgroundColor = [UIColor blueColor];
-
//设置文字颜色
-
label.textColor = [UIColor whiteColor];
-
//设置显示位置居中
-
label.textAlignment = UITextAlignmentCenter;
-
//设置字体大小
-
label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];
-
-
[controller.view addSubview:label];
-
}
-
-
-
//向左按钮
-
+(void)LeftButtonPressed
-
{
-
UnitySendMessage("Cube", "MoveLeft","");
-
}
-
-
//向右按钮
-
+(void)RightButtonPressed
-
{
-
UnitySendMessage("Cube", "MoveRight","");
-
}
-
//向上按钮
-
+(void)UpButtonPressed
-
{
-
UnitySendMessage("Cube", "MoveUp","");
-
}
-
-
//向下按钮
-
+(void)DownButtonPressed
-
{
-
UnitySendMessage("Cube", "MoveDown","");
-
}
-
-
-
+(void)Init:(UIViewController *)controller
-
{
-
[self CreateTitle:controller];
-
-
[self CreateButton:@"左旋转" rect:CGRectMake(0, 50, 100, 40) action:@selector(LeftButtonPressed) controller:controller];
-
[self CreateButton:@"右旋转" rect:CGRectMake(0, 100, 100, 40) action:@selector(RightButtonPressed) controller:controller];
-
[self CreateButton:@"上旋转" rect:CGRectMake(0, 150, 100, 40) action:@selector(UpButtonPressed) controller:controller];
-
[self CreateButton:@"下旋转" rect:CGRectMake(0, 200, 100, 40) action:@selector(DownButtonPressed) controller:controller];
-
}
-
-
-
-
@end
其中:
UnitySendMessage函数有三个参数:
第一个是Scene中的模型的名称,第二个是已经绑定在模型上的某个函数,第三个是char *类型的参数,用来将参数传递给这个函数。
我们可以用这个方法调用Unity3d中的任意一个模型上的任意一个函数。
6.
最后我们要做的是在程序启动时调用上面的Init函数。找到AppController.mm文件:
-
int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion)
函数在Unity3d程序启动时会被调用,其中的EAGLView 就是Unity3d的背景的那个View。我们在这个函数中调用我们的Init函数。修改OpenEAGL_UnityCallback如下:
-
int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion)
-
{
-
CGRect rect = [[UIScreen mainScreen] bounds];
-
-
// Create a full-screen window
-
_window = [[UIWindow alloc] initWithFrame:rect];
-
-
-
EAGLView* view = [[EAGLView alloc] initWithFrame:rect];
-
UnityViewController *controller = [[UnityViewController alloc] init];
-
-
-
sGLViewController = controller;
-
-
#if defined(__IPHONE_3_0)
-
if( _ios30orNewer )
-
controller.wantsFullScreenLayout = TRUE;
-
#endif
-
-
controller.view = view;
-
[_window addSubview:view];
-
-
-
[MyViewInit init:controller];
-
-
-
if( !UnityUseOSAutorotation() )
-
{
-
_autorotEnableHandling = true;
-
[[NSNotificationCenter defaultCenter] postNotificationName: UIDeviceOrientationDidChangeNotification object: [UIDevice currentDevice]];
-
}
-
-
......
其实我们只加入了一句话:
-
[MyViewInit init:controller];
并在该文件首部加入:
7.
好了,终于要在真机上运行了:
初始界面:

点击下旋转按钮:

旋转Cube:

我们发现在XCode的Console窗口中输出了Unity3d中的pirnt语句。因此,Unity3d中的print函数在真机上运行时是输出到XCode的Console中。
(第二部分)
上篇中我们介绍Unity3d与iOS交互的第一部分:iOS传消息到Unity3d中。今天我们介绍第二部分:在Unity3d界面中传递消息到iOS中。下面我们开始吧:
如下图所示,本章我们的目标是在Unity3D 界面中添加两个GUI按钮,并且在iPhone上点击这两个按钮后分别弹出两个IOS 高级界面的对话框。相信盆友们对GUI应该不会太陌生,在这里我在前调一下GUI就是Unity3D 提供的一套UI系统。图中的两个UI 按钮我就是用GUI做出来的。

Project栏目中创建一个c#脚本,命名为Main.cs ,之前没有使用过 C#写脚本,今天我用C#来写这个脚本,哇咔咔~~~ 如下图所示将脚本拖动在摄像机上,脚本中声明两个Texture 类型变量用来保存按钮绘制的图片资源。
Main.cs 代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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();
}
}
}
|
这里详细说一下SDK这个类,这个类我们看作它是一个管理类,它不赋值在任意对象身上,只接受调用管理,点击两个按钮后将分别调用下面方法中的_ActivateButton0() 与 _ActivateButton1(),而这两个方法则是去调用xcode 我们自己实现的方法_pressButton0() 与 _pressButton1(), 前提上须下面代码中的注册,这样子导出项目的时候xcode会帮我们 生成注册信息,我们只须要实现这两个方法就可以了。
SDK.cs 代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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
();
}
}
}
|
这样子Unity3D 部分已经完成,将Untiy3D项目导出成Xcode项目,我们用Xcode打开它。添加Unit3D中GUI按钮点击后的响应事件。创建一个类命名为MyView.h 、MyView.m,用它来接收Unity3D 回馈回来的消息,_PressButton0 与 _PressButton1 这两个方法在Unity3D中已经注册过,所以在这个类中我们须要对它进行Xcode中的实现。
MyView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#import "MyView.h"
@implementation
MyView
//接收Unity3D 传递过来的信息
void
_PressButton0()
{
UIAlertView
*alert
=
[[UIAlertView
alloc]
init];
[alert
setTitle:@"雨松MOMO程序世界"];
[alert
setMessage:@"点击了第一个按钮"];
[alert
addButtonWithTitle:@"确定"];
[alert show];
[alert
release];
}
void
_PressButton1()
{
UIAlertView
*alert
=
[[UIAlertView
alloc]
init];
[alert
setTitle:@"雨松MOMO程序世界"];
[alert
setMessage:@"点击了第二个按钮"];
[alert
addButtonWithTitle:@"确定"];
[alert show];
[alert
release];
}
@end
|
OK大功告成,连上真机运行我们的项目,我们在iPhone中点击了Unity3D 中GUI这两个按钮后,通过消息的回馈顺利的弹出IOS高级界面 的对话框,哇咔咔~

最后欢迎各位盆友可以和MOMO一起讨论Unity3D游戏开发,冬天就要来啦大家注意添加衣服,注意身体健康噢。哇咔咔~~~ 附上Unity3D工程的下载地址,Xcode项目我就不上传了,须要的自己导出。不早了,大家晚安,哇咔咔~~