纳金网

标题: untiy3d iphone简单动画开发教程(二) [打印本页]

作者: 会飞的鱼    时间: 2011-12-16 17:40
标题: untiy3d iphone简单动画开发教程(二)
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).
作者: 晃晃    时间: 2012-1-26 23:24
新年的风,吹散你的阴霾;新年的雨,冲刷你的无奈;新年的雾,锁定你的失败;新年的日,照耀你的期待。新年到来,幸福怀揣,新年快乐,响彻天外!

作者: 晃晃    时间: 2012-3-13 23:20
你们都躲开,我来顶

作者: 彬彬    时间: 2012-3-16 19:43

   

作者: 彬彬    时间: 2012-3-16 19:43

   

作者: C.R.CAN    时间: 2012-3-19 23:27
无聊时可以刷屏幕 灌水 也可以试试 帖子的标题究竟可以写多长

作者: 晃晃    时间: 2012-4-20 23:19
此地無銀。。。

作者: 奇    时间: 2012-4-24 08:09
你们都躲开,我来顶

作者: tc    时间: 2012-7-20 23:25
你们都躲开,我来顶

作者: 菜刀吻电线    时间: 2012-8-13 09:33
我看看就走,你们聊!

作者: C.R.CAN    时间: 2012-8-13 23:58
有意思!学习了!

作者: 晃晃    时间: 2012-9-3 01:25
好`我顶``顶顶

作者: 菜刀吻电线    时间: 2012-10-6 23:24
我是老实人,我来也!





欢迎光临 纳金网 (http://rs.narkii.com/club/) Powered by Discuz! X2.5