找回密码
 立即注册
查看: 6776|回复: 95

[实例] 用cube创建一个时钟效果

[复制链接]
发表于 2012-12-25 22:39 | 显示全部楼层 |阅读模式
资源信息 Tutorial Information
教程名称: 用cube创建一个时钟效果(发帖教程)
适用引擎:   (适用引擎,为空默认为Unity)
教程语种: English
教程等级: 1
教程格式: 图文(请用IE9以上浏览器访问本版块)
教程作者: 转载自互联网 (如有问题请短消息联系作者或发表回复)
下载地址: (兑换积分)
点击查看原图
美丽分割线

开始创建时钟:

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).

添加指针动画:

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

}

}

Each hour, the Hours arm has to rotate 360/12 degrees. The Minutes arm has to rotate 360/60 degrees per minute. Finally, the Seconds arm has to rotate 360/60 degrees every second. Let's define these values as private constant floating-point values for convenience.

代码:

using UnityEngine;

public class ClockAnimator : MonoBehaviour {

private const float

hoursToDegrees = 360 / 12,

minutesToDegrees = 360f / 60f,

secondsToDegrees = 360f / 60f;

public Transform hours, minutes, seconds;

void Update() {

// currently do nothing

}

}

Each update we need to know the current time to get this thing to work. The System namespace contains the DateTime struct, which is suited for this job. It has a property called Now that always corresponds with the current time. Each update we need to grab it and store it in a temporary variable

代码:

using UnityEngine;

using System;

public class ClockAnimator : MonoBehaviour {

private const float

hoursToDegrees = 360 / 12,

minutesToDegrees = 360f / 60f,

secondsToDegrees = 360f / 60f;

public Transform hours, minutes, seconds;

void Update() {

DateTime time = DateTime.Now;

}

}

To get the arms to rotate, we need to change their local rotation. We do this by directly setting the localRotation of the arms, using quaternions. Quaternion has a nice method we can use to define an arbitrary rotation.

Because we're looking down the Z axis and Unity uses a lefthanded coordinate system, the rotation must be negative.

代码:

using UnityEngine;

using System;

public class Clock : MonoBehaviour {

private const float

hoursToDegrees = 360 / 12,

minutesToDegrees = 360f / 60f,

secondsToDegrees = 360f / 60f;

public Transform hours, minutes, seconds;

void Update() {

DateTime time = DateTime.Now;

hours.localRotation = Quaternion.Euler(0f, 0f, time.Hour * -hoursToDegrees);

minutes.localRotation = Quaternion.Euler(0f, 0f, time.Minute * -minutesToDegrees);

seconds.localRotation = Quaternion.Euler(0f, 0f, time.Second * -secondsToDegrees);

}

}

Improving the clock

This works! When in play mode, our clock shows the current time. However, it behaves much like a digital clock as it only shows discrete steps. Let's include an option to show analog time as well. Add a public boolean variable analog to the script and use it to determine what to do in the update method. We can toggle this value in the editor, even when in play mode.

代码:

using UnityEngine;

using System;

public class Clock : MonoBehaviour {

private const float

hoursToDegrees = 360 / 12,

minutesToDegrees = 360f / 60f,

secondsToDegrees = 360f / 60f;

public Transform hours, minutes, seconds;

public bool analog;

void Update() {

if (analog) {

// currently do nothing

}

else {

DateTime time = DateTime.Now;

hours.localRotation = Quaternion.Euler(0f, 0f, time.Hour * -hoursToDegrees);

minutes.localRotation = Quaternion.Euler(0f, 0f, time.Minute * -minutesToDegrees);

seconds.localRotation = Quaternion.Euler(0f, 0f, time.Second * -secondsToDegrees);

}

}

}

For the analog option we need a slightly different approach. Instead of DateTime.Now we'll use DateTime.Now.TimeOfDay, which is a TimeSpan. This allows us easy access to the fractional elapsed hours, minutes, and seconds. Because these values are provided as doubles – double precision floating-point values – we need to cast them to floats.

代码:

using UnityEngine;

using System;

public class Clock : MonoBehaviour {

public Transform hours, minutes, seconds;

public bool analog;

void Update() {

if (analog) {

TimeSpan timespan = DateTime.Now.TimeOfDay;

hours.localRotation = Quaternion.Euler(0f, 0f, (float)timespan.TotalHours * -360f / 12f);

minutes.localRotation = Quaternion.Euler(0f, 0f, (float)timespan.TotalMinutes * -360f / 60f);

seconds.localRotation = Quaternion.Euler(0f, 0f, (float)timespan.TotalSeconds * -360f / 60f);

}

else {

DateTime time = DateTime.Now;

hours.localRotation = Quaternion.Euler(0f, 0f, time.Hour * -360f / 12f);

minutes.localRotation = Quaternion.Euler(0f, 0f, time.Minute * -360f / 60f);

seconds.localRotation = Quaternion.Euler(0f, 0f, time.Second * -360f / 60f);

}

}

}

Now our clock works analog too!


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2017-3-28 09:47 | 显示全部楼层
楼主是超人
发表于 2017-3-28 09:48 | 显示全部楼层
好帖就是要顶
发表于 2017-3-28 09:10 | 显示全部楼层
顶顶多好
发表于 2017-3-28 09:25 | 显示全部楼层
真心顶
发表于 2017-3-28 09:28 | 显示全部楼层
难得一见的好帖
发表于 2017-5-3 11:21 | 显示全部楼层
好帖就是要顶
发表于 2017-5-3 11:41 | 显示全部楼层
顶顶多好
发表于 2017-5-3 11:12 | 显示全部楼层
真心顶
发表于 2017-5-3 10:59 | 显示全部楼层
很好哦
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-30 04:06 , Processed in 0.100290 second(s), 31 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表