查看: 1587|回复: 8
打印 上一主题 下一主题

使用C#创建一个时钟效果

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-11-3 14:48:00 |只看该作者 |倒序浏览


           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.
           






           开始创建时钟:
         

           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!
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-1-19 22:58:47 |只看该作者
林志玲,娇滴滴,祝你新年一定要好好的啦!小沈阳,笑嘻嘻,祝你新年一定要杠杠的哈!春晚群星,闹哄哄的,祝你新年辞旧迎新开创新纪元!
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-1-20 23:28:37 |只看该作者
今年过年不收礼,收礼只收你短信,祝福不分大小,只要真心我就要。条数越多我越高兴,手机越响我越开心,你可否提前把礼送,等礼等得我好心焦。
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-4-6 23:27:23 |只看该作者
你们都躲开,我来顶
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-4-12 23:25:27 |只看该作者
好可爱的字,学习了
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

6#
发表于 2012-4-24 08:05:26 |只看该作者
很经典,很实用,学习了!
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-5-12 23:20:56 |只看该作者
已阵亡的 蝶 随 风 舞 说过  偶尔按一下 CTRL A 会发现 世界还有另一面
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2012-5-16 23:22:03 |只看该作者
不错哦,顶一下......
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

9#
发表于 2012-9-25 23:27:37 |只看该作者
无聊时可以刷屏幕 灌水 也可以试试 帖子的标题究竟可以写多长
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-21 10:35 , Processed in 0.093566 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部