8
8
* usage: I used the prompt "我想要基于unity制作一个赛车小游戏,现在我要实现时间变化脚本,你能帮我写一下控制脚本吗", and
9
9
* directly copy the code from its response
10
10
*/
11
+ /// <summary>
12
+ /// Manages the day-night cycle in the game, controlling sun and moon lighting based on time and weather.
13
+ /// </summary>
11
14
public class DayNightCycle : MonoBehaviour
12
15
{
13
- [ Header ( "时间设置" ) ]
14
- public float dayLengthInMinutes = 1f ; // 一天的长度(1分钟 = 一天)
15
- private float timeOfDay = 0f ; // 范围 0.0~1.0,表示一天的进度
16
+ /// <summary>
17
+ /// The length of a full day in minutes.
18
+ /// </summary>
19
+ public float dayLengthInMinutes = 1f ;
16
20
17
- [ Header ( "太阳设置" ) ]
21
+ /// <summary>
22
+ /// The current progress of the day, ranging from 0.0 to 1.0.
23
+ /// </summary>
24
+ private float timeOfDay = 0f ;
25
+
26
+ /// <summary>
27
+ /// The light representing the sun.
28
+ /// </summary>
18
29
public Light sun ;
30
+
31
+ /// <summary>
32
+ /// Animation curve controlling the sun's light intensity over time.
33
+ /// </summary>
19
34
public AnimationCurve lightIntensity ;
20
35
21
- [ Header ( "月亮设置" ) ]
36
+ /// <summary>
37
+ /// The light representing the moon.
38
+ /// </summary>
22
39
public Light moon ;
23
- public AnimationCurve moonIntensity ; // 控制月亮亮度随时间变化
24
- public Gradient moonColor ; // 控制月亮颜色随时间变化
25
40
26
- [ Header ( "天气影响强度" ) ]
41
+ /// <summary>
42
+ /// Animation curve controlling the moon's light intensity over time.
43
+ /// </summary>
44
+ public AnimationCurve moonIntensity ;
45
+
46
+ /// <summary>
47
+ /// Gradient controlling the moon's color changes over time.
48
+ /// </summary>
49
+ public Gradient moonColor ;
50
+
51
+ /// <summary>
52
+ /// Reference to the weather system affecting light intensity.
53
+ /// </summary>
27
54
public WeatherSystem weatherSystem ;
55
+
56
+ /// <summary>
57
+ /// Maximum light intensity during clear weather.
58
+ /// </summary>
28
59
public float maxIntensityClear = 1.2f ;
60
+
61
+ /// <summary>
62
+ /// Maximum light intensity during rainy weather.
63
+ /// </summary>
29
64
public float maxIntensityRain = 0.6f ;
30
- public float maxIntensityFog = 0.4f ;
31
65
32
- [ Header ( "不同天气的光照颜色渐变" ) ]
66
+ /// <summary>
67
+ /// Maximum light intensity during foggy weather.
68
+ /// </summary>
69
+ public float maxIntensityFog = 0.4f ;
70
+ /// <summary>
71
+ /// Gradient for light color during clear weather.
72
+ /// </summary>
33
73
public Gradient clearColor ;
74
+
75
+ /// <summary>
76
+ /// Gradient for light color during rainy weather.
77
+ /// </summary>
34
78
public Gradient rainColor ;
79
+
80
+ /// <summary>
81
+ /// Gradient for light color during foggy weather.
82
+ /// </summary>
35
83
public Gradient fogColor ;
36
84
85
+ /// <summary>
86
+ /// Initializes the day-night cycle, setting up default gradients for moon and weather-based lighting.
87
+ /// </summary>
37
88
void Start ( )
38
89
{
39
90
// 设置默认月亮颜色渐变(夜晚蓝白色)
@@ -52,61 +103,64 @@ void Start()
52
103
}
53
104
) ;
54
105
55
- // ?? Clear�����죩��ɫ����
106
+ // 设置Clear(晴天)颜色渐变
56
107
clearColor = new Gradient ( ) ;
57
108
clearColor . SetKeys (
58
109
new GradientColorKey [ ]
59
110
{
60
- new GradientColorKey ( new Color ( 15f / 255f , 20f / 255f , 50f / 255f ) , 0.0f ) ,
61
- new GradientColorKey ( new Color ( 255f / 255f , 140f / 255f , 60f / 255f ) , 0.25f ) ,
62
- new GradientColorKey ( new Color ( 255f / 255f , 255f / 255f , 240f / 255f ) , 0.5f ) ,
63
- new GradientColorKey ( new Color ( 255f / 255f , 120f / 255f , 80f / 255f ) , 0.75f ) ,
64
- new GradientColorKey ( new Color ( 15f / 255f , 20f / 255f , 50f / 255f ) , 1.0f )
111
+ new GradientColorKey ( new Color ( 15f / 255f , 20f / 255f , 50f / 255f ) , 0.0f ) ,
112
+ new GradientColorKey ( new Color ( 255f / 255f , 140f / 255f , 60f / 255f ) , 0.25f ) ,
113
+ new GradientColorKey ( new Color ( 255f / 255f , 255f / 255f , 240f / 255f ) , 0.5f ) ,
114
+ new GradientColorKey ( new Color ( 255f / 255f , 120f / 255f , 80f / 255f ) , 0.75f ) ,
115
+ new GradientColorKey ( new Color ( 15f / 255f , 20f / 255f , 50f / 255f ) , 1.0f )
65
116
} ,
66
117
new GradientAlphaKey [ ]
67
118
{
68
- new GradientAlphaKey ( 1.0f , 0.0f ) ,
69
- new GradientAlphaKey ( 1.0f , 1.0f )
119
+ new GradientAlphaKey ( 1.0f , 0.0f ) ,
120
+ new GradientAlphaKey ( 1.0f , 1.0f )
70
121
}
71
122
) ;
72
123
73
- // ??? Rain�����죩��ɫ����
124
+ // 设置Rain(雨天)颜色渐变
74
125
rainColor = new Gradient ( ) ;
75
126
rainColor . SetKeys (
76
127
new GradientColorKey [ ]
77
128
{
78
- new GradientColorKey ( new Color ( 115f / 255f , 120f / 255f , 133f / 255f ) , 0.0f ) ,
79
- new GradientColorKey ( new Color ( 107f / 255f , 112f / 255f , 128f / 255f ) , 0.25f ) ,
80
- new GradientColorKey ( new Color ( 102f / 255f , 107f / 255f , 125f / 255f ) , 0.5f ) ,
81
- new GradientColorKey ( new Color ( 110f / 255f , 115f / 255f , 130f / 255f ) , 0.75f ) ,
82
- new GradientColorKey ( new Color ( 120f / 255f , 122f / 255f , 135f / 255f ) , 1.0f )
129
+ new GradientColorKey ( new Color ( 115f / 255f , 120f / 255f , 133f / 255f ) , 0.0f ) ,
130
+ new GradientColorKey ( new Color ( 107f / 255f , 112f / 255f , 128f / 255f ) , 0.25f ) ,
131
+ new GradientColorKey ( new Color ( 102f / 255f , 107f / 255f , 125f / 255f ) , 0.5f ) ,
132
+ new GradientColorKey ( new Color ( 110f / 255f , 115f / 255f , 130f / 255f ) , 0.75f ) ,
133
+ new GradientColorKey ( new Color ( 120f / 255f , 122f / 255f , 135f / 255f ) , 1.0f )
83
134
} ,
84
135
new GradientAlphaKey [ ]
85
136
{
86
- new GradientAlphaKey ( 1.0f , 0.0f ) ,
87
- new GradientAlphaKey ( 1.0f , 1.0f )
137
+ new GradientAlphaKey ( 1.0f , 0.0f ) ,
138
+ new GradientAlphaKey ( 1.0f , 1.0f )
88
139
}
89
140
) ;
90
141
91
- // ??? Fog�����죩��ɫ����
142
+ // 设置Fog(雾天)颜色渐变
92
143
fogColor = new Gradient ( ) ;
93
144
fogColor . SetKeys (
94
145
new GradientColorKey [ ]
95
146
{
96
- new GradientColorKey ( new Color ( 191f / 255f , 191f / 255f , 191f / 255f ) , 0.0f ) ,
97
- new GradientColorKey ( new Color ( 204f / 255f , 204f / 255f , 204f / 255f ) , 0.25f ) ,
98
- new GradientColorKey ( new Color ( 217f / 255f , 217f / 255f , 217f / 255f ) , 0.5f ) ,
99
- new GradientColorKey ( new Color ( 209f / 255f , 209f / 255f , 209f / 255f ) , 0.75f ) ,
100
- new GradientColorKey ( new Color ( 199f / 255f , 199f / 255f , 199f / 255f ) , 1.0f )
147
+ new GradientColorKey ( new Color ( 191f / 255f , 191f / 255f , 191f / 255f ) , 0.0f ) ,
148
+ new GradientColorKey ( new Color ( 204f / 255f , 204f / 255f , 204f / 255f ) , 0.25f ) ,
149
+ new GradientColorKey ( new Color ( 217f / 255f , 217f / 255f , 217f / 255f ) , 0.5f ) ,
150
+ new GradientColorKey ( new Color ( 209f / 255f , 209f / 255f , 209f / 255f ) , 0.75f ) ,
151
+ new GradientColorKey ( new Color ( 199f / 255f , 199f / 255f , 199f / 255f ) , 1.0f )
101
152
} ,
102
153
new GradientAlphaKey [ ]
103
154
{
104
- new GradientAlphaKey ( 1.0f , 0.0f ) ,
105
- new GradientAlphaKey ( 1.0f , 1.0f )
155
+ new GradientAlphaKey ( 1.0f , 0.0f ) ,
156
+ new GradientAlphaKey ( 1.0f , 1.0f )
106
157
}
107
158
) ;
108
159
}
109
160
161
+ /// <summary>
162
+ /// Updates the day-night cycle, adjusting sun and moon positions, colors, and intensities based on time and weather.
163
+ /// </summary>
110
164
void Update ( )
111
165
{
112
166
// 更新时间
@@ -121,7 +175,7 @@ void Update()
121
175
float moonAngle = sunAngle + 180f ;
122
176
moon . transform . rotation = Quaternion . Euler ( moonAngle , - 90f , 10f ) ;
123
177
124
- // ���ù�����ɫ
178
+ // 设置光照颜色
125
179
Gradient currentGradient = clearColor ;
126
180
if ( weatherSystem != null )
127
181
{
@@ -133,7 +187,7 @@ void Update()
133
187
}
134
188
sun . color = currentGradient . Evaluate ( timeOfDay ) ;
135
189
136
- // ���ù���ǿ��
190
+ // 设置光照强度
137
191
float weatherMultiplier = 1f ;
138
192
if ( weatherSystem != null )
139
193
{
@@ -160,4 +214,4 @@ void Update()
160
214
}
161
215
moon . intensity = moonIntensity . Evaluate ( timeOfDay ) * moonMultiplier * 0.4f ;
162
216
}
163
- }
217
+ }
0 commit comments