Unity入門(mén)系列
目標(biāo): 創(chuàng)建一個(gè)時(shí)鐘

1, 建一個(gè)簡(jiǎn)單的時(shí)鐘
1.1 建一個(gè)Game Objec并命名為Clock
菜單: GameObject / Create Empty?

選中,在Inspector窗口查看屬性,確保Position 和 Rotation都為0, Scale為1

1.2 創(chuàng)建時(shí)鐘的盤(pán)面
菜單:GameObject / 3D Object / Cylinder
確保Transform的值和Clock一樣

Capsule Collider可以不要, 刪除它(點(diǎn)擊齒輪,有下拉菜單)

如圖設(shè)置參數(shù),可以看到表盤(pán)的樣子


重命名為“Face“, 并拖到Clock下面, 成為clock的子節(jié)點(diǎn)

子節(jié)點(diǎn)的位置會(huì)跟隨父節(jié)點(diǎn)變化,這個(gè)很有用。
1.3 創(chuàng)建時(shí)鐘的刻度
菜單:GameObject / 3D Object / Cube
設(shè)置scale:??(0.5, 0.2, 1)?
設(shè)置position:??(0, 0.2, 4)?
命名為:?Hour Indicator


接下來(lái)要給刻度附上顏色:
創(chuàng)建一個(gè)Material
重命名為Clock Dark
設(shè)置顏色


將”Clock Dark“拉到”Hour Indicator“ 上面, 或者中”Hour Indicator“的屬性中設(shè)置。
這樣就完成著色。

接下來(lái)要布置12個(gè)刻度, 圓圈是360度, 每個(gè)刻度相距30度, 我們?cè)囋囍苯痈淖僅our Indicator的Rotation值


結(jié)果顯然是不對(duì)的, 這是因?yàn)閷?duì)象的旋轉(zhuǎn)是基于自己的原點(diǎn).
為了使Hour Indicator沿著表盤(pán)Face的原點(diǎn)旋轉(zhuǎn), 我們要利用子節(jié)點(diǎn)的位置會(huì)隨父節(jié)點(diǎn)一起變化的原理.
創(chuàng)建一個(gè)Game Object(create Empty), 并把Hour Indicator變成它的子對(duì)象.

選擇這個(gè)臨時(shí)的父節(jié)點(diǎn)"GameObjec", 復(fù)制多11個(gè)(Control + D)
逐個(gè)設(shè)置Rotatioin的Y值, 每個(gè)加30: (0, 30, 60, 90 ..., 330)

這就是我們想要的效果, 這時(shí)候我們已經(jīng)不需要臨時(shí)的父節(jié)點(diǎn)了,?
把所有Hour Indicator都拉出來(lái)(可以批量), 變成Clock的子節(jié)點(diǎn), 再把這些臨時(shí)GameObjec刪掉..

1.4 創(chuàng)建指針
類(lèi)似的方法,創(chuàng)建一個(gè)指針:
新建一個(gè)Cube, 命名為"Arm"
Set its scale to (0.3, 0.2, 2.5)?
Set its position to (0, 0.2, 0.75)?
附上顏色Clock Dark


為了讓指針繞中心點(diǎn)旋轉(zhuǎn), 給它一個(gè)一個(gè)父節(jié)點(diǎn), 讓父節(jié)點(diǎn)帶它飛
新建一個(gè)Game Object, Position and Rotation 默認(rèn)為0, Scale 默認(rèn)為1
重命名為Hours Arm
擺放為Clock的子節(jié)點(diǎn)
把Arm擺放為Hour Arm的子節(jié)點(diǎn), 也就成為Clock的孫節(jié)點(diǎn).

復(fù)制兩份Hours Arm, 風(fēng)別重命名為Minutes Arm和Seconds Arm
?Minutes Arm 的子節(jié)點(diǎn)?Arm設(shè)置屬性?scale (0.2, 0.15, 4) , position? (0, 0.375, 1).?
?Seconds Arm 的子節(jié)點(diǎn)?Arm設(shè)置屬性??scale (0.1, 0.1, 5) , position (0, 0.5, 1.25).
為秒針創(chuàng)建一個(gè)紅色的Materia, 命名為Clock Red, 設(shè)置lalbedo (197, 0, 0)?


外形構(gòu)建完成, 保存scene

2 讓時(shí)鐘動(dòng)起來(lái)
創(chuàng)建?C# Script?,命名為Clock.

2.1 將這個(gè)腳本設(shè)置為Clock Game Object的一個(gè)component

2.2 控制指針
指針必須根據(jù)時(shí)間旋轉(zhuǎn)一定的角度,為此我們創(chuàng)建一個(gè)變量:
public Transform hoursTransform;
一旦定義為public, 屬性窗口就會(huì)同步顯示(clock game object中)

把Hours Arm跟這個(gè)字段連接起來(lái)(拖拉過(guò)來(lái), 或者點(diǎn)擊圓圈圖標(biāo)做設(shè)置)

