- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
简洁明了的教程,主要讲解了层级物体的使用,脚本的使用,命名空间,帧刷新函数以及随时间旋转的实现。下面是原文:
In this tutorial we'll write a small C# script to animate the arms of a very simple clock. You'll learn to
create an object hierarchy;
create a script and attach it to an object;
access namespaces;
use the Update method;
rotate stuff based on time.
You're assumed to already have a basic understanding of Unity's editor. If you've played with it for a few minutes you're good to go.
Creating the clock
We start by creating a new Unity project without any packages. The default scene contains a camera positioned at (0, 1, -10) looking down the Z axis. To get a similar perspective as the camera in the scene view, select the camera and perform GameObject / Align View to Selected from the menu.
We need an object structure to represent the clock. Create a new empty GameObject via GameObject / Create Empty and call it Clock. Create three empty child objects for it and call them Hours, Minutes, and Seconds. Make sure they are all positioned at (0, 0, 0).
We'll use simple boxes to visualize the arms of the clock. Create a child cube for each arm via GameObject / Create Other / Cube. Give the cube for Hours position (0, 1, 0) and scale (0.5, 2, 0.5). For the minutes cube it's position (0, 1.5, 0) and scale (0.25, 3, 0.25). For seconds cube it's (0, 2, 0) and (0.1, 4, 0.1).
Animating the clock
We need a script to animate the clock. Create a new C# script via Create / C# Script in the Project view and name it ClockAnimator. Open the script and empty it so we can start fresh.
First, we indicate that we want to use stuff from the UnityEngine namespace. Then we declare the existence of the ClockAnimator. We describe it as a publicly available class that inherits from MonoBehaviour.
This gives us a minimal class that can be used to create components. Save it, then attach it to the the Clock object by dragging from the Project to the Hierarchy view.
using UnityEngine;
public class ClockAnimator : MonoBehaviour {
}
To animate the arms, we need access to their Transform components first. Add a public Transform variable for each arm to the script, then save it. These public variables will become component properties which you can assign object to in the editor. The editor will then grab the Transform components of these objects and assign them to our variables. Select the Clock object, then drag the corresponding objects to the new properties.
using UnityEngine;
public class ClockAnimator : MonoBehaviour {
public Transform hours, minutes, seconds;
}
Next, we'll add an update method to the script. This is a special method that will be called once every frame. We'll use it to set the rotation of the clock arms.
After saving the script, the editor will notice that our component has an update method and will show a checkbox that allows us to disable it. Of course we keep it enabled.
using UnityEngine;
public class ClockAnimator : MonoBehaviour {
public Transform hours, minutes, seconds;
void Update() {
// currently do nothing
}
}
转自:http://catlikecoding.com/unity/tutorials/clock/ |
|