Unity3D应用之时钟与钟表小组件的使用教程
作者:恬静的小魔龙 时间:2023-05-15 04:43:35
一、前言
又见面了哈,今天为大家介绍时钟、钟表的实现方法教程。
实现的方法有很多,这里只是提供了一个思路,本着抛砖引玉的心态,希望能和大家共同学习。
二、效果图及源工程下载
效果图:
源工程下载:
https://pan.baidu.com/s/1x0AwvjoUkR5VBC8xnSQw7Q 提取码:mhns
三、实现
3-1 场景搭建
使用Cylinder搭建一个时钟面,然后使用TextMesh设置小时数,最后使用长短不一的Cube做成时针、分针、秒针。
在这里需要说明的第一点是,需要将时针、分针、秒针拖到对应的父物体下,做成预制体,因为代码是使用Quaternion.AngleAxis函数进行绕轴旋转,所以直接旋转针的话,就变成自转了,所以需要旋转它的父物体,这样子物体就跟着转了,将父物体的坐标设置为0,0,0,那么子物体就是以中心点旋转了。
搭建后如下图所示:
3-2 代码实现
using System;
using UnityEngine;
public class Clock : MonoBehaviour
{
private GameObject HourHands;//时针
private GameObject MinuteHand;//分针
private GameObject SecondHand;//秒针
void Start()
{
HourHands = GameObject.Find("时钟/时针");
MinuteHand = GameObject.Find("时钟/分针");
SecondHand = GameObject.Find("时钟/秒针");
}
void Update()
{
TimeSpan time = DateTime.Now.TimeOfDay;
HourHands.transform.localRotation = Quaternion.AngleAxis(30 * time.Hours, Vector3.up);
MinuteHand.transform.localRotation = Quaternion.AngleAxis(6 * time.Minutes, Vector3.up);
SecondHand.transform.localRotation= Quaternion.AngleAxis(6 * time.Seconds, Vector3.up);
}
}
没错,就是这几行代码就实现了:
四、后言
代码依旧延续简约风格,不多写一行代码
整体思路也很简单,就是获取到当前时间,然后让时针分针秒针转到指定的角度即可。
来源:https://blog.csdn.net/q764424567/article/details/117330118
标签:Unity3D,时钟
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# 通过ServiceStack 操作Redis
2023-12-13 06:18:01
![](https://img.aspxhome.com/file/2023/2/80022_0s.png)
详解Spring Boot Security工作流程
2023-12-17 12:23:52
![](https://img.aspxhome.com/file/2023/8/62668_0s.jpg)
Java聊天室之解决连接超时问题
2021-06-07 16:30:54
![](https://img.aspxhome.com/file/2023/5/69115_0s.png)
java课程设计做一个多人聊天室(socket+多线程)
2022-10-31 08:11:54
![](https://img.aspxhome.com/file/2023/8/132088_0s.jpg)
SpringCloud Eureka搭建的方法步骤
2021-10-10 11:03:29
![](https://img.aspxhome.com/file/2023/1/61241_0s.jpg)
深入剖析Java工厂模式让你的代码更灵活
2022-05-26 00:42:28
C# TrieTree介绍及实现方法
2022-02-10 22:04:53
![](https://img.aspxhome.com/file/2023/8/122718_0s.jpg)
Android天气预报之基于HttpGet对象解析天气数据的方法
2021-11-01 08:00:13
java中面向对象的概念及知识点总结
2022-07-14 03:14:01
详细解读java同步之synchronized解析
2022-08-01 15:27:05
详解在spring boot中消息推送系统设计与实现
2023-05-28 04:17:00
![](https://img.aspxhome.com/file/2023/2/74902_0s.jpg)
mybatis如何实现saveOrUpdate
2021-07-18 10:33:11
C# 对文件与文件夹的操作包括删除、移动与复制
2023-11-16 07:20:46
如何利用Android仿微博正文链接交互效果
2022-03-22 03:41:41
![](https://img.aspxhome.com/file/2023/0/137240_0s.gif)
Springboot实现Shiro整合JWT的示例代码
2022-01-30 02:58:32
![](https://img.aspxhome.com/file/2023/1/65601_0s.png)
Android 高德地图之poi搜索功能的实现代码
2022-03-07 01:36:52
![](https://img.aspxhome.com/file/2023/3/94983_0s.gif)
SpringBoot配置Email发送功能实例
2022-03-07 20:52:20
![](https://img.aspxhome.com/file/2023/3/61943_0s.jpg)
Spring如何将bean添加到容器中
2021-08-07 08:48:06
![](https://img.aspxhome.com/file/2023/7/108187_0s.jpg)
C# using的本质及使用详解
2022-10-10 06:11:23
![](https://img.aspxhome.com/file/2023/0/76390_0s.png)
IDEA报错:无效的源发行版解决方案
2022-06-05 08:38:58
![](https://img.aspxhome.com/file/2023/4/62454_0s.png)