2.3 設(shè)置全部三個(gè)指針
public Transform hoursTransform;
public Transform minutesTransform;
public Transform secondsTransform;
連接相應(yīng)的Game Objec

2.4 關(guān)于時(shí)間
我們將通過(guò)Awake函數(shù)開(kāi)始編程
void Awake () {
????????Debug.Log("Test");
????}
點(diǎn)擊"Play", 進(jìn)入play模式, 中console窗口就可以看到結(jié)果.
修改腳本:
using System;
using UnityEngine;
public class Clock : MonoBehaviour {
????public Transform hoursTransform, minutesTransform, secondsTransform;
????void Awake () {
????????Debug.Log(DateTime.Now);
????}
}
再回到play模式,看看效果, 這就是腳本的功效.
2.5 旋轉(zhuǎn)指針
Unity通過(guò) Quaternion.Euler 處理Rotation的問(wèn)題,?
中表盤(pán)中, 一小時(shí)是30度, 一分鐘是6度, 一秒鐘也是6度, clock里面的代碼如下
? ? public Transform hoursTransform;
? ? public Transform minutesTransform;
? ? public Transform secondsTransform;
? ? const float degreesPerHour = 30f;
? ? const float degreesPerMinute = 6f;
? ? const float degreesPerSecond = 6f;
? ? void Awake()
? ? {
? ? ? ? hoursTransform.localRotation = Quaternion.Euler(0f, DateTime.Now.Hour * degreesPerHour, 0f);
? ? ? ? minutesTransform.localRotation = Quaternion.Euler(0f, DateTime.Now.Minute * degreesPerMinute, 0f);
? ? ? ? secondsTransform.localRotation = Quaternion.Euler(0f, DateTime.Now.Second * degreesPerSecond, 0f);
? ? }

2.6 讓指針動(dòng)起來(lái)
現(xiàn)在的時(shí)鐘,在進(jìn)入play模式的時(shí)候獲取時(shí)間, 然后就不動(dòng)了. 為了讓指針動(dòng)起來(lái), 改用Update函數(shù)
將void Awake(), 改為?void Update()
這時(shí), 部件也會(huì)出現(xiàn)一個(gè)可選框, 可以設(shè)置要不要實(shí)時(shí)調(diào)用這個(gè)腳本

保持選中狀態(tài), 進(jìn)入play模式, 可以看到一個(gè)會(huì)動(dòng)的時(shí)鐘.
2.7 不間斷旋轉(zhuǎn)float
現(xiàn)在的時(shí)針,只會(huì)指在正點(diǎn)的位置, 添加一個(gè)功能, 讓指針可以出現(xiàn)在連續(xù)的位置.
添加一個(gè)變量, 讓用戶選擇要連續(xù)轉(zhuǎn), 還是只指向正點(diǎn)
public?bool?continuous;
屬性面板會(huì)有相應(yīng)變化

因?yàn)? DateTime.Now.Hour 只返回整數(shù). DateTime.Now.TimeOfDay.TotalHours 可以返回浮點(diǎn)數(shù)
完整代碼如下:?
using System;
using UnityEngine;
public class Clock : MonoBehaviour
{
? ? public Transform hoursTransform;
? ? public Transform minutesTransform;
? ? public Transform secondsTransform;
? ? const float degreesPerHour = 30f;
? ? const float degreesPerMinute = 6f;
? ? const float degreesPerSecond = 6f;
? ? public bool continuous;
? ? void Update()
? ? {
? ? ? ? if (continuous)
? ? ? ? {
? ? ? ? ? ? UpdateContinuous();
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? UpdateDiscrete();
? ? ? ? }
? ? }
? ? void UpdateContinuous()
? ? {
? ? ? ? TimeSpan time = DateTime.Now.TimeOfDay;
? ? ? ? hoursTransform.localRotation =
? ? ? ? ? ? Quaternion.Euler(0f, (float)time.TotalHours * degreesPerHour, 0f);
? ? ? ? minutesTransform.localRotation =
? ? ? ? ? ? Quaternion.Euler(0f, (float)time.TotalMinutes * degreesPerMinute, 0f);
? ? ? ? secondsTransform.localRotation =
? ? ? ? ? ? Quaternion.Euler(0f, (float)time.TotalSeconds * degreesPerSecond, 0f);
? ? }
? ? void UpdateDiscrete()
? ? {
? ? ? ? DateTime time = DateTime.Now;
? ? ? ? hoursTransform.localRotation =
? ? ? ? ? ? Quaternion.Euler(0f, time.Hour * degreesPerHour, 0f);
? ? ? ? minutesTransform.localRotation =
? ? ? ? ? ? Quaternion.Euler(0f, time.Minute * degreesPerMinute, 0f);
? ? ? ? secondsTransform.localRotation =
? ? ? ? ? ? Quaternion.Euler(0f, time.Second * degreesPerSecond, 0f);
? ? }
}
大功告成!!
原文地址:?http://catlikecoding.com/unity/tutorials/basics/game-objects-and-scripts/
沒(méi)能完成的可以過(guò)去下載.