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);
}
打开Unity3D导出的AppController.mm这个类,头文件处先导入我们的这个类 #import “MyView”找到下面这个方法,来添加viewint OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion)
EAGLView 是Unity3D 背景的那个View, 下面我们添加一个我们自己写的View 覆盖在它上面。
// Create a full-screen window
_window = [[UIWindow alloc] initWithFrame:rect];
EAGLView* view = [[EAGLView alloc] initWithFrame:rect];
[_window addSubview:view];
MyView * myView = [[MyView alloc] init];
[_window addSubview:myView.view];
贴出MyView的代码,写完发现忘释放内存了,呵呵,懒得改了,本章主要的介绍的不是这个哦。
//
// MyView.m
// Unity-iPhone
//
// Created by 雨松MOMO on 11-11-1.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#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 = @"雨松MOMO的程序世界";
//设置背景颜色
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
这里我主要说一下下面这个方法,它是Unity底层帮我们写好的一个方法,意思iPhone向向Unity发送消息,
参数1:场景中的模型名称,Cube就是我们定义的一个箱子。
参数2:脚本方法名称MoveLeft就是上面脚本中的方法,
参数3:为一个char *类型的 可以向Unity中传递数据。
UnitySendMessage(“Cube”,”MoveLeft”,””);
我们可以向Unity3D中任意模型发送消息调用它绑定的脚本中的方法,当前前提是模型名称、方法名称、 参数都填写正确。
这里4个按钮都是以这种方式传递消息,下面是iPhone 真机的效果图,我们触摸点击4个高级界面的按钮可以实现Unity3D世界中的模型旋转, 所以大家一定要切记这个方法,很重要噢,哇咔咔~
MyViewController * myView = [[MyViewController alloc] init];
[sGLViewController.view addSubview:myView.view];
MyViewController是我们新定义的,
也就是说把我们的写的视图添加至
sGLViewController当中,这样就完事OK啦。 Unity每次升级都会小改动一下,所以我们也需要小改动一下,哈哈!
2013年2月25日补充
在Unity4中 把之前的sGLViewController 替代成了 UnityGetGLViewController() 那么在Unity4中简单的修改一下上面的代码即可。
MyViewController * myView = [[MyViewController alloc] init];
[UnityGetGLViewController().view addSubview:myView.view];
- 本文固定链接: https://www.xuanyusong.com/archives/517
- 转载请注明: 雨松MOMO于 雨松MOMO程序研究院 发表



…..
雨凇sama,我现在用的unity版本是4.6.3,请问修改view是在哪里修改呢?appcontroler.mm里面找不到之前的方法了。然后还有个问题想请教,我想在unity做一个按钮,点击了才显示这个view而不是改了之后一直显示,有什么思路可以提供吗?谢谢
请问你这个问题解决了吗,点击按钮显示View这个….
您好,我按照您的教程自己走了一遍下来在IOS真机运行时调用UnitySendMessage方法就提示SendMessage: object Cube does not have receiver for function MoveUp 等等,我重新检查了很多遍脚本绑定(拖动脚本到Cube上),然后打包到xcode 运行界面都没问题,就是unity怎么都找不到js的方法,请问解决的思路在哪里?再次感谢
我也是这个问题,你是怎么解决的呢
Unity4.6, Xcode6下,我是在- (void)startUnity:(UIApplication*)application 函数的末尾添加MyView * myView = [[MyView alloc] init]; [UnityGetGLViewController().view addSubview:myView.view]; 的
怎么不可以点击转成c#?
郁闷了,一直在android上面做开发的。
我unity4.5.2, xcode5.1下,MyView这个视图怎么显示不出来啊,有没有大神出来指点一下啊,接口都调用了,就是视图显示不出来
showGameUI在/UI/UnityAppController+ViewHandling这里,把那两句加在// UI hierarchy这注释下面就可以了。
unity4.5.2, xcode5.1下 可以做到将unity场景嵌入到源生中吗 源生中有视图,现在就是分别单独运行源生视图和U3D视图没有问题 但是两个同时实例化 就会报错 U3D找不到自己的方法了 我QQ120987401 望不吝赐教
很麻烦。。
给你发QQ了 我测试成功了一次 还算可以 不过测试DEMO OK在整合的时候发现了新的问题 加入的功能就是U3D传值功能 不过出现了库连接问题 问题离线你QQ了 有空看下啊
showGameUI在/UI/UnityAppController ViewHandling这里,把那两句加在// UI hierarchy这注释下面就可以了。
我unity4.5.2, xcode5.1下,MyView这个视图怎么显示不出来啊,有没有大神出来指点一下啊,接口都调用了,就是视图显示不出来
太及时了
这个太及时了!
unity4.3, xcode5下, 只要在UnityAppController下的showGameUI里面加上这两句:MyView * myView = [[MyView alloc] init]; [window addSubview:myView.view];就可以用了哦~
Momo 哥,我unity 4.3, xcode 5,没有找到OpenEAGL_UnityCallback函数,是不是UNITY 又做了修改,那现在该怎么操作呢
Votre ?tude :…
I?d need to take advice from you here. Which is not something I normally do! I enjoy reading a piece which will get people to think. Also, thank you for permitting me to comment!…
_window = [[UIWindow alloc] initWithFrame:rect];EAGLView* view = [[EAGLView alloc] initWithFrame:rect];[_window addSubview:view];MyView * myView = [[MyView alloc] init];[_window addSubview:myView.view];在UnityAppController.mm- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions方法里面添加?
是不是改成GLView了??
EAGLView从哪里获取??是不是我用的4.2版本名字换了???请赐教?
问一个问题,能否动态加载服务器上的模型在ios设备中?
请教如何 删除这个自定义的view,删除后显示出来unity的view,然后再unity里面点击一个按钮再次出来这个view,请赐教。或者给个思路。谢谢!
这一集建议更新一下,新手有些做不来
unity 4.1.5里,用这种方式已经不行了。