Unity3D动态天空之UniSky

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Unity3D动态天空之UniSky
找⼯作时⼀家公司时给我出的题⽬,去uniSky官⽹看了看快速⼊门就做了出来,时间⾃动流逝,并且可以⼿动设置时间段以及相应的天⽓效果,直接上代码,要.package的留邮箱啊,我在博客园找不到上传的地⽅
1///<summary>
2///天⽓状态
3///</summary>
4public enum WeatherState
5 {
6///<summary>
7///晴天
8///</summary>
9 SUNNY_DAY,
10
11///<summary>
12///⾬天
13///</summary>
14 RAINY_DAY,
15
16///<summary>
17///雪天
18///</summary>
19 SNOWY_DAY,
20
21///<summary>
22///雾天
23///</summary>
24 FOGGY_DAY,
25
26///<summary>
27///阴天
28///</summary>
29 CLOUD_DAY
30 }
天⽓状态
1///<summary>
2///时间状态
3///</summary>
4public enum TimeState
5 {
6///<summary>
7///早晨
8///</summary>
9 MORNING,
10
11///<summary>
12///上午
13///</summary>
14 AM,
15
16///<summary>
17///中午
18///</summary>
19 NOONING,
20
21///<summary>
22///下午
23///</summary>
24 PM,
25
26///<summary>
27///傍晚
28///</summary>
29 DUSK,
30
31///<summary>
32///夜晚
33///</summary>
34 NIGHT
35 }
时间状态
1using UnityEngine;
2using System.Collections;
3
4public class SkyController : MonoBehaviour
6//导⼊UniSky插件
7private UniSkyAPI uniSky;
8
9//存储当前时间状态
10private TimeState nowTime;
11//存储当前天⽓状态
12private WeatherState nowWeather;
13
14//时间状态是否更改
15private bool changeTime = false;
16//天⽓状态是否更改
17private bool changeWeather = false;
18
19//时间流逝速度
20public float timeSpeed = 0.0F;
21
22void Start ()
23 {
24//初始化UniSky插件
25 uniSky = GameObject.Find("UniSkyAPI").GetComponent("UniSkyAPI") as UniSkyAPI;
26 uniSky.InstantiateUniSky();
27//初始化时间状态(上午)和天⽓状态(晴天)
28 nowWeather = WeatherState.SUNNY_DAY;
29 nowTime = TimeState.AM;
30//防⽌bug天空
31 uniSky.SetStormCloudCover(-3.5F);
32 uniSky.LerpStormCloudCover(-3.5F, -3.5F);
33
34//去掉⿏标
35 Screen.showCursor = false;
36 }
37
38void Update()
39 {
40//根据键盘输⼊改变时间状态和天⽓状态
41 ChangeTimeAndWeather();
42//根据时间状态和天⽓状态绘制相关效果
43 RealizeTimeAndWeather();
44
45//如果没有强⾏改变时间,则时间正常流逝,反之则强⾏改变时间的这⼀帧时间不流逝
46if (changeTime)
47 changeTime = false;
48else
49 ChanageTime();
50
51if (changeWeather)
52 {
53 ResetSky();
54 changeWeather = false;
55 }
56
57//输出当前的时间和天⽓
58 Debug.Log(nowTime + "," + nowWeather + "," + changeWeather);
59 }
60
61void OnGUI()
62 {
63 bel(new Rect(0, 0, 100, 20), "操作说明:");
64
65 bel(new Rect(0, 50, 500, 20), "F1-F6功能键切换时间:F1->早晨 F2->上午 F3->中午 F4->下午 F5->黄昏 F6->晚上");
66 bel(new Rect(0, 80, 500, 20), "1-5数字键切换天⽓: 1->晴天 2 ->⾬天 3->⼤风 4->雾天 5->阴天");
67
68 bel(new Rect(0, 130, 200, 20), "W->⼈物向前");
69 bel(new Rect(0, 160, 200, 20), "S->⼈物向后");
70 bel(new Rect(0, 190, 200, 20), "A->⼈物向左");
71 bel(new Rect(0, 220, 200, 20), "D->⼈物向右");
72 bel(new Rect(0, 250, 200, 20), "空格->跳跃");
73 bel(new Rect(0, 280, 200, 20), "⿏标->旋转");
74 }
75
76///<summary>
77///时间⾃动流逝
78///</summary>
79void ChanageTime()
80 {
81 uniSky.SetTime(uniSky.GetTime() + Time.deltaTime * timeSpeed);
82 }
83
84///<summary>
85///重置天空
86///</summary>
87void ResetSky()
88 {
90 uniSky.SetCloudCover(-2);
91 uniSky.LerpCloudCover(-2, -2);
92//云⽩
93 uniSky.SetPrecipitationLevel(1);
94 uniSky.LerpPrecipitationLevel(1, 1);
95//光多
96 uniSky.SetSunIntensity(0.5F);
97 uniSky.LerpSunIntensity(0.5F, 0.5F);
98
99//不打雷
100 uniSky.SetLightningFrequency(0);
101//不下⾬
102 uniSky.SetRainLevel(0, 0);
103 uniSky.LerpRainLevel(0, 0, 0);
104//⾬不滴落下屏幕上
105 uniSky.SetDropletLevel(1);
106 uniSky.LerpDropletLevel(1, 1);
107//没烟雾
108 uniSky.SetFogLevel(0);
109 uniSky.LerpFogLevel(0, 0);
110
111//正常卷云
112 uniSky.SetGlowVariance(10);
113 uniSky.LerpGlowVariance(10, 10);
114
115//清除缓存
116 uniSky.ClearDropletBuffer();
117 }
118
119///<summary>
120///实现时间天⽓效果
121///</summary>
122void RealizeTimeAndWeather()
123 {
124//--------------------------------------------时间 -----------------------------------------125
126//早晨
127if(nowTime == TimeState.MORNING)
128 {
129 uniSky.SetTime(8);
130 }
131
132//上午
133if (nowTime == TimeState.AM)
134 {
135 uniSky.SetTime(10);
136 }
137
138//中午
139if (nowTime == TimeState.NOONING)
140 {
141 uniSky.SetTime(12);
142 }
143
144//下午
145if (nowTime == TimeState.PM)
146 {
147 uniSky.SetTime(14);
148 }
149
150//傍晚
151if (nowTime == TimeState.DUSK)
152 {
153 uniSky.SetTime(16);
154 }
155
156//晚上
157if (nowTime == TimeState.NIGHT)
158 {
159 uniSky.SetTime(20);
160 }
161
162//--------------------------------------------天⽓ -----------------------------------------163
164//晴天
165if (nowWeather == WeatherState.SUNNY_DAY)
166 {
167//云少
168 uniSky.SetCloudCover(-2);
169 uniSky.LerpCloudCover(-2, -2);
170//云⽩
171 uniSky.SetPrecipitationLevel(1);
172 uniSky.LerpPrecipitationLevel(1, 1);
174 uniSky.SetGlowVariance(15);
175 uniSky.LerpGlowVariance(15, 15);
176 }
177
178//⾬天
179if (nowWeather == WeatherState.RAINY_DAY)
180 {
181//云多
182 uniSky.SetCloudCover(-1);
183 uniSky.LerpCloudCover(-1, -1);
184//云⿊
185 uniSky.SetPrecipitationLevel(0.3F);
186 uniSky.LerpPrecipitationLevel(0.3F, 0.3F);
187//光少
188 uniSky.SetSunIntensity(0.2F);
189 uniSky.LerpSunIntensity(0.2F, 0.2F);
190//打雷
191 uniSky.SetLightningFrequency(200);
192//下⾬
193 uniSky.SetRainLevel(200, 200);
194 uniSky.LerpRainLevel(200, 200, 200);
195//⾬在屏幕图像上滴落(0到5)
196 uniSky.SetDropletLevel(100);
197 uniSky.LerpDropletLevel(100, 100);
198//多卷云
199 uniSky.SetGlowVariance(5);
200 uniSky.LerpGlowVariance(5, 5);
201 }
202
203//雪天
204if (nowWeather == WeatherState.SNOWY_DAY)
205 {
206//云多
207 uniSky.SetCloudCover(-1.5F);
208 uniSky.LerpCloudCover(-1.5F, -1.5F);
209//光少
210 uniSky.SetSunIntensity(0.25F);
211 uniSky.LerpSunIntensity(0.25F, 0.25F);
212//多卷云
213 uniSky.SetGlowVariance(-4);
214 uniSky.LerpGlowVariance(-4, -4);
215 }
216
217//雾天
218if (nowWeather == WeatherState.FOGGY_DAY)
219 {
220//云多
221 uniSky.SetCloudCover(1);
222 uniSky.LerpCloudCover(1, 1);
223//光少
224 uniSky.SetSunIntensity(0.2F);
225 uniSky.LerpSunIntensity(0.2F, 0.2F);
226//雾多
227 uniSky.SetFogLevel(0.02F);
228 uniSky.LerpFogLevel(0.02F, 0.02F);
229 }
230
231//阴天
232if (nowWeather == WeatherState.CLOUD_DAY)
233 {
234//云多
235 uniSky.SetCloudCover(2);
236 uniSky.LerpCloudCover(2, 2);
237//光少
238 uniSky.SetSunIntensity(0.2F);
239 uniSky.LerpSunIntensity(0.2F, 0.2F);
240 }
241 }
242
243///<summary>
244///改变时间天⽓状态
245///</summary>
246void ChangeTimeAndWeather()
247 {
248//--------------------------------------------时间 -----------------------------------------249
250//F1键为早晨
251if (Input.GetKeyDown(KeyCode.F1))
252 {
253 changeTime = true;
254 nowTime = TimeState.MORNING;
255 }
256
258if (Input.GetKeyDown(KeyCode.F2))
259 {
260 changeTime = true;
261 nowTime = TimeState.AM;
262 }
263
264//F3键为中午
265if (Input.GetKeyDown(KeyCode.F3))
266 {
267 changeTime = true;
268 nowTime = TimeState.NOONING;
269 }
270
271//F4键为下午
272if (Input.GetKeyDown(KeyCode.F4))
273 {
274 changeTime = true;
275 nowTime = TimeState.PM;
276 }
277
278//F5键为傍晚
279if (Input.GetKeyDown(KeyCode.F5))
280 {
281 changeTime = true;
282 nowTime = TimeState.DUSK;
283 }
284
285//F6键为晚上
286if (Input.GetKeyDown(KeyCode.F6))
287 {
288 changeTime = true;
289 nowTime = TimeState.NIGHT;
290 }
291
292//--------------------------------------------天⽓ -------------------------------------------293
294//数字键1为晴天
295if (Input.GetKeyDown(KeyCode.Alpha1))
296 {
297 changeWeather = true;
298 nowWeather = WeatherState.SUNNY_DAY;
299 }
300
301//数字键2为⾬天
302if (Input.GetKeyDown(KeyCode.Alpha2))
303 {
304 changeWeather = true;
305 nowWeather = WeatherState.RAINY_DAY;
306 }
307
308//数字键3为雪天
309if (Input.GetKeyDown(KeyCode.Alpha3))
310 {
311 changeWeather = true;
312 nowWeather = WeatherState.SNOWY_DAY;
313 }
314
315//数字键4为雾天
316if (Input.GetKeyDown(KeyCode.Alpha4))
317 {
318 changeWeather = true;
319 nowWeather = WeatherState.FOGGY_DAY;
320 }
321
322//数字键5为阴天
323if (Input.GetKeyDown(KeyCode.Alpha5))
324 {
325 changeWeather = true;
326 nowWeather = WeatherState.CLOUD_DAY;
327 }
328 }
329 }
核⼼控制类
效果图。

相关文档
最新文档