1.调整节点相对位置
其中hourTrans,minuteTrans,secTrans为空节点
hour,minute,sec为一个quad.
调整好相对位置,可以使得指针绕端点旋转的效果
2.脚本挂载在clock节点上,为空节点
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Clock : MonoBehaviour
{
private const float hours2Degree = 360f / 12f;
private const float minute2Degree = 360f / 60f;
private const float sec2Degree = 360f / 60f;
public Transform hour;
public Transform minute;
public Transform sec;
public bool useTimeSpan = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (useTimeSpan)
{
TimeSpan ts = DateTime.Now.TimeOfDay;
hour.localRotation = Quaternion.Euler(0f,0f,(float)ts.TotalHours * -hours2Degree);
minute.localRotation = Quaternion.Euler(0f,0f,(float)ts.TotalMinutes * -minute2Degree);
sec.localRotation = Quaternion.Euler(0f,0f,(float)ts.TotalSeconds * -sec2Degree);
}
else
{
DateTime time = DateTime.Now;
hour.localRotation = Quaternion.Euler(0f,0f,time.Hour * -hours2Degree);
minute.localRotation = Quaternion.Euler(0f,0f,time.Minute * -minute2Degree);
sec.localRotation = Quaternion.Euler(0f,0f,time.Second * -sec2Degree);
}
}
}
3.裁剪像素的shader,PS用不熟,效果不好。
shader代码
Shader "Custom/AlphaTex"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_MaskTex ("Texture",2D) = "white"{}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
sampler2D _MaskTex;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 mask = tex2D(_MaskTex,i.uv);
if(mask.r < 1e-4){
discard;
}
return col;
}
ENDCG
}
}
}
贴图和遮罩
效果如下: