效果图:
工程里需要有个Clock的空物体,脚本挂上去,物体下有时,分,秒3个空物体,三个空物体下分别放时分秒的指针(游戏物体,位置自己调整下),然后把时分秒的空物体拖到脚本对应的位置。
位置参考:
时钟的全部物件归0后:
小时坐标:0,0.75,0 Scale:0.1,1.5,0.1
分坐标:01,0 Scale:0.1,2,0.1
秒坐标:01.5,0 Scale:0.1,3,0.1
using System.Collections;
using System.Collections.Generic;using UnityEngine;
using System;
public class Clock : MonoBehaviour {
DateTime time;
public Transform hourTransform, minuteTransform, secondTransform;
private float degreesInHour, degreesInMinute, degreesInSecond;
// Use this for initialization
void Start () {
degreesInHour = 360 / 12; // rotate 360/12 in hour. //一圈12小时,则每小时要转360/12多个角度
degreesInMinute = 360 / 60; // rotate 360/60 in minute.
degreesInSecond = 360 / 60; // rotate 360/60 in second.
}
// Update is called once per frame
void FixedUpdate () {
DateTime time = DateTime.Now;
hourTransform.localRotation = Quaternion.Euler(0f, 0f, time.Hour * -degreesInHour);
minuteTransform.localRotation = Quaternion.Euler(0f, 0f, time.Minute * -degreesInMinute);
secondTransform.localRotation = Quaternion.Euler(0f, 0f, time.Second * -degreesInSecond);
}