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程序研究院 发表



你好、我想从ios里面向Unity传一个数组,可以做到吗、我看你说只能传一个char*、是这样的、我想在ios里得到itunes里面的所有歌曲名称的数组、然后传到Unity里面,作为一个list列表、点击之后再把点中的那个歌曲的名称传到ios里、进行播放
看教程博客点广告是美德。
谢谢。。
郁闷了。。一直看着momo的博客在android上开发,这篇竟然。。。好吧我导入到eclipse中去重写代码试试。。能不能用
三件非常重要的事1:关于你2:关于我3:关于我们一:我点了你这个3次广告本次二:我电脑被偷了三:请问,我能在U3D前面放一个 摄像头的画面么??
当然可以 用RenderTexture 谷歌吧~少年 哈哈
我有点忧伤。我电脑被偷你居然不当回事
好吧, 争取你早日找回你的电脑,可以试试报警 哈哈~
最后 3.5 和 4 的修订,应该是 MyView 而不是 MyViewController 吧。前面新建的类名字是 MyView。十分误导。
好吧,可能我写错了。
首先 要谢谢你! MoMo我现在想做这么一个demo :“通过鼠标来实现立方体的旋转” 。也就是通过“鼠标”在屏幕上的滑动“手势”让Cube按照“鼠标”的“手势”方向旋转!我现在做的是通过判断Input.GetMouseButtonDown()和Input.GetMouseButtonUp()起始位置,但是在旋转的时候很不流畅,请MoMo指点迷津!
雨松能稍微说下如何移除吗?对obj-c刚入门,多谢了。
MOMO下载Eclipse是那个版本?是eclipse-jee-juno-SR2-win32-x86_64.zip还是eclipse-java-juno-SR2-win32-x86_64.zip ?我是学习.net。java还没有玩过.吼吼……
我开始用的是eclipse 3.5 现在直接用ADT了 因为下载Android Sdk 自带adt
谢谢!!
MOMO老师,怎么用代码创建一个空游戏对象啊。
打开Unity3D导出的AppController.mm这个类是什么意思???AppController.mm这个类到底是什么样的?viewint OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion)是谁的方法??
这个类是Unity生成的。。
嗯,明白了,谢谢
恩呢 。。。
MOMO我想问一下。如果我从Unity向Object-c传string类型的参数该怎么传呢?
sendmessage 可以传递的。。
int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion){ // TODO: in splash do use info.plist values and push creation earlier CreateViewHierarchy(); for (int openglesApi = kEAGLRenderingAPIOpenGLES2 ; openglesApi >= kEAGLRenderingAPIOpenGLES1 && !_context ; –openglesApi) { if (!UnityIsRenderingAPISupported(openglesApi)) continue; _context = [[EAGLContext alloc] initWithAPI:openglesApi]; } if (!_context) return false; if (![EAGLContext setCurrentContext:_context]) { _context = 0; return false; } const GLuint colorFormat = UnityUse32bitDisplayBuffer() ? GL_RGBA8_OES : GL_RGB565_OES; if( !CreateWindowSurface( (EAGLView*)UnityGetGLView(), colorFormat, GL_DEPTH_COMPONENT16_OES, UnityGetDesiredMSAASampleCount(MSAA_DEFAULT_SAMPLE_COUNT), NO, &_surface ) ) { return false; } glViewport(0, 0, _surface.targetW, _surface.targetH); *window = UnityGetMainWindow(); *screenWidth = _surface.targetW; *screenHeight = _surface.targetH; *openglesVersion = _context.API; _glesContextCreated = true; CGRect rectCode = CGRectMake(0, 0, *screenWidth, *screenHeight); *window = [[UIWindow alloc] initWithFrame:rectCode]; EAGLView *glView = [[EAGLView alloc] initWithFrame:rectCode]; [*window addSubview:glView]; [glView release]; myViewController *myView = [[myViewController alloc] init]; [*window addSubview:myView.view]; return true;}unity4.0;XCodea最新版 ,自定义View没有显示,什么原因????救命
MyViewController * myView = [[MyViewController alloc] init];[sGLViewController.view addSubview:myView.view]; 我这里正常啊。。。
你那个 XCode 和 unity3d 分别是什么版本?
你好,为什么我的xcode里提示sGLViewController未定义。。。我的unity是4.0 xcode是4.6。。。。这怎么办呢???
我知道了 是unity4的问题, 这个方法在unity3上是正常的。 unity4我晚上回家做个例子放在博客中吧。。
谢谢了!
不好意思, 这两个天临时有点事情没能来看这个问题,非常抱歉, 不过这两天我一定会贴上去的。
好的,谢谢哈,期待问题的解决
已经更新上去了。 看看吧
已经更新上去了, 看看更新内容吧
您好,我的程序在ipad上运行,如何让这个ui窗口全屏并且随着屏幕旋转呢?
看看 IOS的API吧
麻烦请教以下,如何在ios程序里面打开本地的html文件呢?
从IOS 层面 打开 然后再传给unity
我在int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion)这个方法里加入了 [_window addSubview:view]; MyView *myView=[[MyView alloc] init]; [_window addSubview:myView.view];出现了如下错误,如果不加入以上代码的话,就不会报错。求解啊! “_OBJC_CLASS_$_MyView”, referenced from: objc-class-ref in AppController.old: symbol(s) not found for architecture i386clang: error: linker command failed with exit code 1 (use -v to see invocation)
我把新写的MyView.h和MyView.m 添加到 Compile Sources里,就不会抱以上错了,但是程序崩了,跳到 新写的那个类MyView.m里的-(void)viewDidLoad方法里,断到 UILabel *label=[[UILabel alloc] initwithframe:CGRectMake(0,0,320,480)];求解释啊。MOMO
没遇见这个错误,, 。。。。 标示亚历山大!!!
谢谢回答啊momo,我好好看看吧
hi MOMO,我为按钮添加了seletor,但是程序运行之后我点击按钮,起初提示我不识别selector,但是我查了函数名字是匹配的,后来我开启了僵尸调试,错误信息变成了这样..有什么解决办法吗? [TestViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0xa61f9b0
MOMO你好 ,我的MyView添加之后出现:Undefined symbols for architecture i386: “_OBJC_CLASS_$_MyView”, referenced from: objc-class-ref in AppController.old: symbol(s) not found for architecture i386clang: error: linker command failed with exit code 1 (use -v to see invocation)求解决方式
你中间某个环节出现错误了, 仔细检查一下吧。
hi,你在创建viewcontroller的时候,会让你勾选target,记得把unity iphone和unity iphoneSimulator都勾选上,就不会报错了
非常感谢。。对obc 还有xcode 基本不怎么会。。只是会u3d。。已经按照方法把google maps 加入 到u3d 了 开心。。
加上就好了。。
Hi,MOMO,能否发布Xcode项目的源码呢
xcode工程太大了。。
哦还有
MyView * myView = [[MyView alloc] init];
[_window addSubview:myView.view];
这个自定的VIEW 只能在OpenEAGL_UnityCallback这么方法里面调用?
能不能通过一个自定义的方法来调用?
那有没有办法 移除这个覆盖在U3D上的自定义VIEW呢?