一个简单的时钟演示
1:画好时间表盘,时,分,秒 指针, 注意要三个指针统一父级空物体的坐标点。然后调用指针大小位置材质对好~ 后面的方法是以Y轴旋转的,最好以地面俯视角度画物体。演示的 ObjClock里除了三指针外其它层物体都是跑龙套的,参考可忽略~
2:给父级拖上写好的代码 ,把对应的三指针拖上去,就可~ (没啥技术含量,只供新人学习)
3:运行效果。
4:码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// author:mythlong
/// 这是一个模拟时钟表盘旋转的方法
/// </summary>
public class MythlClock : MonoBehaviour
{
public Transform hourObj;
public Transform minuteObj;
public Transform secondObj;
private float hour;
private float minute;
private float second;
private DateTime nowTime;
// Start is called before the first frame update
void Start()
{
nowTime = DateTime.Now;
hour = nowTime.Hour;
minute = nowTime.Minute;
second = nowTime.Second;
secondObj.rotation = Quaternion.AngleAxis(second * 6, Vector3.up);
}
// Update is called once per frame
void Update()
{
nowTime = DateTime.Now;
hour = nowTime.Hour;
minute = nowTime.Minute;
second = nowTime.Second;
//Debug.Log("time:" + hour + " : " + minute + " : " + second);
Quaternion tmpSec = Quaternion.AngleAxis(second * 6, Vector3.up);
//secondObj.rotation = Quaternion.Lerp(secondObj.rotation, tmpSec, Time.deltaTime);
secondObj.rotation = Quaternion.RotateTowards(secondObj.rotation, tmpSec, Time.deltaTime*6);
minuteObj.rotation = Quaternion.AngleAxis(minute*6+ second * 6/60, Vector3.up);
hourObj.rotation = Quaternion.AngleAxis(hour*30+minute*6/12, Vector3.up);
}
}