纳金网

标题: 使用C#创建一个简单的时钟(一) [打印本页]

作者: 会飞的鱼    时间: 2011-12-14 14:33
标题: 使用C#创建一个简单的时钟(一)
简洁明了的教程,主要讲解了层级物体的使用,脚本的使用,命名空间,帧刷新函数以及随时间旋转的实现。下面是原文:

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/
作者: 晃晃    时间: 2012-2-18 23:18
心中有爱,爱咋咋地

作者: tc    时间: 2012-2-22 23:19
好`我顶``顶顶

作者: 奇    时间: 2012-4-22 23:22
提醒猪猪,千万不能让你看见

作者: 晃晃    时间: 2012-4-24 23:31
凡系斑竹滴话要听;凡系朋友滴帖要顶!

作者: 菜刀吻电线    时间: 2012-4-29 23:19
呵呵,很漂亮啊

作者: 奇    时间: 2012-5-11 23:27
楼主收集的可真全哦

作者: 奇    时间: 2012-6-3 23:23
凡系斑竹滴话要听;凡系朋友滴帖要顶!

作者: 晃晃    时间: 2012-6-10 23:24
不错 非常经典  实用

作者: tc    时间: 2012-7-1 23:26
很有心,部分已收录自用,谢谢

作者: C.R.CAN    时间: 2012-8-11 23:49
楼主收集的可真全哦

作者: 晃晃    时间: 2012-8-18 23:47
心中有爱,爱咋咋地

作者: 晃晃    时间: 2012-10-27 23:27
再看一看,再顶楼主

作者: 菜刀吻电线    时间: 2013-2-7 00:21
水……生命之源……灌……





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