- 最后登录
- 2018-6-29
- 注册时间
- 2011-7-1
- 阅读权限
- 20
- 积分
- 359

- 纳金币
- 335582
- 精华
- 0
|
unity3d IPhone触摸动画
Unity3D IPhone触摸动画教程
I started working with Unity iPhone a few days ago and I’ve been digging around in the example game project files and reading thru forum posts looking for clues on how to make stuff work. I’ve found a few good/basic tutorials but I was looking for something a little different so I figured I’d just dive in.
For my first “hello world” app, I came up with a basic goal: touch the screen to play an animation on an object.
After an hour or two digging around in forums, scripting manual, and the iPhone-Match example game, I managed to come up with a simple little app that actually works on my iPhone!
I was so excited to see this thing working and it was so much fun to put together that I figured why not put together a tutorial!? So I did!
This tutorial covers quite a few basics including:
– playing animations on a 3d object when you touch and un-touch the screen.
– creating and animating a simple object in maya
– setting up the animations in Unity
– setting up a basic 2D scene with default lighting and Main Camera position
– adding a 2D background image
You can download the entire Tut1-TouchAnim.zip project before you get started or you can do the whole thing from scratch just by following along with the steps below. Let’s get started!
1. Create a new Project: Go to File –> New Project –> Set… and browse to where you keep your Unity projects (mine are in DocumentsUnity Projects), then in the Save As… field type “Tut1-TouchAnim” and click the Save button.
2. Create Project folders: In the Project panel, click the Create button and then select Folder from the list. This will add a new folder to your Project named New Folder. The folder is also created on the hard drive in your Tut1-TouchAnimAssets directory.
Click once on the folder name (New Folder) to edit it and type “Objects” (without the quotes).
Click anywhere in the Project panel to deselect the Objects folder then click the Create button again and select Folder from the list. Rename this 2nd folder “Scripts“.
Click anywhere in the Project panel to deselect the Scripts folder then click the Create button again and select Folder from the list. Rename this 2nd folder “Textures“.
3. Save your Scene: Go to File –> Save Scene and name it “Tut1-TouchAnim”
4. Maya Setup: In Maya go to Window –> Settings/Preferences –> Preferences and then click on Settings in the Preferences window.
– Set the Up axis to “Y” since Y is up in Unity.
–Change the Linear Working Units to “Millimeter”. Currently I find that this is the best scale for getting a good (close enough?) 1:1 size from Maya to Unity.
Note: please feel free to comment on the above Maya settings if you’ve found a better setup.
5. Create an object in Maya: I’m using Maya 2008, but the general ideas here will apply to any modeling/animation program.
Make a cube by going to Create –> Polygon Primitives –> Cube and then draw out a rough cube on the grid.
Select the cube and then in the Channel Bar under INPUTS click on polyCube1 (yours may be named slightly different). Set the Width, Height and Depth to 20 (this assumes your working units are set to millimeters).
Set the Translate X:0, Y:0 and Z:0 so the cube is centered on the grid.
Set the Rotate X:45, Rotate Y:45, and Rotate Z:45. This will make the cube kinda stand up on one corner to give the animation a little visual interest.
6. Animate the cube:
We’re going to be making 2 very simple animations where the cube will rotate “forward” 180 degrees and then rotate “backward” 180 degrees to its default position.
First create the forward animation:
With the Timeline slider at Frame 1 and your Cube selected, press the “s” key to add a frame. Or go to Animate –> Set key (you’ll need to be in Animation mode).
Drag the slider on the timeline to Frame 10, change the cube’s Rotate Y:225 and press “s” to add a new keyframe at frame 10.
Next create the backward animation:
Drag the timeline slider to Frame 20, change the cube’s Rotate Y:45 and press the “s” key to add a new keyframe at frame 20.
That’s it for the animations. As far as Maya is concerned, we’ve only created 1 animation, but in Unity we’ll see how this single Maya anim can be made into 2.
7. Save the animated cube: In Maya go to File –> Save Scene As… and browse to your Tut1-TouchAnimAssetsObjects directory and save it there as cubeAnim.mb.
8. Setting up the animated object in Unity: Switch back to Unity and you’ll see that there’s now a file in the project’s Object folder named “cubeAnim”.
Select cubeAnim (top node) in the project’s Objects folder and then click on the Settings… button. This will bring up a dialogue that allows you to adjust the objects Import settings.
Click the Add clip button which will add a default animation to the list below named “idle”. Click on the word “idle” and type in forward. Leave first frame set to 1 and change the last frame to 10.
Click the Add clip button again, click on the “idle” text, and type in backward. Leave the first frame set to 11 and change the last frame to 20.
Leave “loop frame” unchecked for both clips.
Click the Import button and after a few seconds you should see in the Project panel under cubeAnim that there are now 2 animations named backward and forward. If you select cubeAnim and look in the Inspector under Animations, you’ll see that Element 0 has the “forward” animation assigned and Element 1 has the “backward” animation assigned.
9. Adding your object to the Scene: First thing we’re going to do is add a new empty Game Object to the scene which we will Parent our cubeAnim object to. Go to Game Object –> Create Empty. You’ll see that a GameObject has been added to the Hierarchy panel.
Next select the cubeAnim object in the Projects panel and drag-and-drop it onto the GameObject in the Hierarchy panel to make the cubeAnim a child of GameObject. Your cubeAnim object should now also be visible in the Scene and in the Game panel (if you have it open).
Note: you could simply drag cubeAnim into the scene without parenting it to a GameObject, but you’ll find that when the cubeAnim’s animation plays, it’ll always reset its position in the Scene to Frame 1 as it was set in Maya. With the cube parented to a GameObject, you can move the GameObject anywhere in the scene and the animation will play at that location.
10. Adding a script: In the Project panel, select the Scripts folder, click the Create button, and select JavaScript from the drop down list. This will add a new script to the Scripts folder named NewBehaviorScript. Click on the scripts text and rename it to TouchAnim.
Double click the TouchAnim script in the Projects panel to open it up in Unitron (or whatever script editor you have set).
Download my TouchAnim.js file (right or ctrl click to save as…) and copy/paste the contents into your own script. Save your script file and then task back to Unity. If you click on the TouchAnim script in the Project panel, you should see the contents of the file in the Inspector.
11. Attach the script to your object: Select the TouchAnim script in the Project panel and then drag-and-drop it onto the cubeAnim object in the Hierarchy panel.
To confirm that the script is attached, select the cubeAnim object in the Hierarchy panel and you should see Touch Anim (Script) component listed in the Inspector.
12. Add a Mesh Collider to your object: Select cubeAnim in the Hierarchy panel and then go to Component –> Physics –> Mesh Collider. This will add the Mesh Collider component to your object — you should see it in the Inspector. Without a Mesh Collider the script won’t be able to detect a collision with your cube.
At this point, if you press the Play button on your Game view, you can mouse-click on/off the cube and it should animate forward and backward.
13. Adjusting the Main Camera : Something that I don’t understand about the default Main Camera Position and Rotation, is that it’s set to look at the scene from the back instead of the front. Which means any objects you import from Maya will look backwards in the Game view. We’re going to set up the Main Camera so that it’s facing (what I consider to be) forward and adjust its position so that it’ll nicely frame the background image we’ll be creating in the next step.
Select the Main Camera in the Hierarchy panel and set Position: 0,0, 8.1 and Rotation: 0, 180,0.
14. Adding a textured background: To give the scene a little nicer presentation, add a Plane by going to Game Object –> Create Other –> Plane. This will add a Plane at 0,0,0.
Select the Plane in the Scene or from the Hierarchy and set Rotation:90,0,0. The default position (0,0,0) and Scale (1,1,1) should be just right for the Main Camera we setup in the previous step.
Your new Plane will have the Mesh Collider component on it by default (mine does at least). You’ll need to remove this so that the script will only collide with the cube and not with the background. Select the Plane, then in the Inspector click the little “gear” icon next to the Mesh Collider component and select “Remove Component” from the dropdown list.
Download this default grid texture and put it in your Tut1-TouchAnimAssetsTextures directory.
In Unity, select the defGrid texture from the Project’s Textures folder and drag-and-drop it onto the Plane in the Scene view. If you adjusted the Main Camera position and the Plane rotation correctly in the previous steps, the Grid pattern should fit perfectly in the Game view.
15. Adding a Light to the scene: To get a nice bright rendering of the scene, add a light by going to Game Object –> Create Other –> Directional Light. This will create a new Directional Light at 0,0,0. Move the light somewhere near the Main Camera and rotate it so it points at the cube. My position is set to 0,0,15 and the rotation is set to 0,180,0.
All of the default light settings for the Directional Light should work fine, except change the Render Mode to “Force vertex”. I’m not sure yet what that does, but that’s how the iPhone-Match game has their Directional Light set.
16. Set the iPhone Bundle Identifier: I’m still a bit confused as to exactly how this works as I think it’s supposed to somehow match your App ID that you setup at the iPhone Developer Portal. But I set my Bundle Identifier to * (asterisks) so I assumed that’s what I’d enter in Unity… but that doesn’t work for me.
In Unity, go to Edit –> Project Settings –> Player to display the player settings in the Inspector. Next to iPhone Bundle Identifier, you should see “com.Company.ProductName“. I set this to com.bleepng.touchanim which works for me. You may need to play around with this on your side to get it to work properly.
17. Play it in Unity: We’re all done. If you press the Play button in your Game view, you should be able to click on and off of the cube to see the animation play forward and backward. Note that the animation should only play if you click directly on the cube, not on the background plane.
Note, if you want the animation to play when you click anywhere on the screen, simply remove the if (Physics.Raycast (ray, hit, 100)) lines from the script. Or add a Mesh Collider component to your background Plane.
18. Build & Run it on your iPhone: Setting up Xcode and configuring your iPhone to run apps is beyond the scope of this tutorial, so I’ll assume you’ve done all of that already.
Launch Xcode and plug in your iPhone. Be sure to unlock your phone or Xcode may give you warnings when it tries to install and launch the app on your phone.
In Unity, go to File –> Build & Run and then click the Build & Run button on the Build Game screen. At the Save Player window, type touchanim and click Save.
Once Unity finishes building the game, Xcode will compile everything, install it on your iPhone and automatically launch it (if your iPhone isn’t locked).
Conclusion:
When you're all done, your Unity project should look something like my setup in this screenshot (click the image to see full sized).
Hopefully you had as much fun following this tutorial as I had writing it. Feel free to leave comments on how to improve this tutorial, or if you find any bugs in it, or if you have any questions. Also please let me know if you’re aware of any other tutorials out there we should look at.
//=============================
我开始使用Unity iPhone的前几天,我一直在周围挖掘例如游戏项目文件,并通过论坛寻找线索就如何使东西工作职位阅读。我发现了几个不错的/基本教程,但我有一点不同的东西,所以我想我只是潜水英寸
我第一次的“Hello World”应用程序,我来到了一个基本目标:触摸屏上播放动画的对象。
脚本手册,和iPhone的匹配例如游戏,一个或两个小时在论坛周围挖后,我拿出一个简单的小应用程序实际上在我的iPhone!
我很高兴能看到这个东西的工作,它是这么多的乐趣放在一起,我想通为什么不能放在一起的教程!所以,我没有!
本教程涵盖了相当多,包括一些基本知识:
- 玩3D对象的动画,当你触摸触摸屏和联合国。
- 建立和动画Maya中的一个简单的对象
- 建立Unity3D的动画
- 设立一个基本的2D场景默认照明和主摄像机的位置
- 增加了2D的背景图片
在你开始之前,您可以下载整个TUT1 TouchAnim.zip项目或整个事情只是按照下面的步骤你可以从头开始。让我们开始吧!
1。创建一个新项目:转到文件 - >新建项目 - >设置...,并浏览到您保持您的Unity3D项目(我的文档Unity3D项目),然后在“另存为... ...”TUT1 TouchAnim“字段类型,然后单击“保存”按钮。
2。创建项目文件夹:在“项目”面板中,单击“创建”按钮,然后从列表中选择文件夹。这将添加一个新的文件夹到你的项目名为“新建文件夹。也是在您TUT1 TouchAnim 资产目录的硬盘驱动器上创建文件夹。
点击一次对其进行编辑,然后键入“对象”(不带引号)的文件夹的名称(新建文件夹)。
在“项目”面板中的任意位置单击以取消选择对象的文件夹,然后再次单击“创建”按钮,并从列表中选择文件夹。重命名“脚本”这第二个文件夹。
在“项目”面板中的任意位置单击以取消选择脚本的文件夹,然后再次单击“创建”按钮,并从列表中选择文件夹。重命名这第二个文件夹“纹理”。
3。保存你的场景:转到文件 - >保存现场,并将其命名为“TUT1 - TouchAnim”
4。玛雅安装:在Maya中去“窗口” - >“设置/首选项” - >“首选项”,然后单击“首选项”窗口中的“设置”。
- 向上轴设置为“Y”,因为Y是在Unity3D。
- 更改线性工作单位的“毫米波”。目前,我觉得这是最好的规模,获得一个很好的(足够接近?)1:1大小从MayaUnity3D。
注:请随意评论上述玛雅设置,如果你找到一个更好的安装。
5。在Maya中创建一个对象:我使用Maya 2008,但这里的总体思路,将适用于任何建模/动画节目。
要创建的多维数据集 - >多边形原语 - >立方,然后绘制出一个粗糙的网格立方体。
选择多维数据集,然后点击在频道栏下输入polyCube1(你可能被命名为略有不同)。设置宽度,高度和深度为20(这是假设你的工作单位设置为毫米)。
设置翻译的X:0,Y:0和Z:0,所以对电网为中心的多维数据集。
设置旋转:45,Y:45,旋转和旋转Z:45。这将使该多维数据集还挺站在一个角落里给一个小动画的视觉兴趣。
6。多维数据集的动画:
我们将作出了非常简单的动画多维数据集的“前进”的180度旋转,然后“落后”的180度旋转,其默认位置。
首先,创建正向动画:
随着时间轴滑块在第1帧和多维数据集选择的,按“S”键,添加一帧。或者去动画 - >设置键(你需要在动画模式)。
拖动时间轴滑块到第10帧,改变立方体的旋转Y:225,然后按“S”添加一个新的关键帧,10帧。
接下来,创建落后动画:
拖动时间轴滑块到第20帧,改变立方体的旋转Y:45,按“S”键,添加一个新的关键帧,在第20帧。
这对动画。至于玛雅人来说,我们只创建了1动画,但在Unity3D,我们将看到,这个单一的玛雅ANIM,可分为2。
7。保存动画的多维数据集:在Maya文件 - >保存现场,... ...,浏览到你的目录TUT1 TouchAnim 资产对象,并保存它作为cubeAnim.mb。
8。设立Unity3D的动画对象:切换回Unity3D,你会看到,现在有一个文件名为“cubeAnim”的文件夹在该项目的对象。
选择cubeAnim(顶级节点)在该项目的对象文件夹中,然后单击“设置...”按钮。这将弹出一个对话,让您调整对象导入设置。
单击“添加剪辑”按钮,会添加一个默认的动画名为“闲置”下面的列表。点击“闲置”字和类型,在前进。留下的第一帧设置为1,改变最后一帧到10。
再次点击“添加剪辑”按钮,点击“空转”的文本,然后键入落后。离开的第一帧集至11日,更改至20日的最后一帧。
离开这两个剪辑未选中“循环帧”。
点击“导入”按钮,几秒钟后,你应该看到cubeAnim在“项目”面板,现在有2动画名为向后和向前。如果您选择cubeAnim,看动画“下的”督察,你会看到,元素0“前进”动画分配和元素1“落后”的动画分配。
9。添加您的对象的场景:我们打算做的第一件事情是添加一个新的空游戏对象的场景,我们将家长我们cubeAnim对象。游戏对象 - >创建空。你会看到一个游戏物体已被添加到“层次”面板。
接下来在“项目”面板中选择cubeAnim的对象和拖放到游戏物体的层次“面板,使孩子的游戏物体cubeAnim它。你cubeAnim对象,现在应该也可以在现场并在游戏面板可见(如果您有打开它)。
注意:你可以简单地拖放到现场cubeAnim没有一个游戏物体养育它,但你会发现cubeAnim的动画播放时,它会永远在现场复位位置的第1帧,因为它是设置在Maya。父一个游戏物体的立方体,你可以在场景中任意位置的游戏物体移动和动画将在该位置发挥。
10。添加脚本:在“项目”面板中,选择“脚本”文件夹,单击“创建”按钮,然后从下拉列表中的JavaScript。这将添加一个新的脚本Scripts文件夹命名NewBehaviorScript。点击脚本文本,它重命名为TouchAnim。
在“项目”面板打开它在优利康(或您所设定的任何脚本编辑器)中,双击TouchAnim脚本。
下载我的TouchAnim.js文件(权利或CTRL单击“另存为... ...),复制/粘贴到您自己的脚本的内容。保存您的脚本文件,然后任务的Unity3D。如果您按一下在“项目”面板TouchAnim的脚本,你应该看到在检查该文件的内容。
11。将脚本到对象:选择在“项目”面板TouchAnim的脚本,然后拖动和拖放到cubeAnim对象的“层次”面板。
为了确认该脚本连接,选择在“层次”面板cubeAnim的对象,你应该看到触摸ANIM(脚本)在检查中列出的组件。
12。添加网撞机你的对象:选择“层次”面板cubeAnim,然后去组件 - >物理学 - >网对撞机。这将网撞机组件添加到您的对象 - 你应该看到,在督察。没有网对撞机的脚本将无法检测你的立方体碰撞。
在这一点上,如果您对您的游戏视图按“播放”按钮,您可以点击鼠标的多维数据集的开/关,应该向前和向后动画。
13。调整的主相机:默认的主摄像头的位置和旋转,我并不了解,就是它的设置在现场,从后面,而不是前面。这意味着从Maya中导入任何对象,你会向后看看在游戏中查看。我们打算成立的主摄像头,所以,它面临的(我认为是)前进,并调整其位置,这样它就会很好地框的背景图片,我们将在下一步创建。
选择在“层次”面板和设置位置的主相机:0,0,8.1和旋转:0,180,0。
14。添加纹理背景:为了给场景一点点更好演示,飞机去比赛的对象 - >创建其他 - >平面。这将增加飞机在0,0,0。
选择在现场或飞机从层次结构,并设置旋转:90,0,0。应恰到好处的主摄像头,我们在上一步设置的默认位置(0,0,0)和规模(1,1,1)。
默认情况下,您的新飞机将网撞机组件(矿并至少)。您需要将其删除,使脚本只能与多维数据集,并与背景不发生碰撞。选择飞机,然后在督察小的“齿轮”图标旁边的网撞机组件,并从下拉列表中选择“删除组件”。
下载这个默认的网格纹理,并把它在您TUT1 TouchAnim 资产纹理目录。
团结,选择从该项目的纹理文件夹中的defGrid质地和拖和拖放到平面上,在现场查看。如果你在前面的步骤调整的主摄像头的位置和平面旋转,正确的格子图案应伏贴在游戏中查看。
15。添加的一幕:一盏灯,要获得一个很好的明亮的场景渲染,添加光去比赛的对象 - >创建其他 - >定向光。 0,0,0,这将创建一个新的定向光。光的主摄像头附近的某处移动和旋转,以便它在立方体点。我的位置设置为0,0,15和旋转设置为0,180,0。
定向光的默认灯光设置都应该做工精细,除了改变渲染模式,以“队顶点”。我不知道还什么,但如何匹配的iPhone游戏都有自己的定向光集。
16。设置的iPhone捆绑标识符:我还是有点困惑,这是怎样工作的,因为我认为它应该以某种方式与您的应用程序ID,你在iPhone开发者门户网站的设置。但我设置我的捆绑标识符*(星号),所以我以为我进入Unity3D的... ...但是,这并不为我工作。
团结奋斗,去编辑 - >项目设置 - >播放器播放器设置显示在督察。到iPhone捆绑标识符旁边,你应该看到“com.Company.ProductName”。我这com.bleepng.touchanim这为我的作品。您可能需要发挥各地就在你身边,让它正常工作。
17。发挥团结:我们就大功告成了。如果按“播放”按钮在您的游戏视图,你应该可以看到动画播放前进和后退的立方体点击。注意:动画只播放如果你点击直接对多维数据集,背景平面上。
注意,如果你想播放该动画,当你点击屏幕上的任何地方,只需从脚本中删除(Physics.Raycast(射线,命中,100))线。网撞机组件或添加到您的背景平面。
18。构建和运行在您的iPhone设置Xcode和配置你的iPhone运行的应用程序,超出了本教程的范围,所以我将假定你已经做了所有这一切已经的。
在你的iPhone启动Xcode和插件。一定要解开您的手机或Xcode中可能会给你警告,当它试图安装和启动手机上的应用程序。
团结,转到文件 - >构建与运行“,然后单击”生成游戏画面上的构建和运行“按钮。在储存播放器窗口中,键入touchanim,然后单击保存。
一旦Unity3D完成建设游戏时,Xcode将编译所有文件,它安装在您的iPhone,并自动启动(如果你的iPhone是没有锁定)。
结论:
当你全部完成后,Unity3D项目看起来应该像我的设置,在这个截图(点击图片以查看全尺寸)。
希望你有尽可能多的乐趣,下面这个教程,因为我写它。随意离开就如何改进本教程中,或如果您发现任何错误,或者如果您有任何疑问的意见。另外,请让我知道,如果你知道有任何其他的教程中,我们应该看看。 |
|