Unity Demo ——3D时钟

需求

模拟3D时钟。

需要资源

一个3D时钟模型

值:

现实世界:

小时 :0-12    0-24

分钟: 0-60

秒: 0-60

unity值:

Rotation: 0-360 度。

模型,商店提供的还不错,中心点也提前做好了。

手动改下命名: hour minute second

模型截图:

代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Clock : MonoBehaviour
{
    public GameObject hours;
    public GameObject minutes;
    public GameObject seconds;
    public bool isControll;
    [Range(0,24)]
    public int hour;
    [Range(0,60)]
    public int minute;
    [Range(0, 60)]
    public int second;
  
    void Update()
    {
        //物体 0 - 360 度
        //时间 0 - 60


        //示例  
        // 秒 
        // 5 30
        // 10 60
        // 15 90
        // 30 180

        //分 同理 

        //时 0 -24
        //这个时针有点特殊

        //分针转90°  时针转 7.5°
        //分针转180° 时针转15度
        if (isControll)
        {
            hours.transform.eulerAngles = new Vector3(0, 0, (hour - 12) * 30 + (minute / 2));
            minutes.transform.eulerAngles = new Vector3(0, 0, minute * 6 + second / 10);
            seconds.transform.eulerAngles = new Vector3(0, 0, second * 6);
        }
        else
        {
            hour = System.DateTime.Now.Hour;
            minute = System.DateTime.Now.Minute;
            second = System.DateTime.Now.Second;
             
            hours.transform.eulerAngles = new Vector3(0, 0, (hour - 12) * 30 + (minute /2));
            minutes.transform.eulerAngles = new Vector3(0, 0, minute * 6 + second / 10);
            seconds.transform.eulerAngles = new Vector3(0, 0, second * 6);
        }
       
    }
}

效果:

勾选IsControll 可以手动旋转每个指针。

UnityPackage包,稍后发在CSND资源包中。去查看我的上传或者群内滴滴我。

发布了57 篇原创文章 · 获赞 37 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_35030499/article/details/104207136
今日推荐