[Unity3D]呼风唤雨:天气插件UniSky的使用教程与案例

发表于2017-06-16
评论0 5.6k浏览

一些游戏场景需要用到天气变化,这就要借助UniSky这款插件,UniSky是Unity3D的一款模拟环境天气的插件,使用它可以简单的实现呼风唤雨的各种功能。


使用起来也非常的方便。

引入Package后(注意路径必须是英文否则会出错)Project中会多一个文件夹:

下面我们来简单的创建一个下雨的场景。

首先从UniSky中拖动UniSkyAPI到Hierachy面板:

然后再把Camara与API建立连接:

接下来创建一个空的GameObject用来控制UniSkyAPI,并将脚本ExampleScript拖到它身上:


这个脚本的源码如下:

[javascript] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class ExampleScript : MonoBehaviour {  
  5.   
  6.     // instance of the API  
  7.     private UniSkyAPI uniSky;  
  8.       
  9.     void Awake() {  
  10.           
  11.         // Define instance  
  12.         uniSky = GameObject.Find("UniSkyAPI").GetComponent("UniSkyAPI") as UniSkyAPI;  
  13.           
  14.         // Initiate and create default UniSky   
  15.         uniSky.InstantiateUniSky();  
  16.         // Set some initial states   
  17.         uniSky.SetTime(12.0f);  
  18.         uniSky.SetAmbientLighting(new Color(0.1f, 0.1f, 0.1f, 0.1f));  
  19.         uniSky.SetStormCenter(new Vector3(0,0,0));  
  20.         uniSky.SetSunShadows(LightShadows.Soft);  
  21.           
  22.         // Functions to interpolate parameters over time  
  23.         /* 
  24.         uniSky.LerpCloudCover(0.5f, 5000.0f); 
  25.         uniSky.LerpPrecipitationLevel(0.6f, 5000.0f); 
  26.         uniSky.LerpStormCloudCover(-1.0f, 10000.0f); 
  27.         uniSky.LerpRainLevel(100, 0.2f, 10000.0f); 
  28.         uniSky.LerpStormLevel(150, 0.4f, 20000.0f); 
  29.         uniSky.LerpSunIntensity(0.2f, 10000.0f); 
  30.         uniSky.LerpFogLevel(0.02f, 20000.0f); 
  31.         uniSky.LerpAmbientLighting(new Color(0.0f, 0.0f, 0.0f, 0.0f), 5000); 
  32.         uniSky.ClearDropletBuffer(); 
  33.         uniSky.LerpDropletLevel(10, 20000.0f); 
  34.         */  
  35.     }  
  36.   
  37.     void Update() {  
  38.   
  39.     }  
  40. }  

以下是对于插件API函数的简要解释:

[javascript] view plain copy
  1. API函数/参数  
  2. 下面是对UniSky参数的介绍,并且都对其进行了效果解释。下面的列表可以再UniSkyAPI.js/cs中找到。  
  3. //一天的时间(0到24小时循环)  
  4. SetTime(float time);  
  5. LerpTime(float time, float milliseconds);  
  6. GetTime();  
  7.   
  8.   
  9.   
  10. //返回太阳的颜色  
  11. GetSunColor();  
  12. //启用或禁用太阳和月亮的阴影。类型: "LightShadows.None" 、"LightShadows.Hard"和 "LightShadows.Soft"  
  13. public void SetSunShadows(LightShadows sunShadows)  
  14. public void SetMoonShadows(LightShadows moonShadows)  
  15.   
  16.    
  17. //设置暴风雨云层中心的坐标  
  18. SetStormCenter(Vector3 stormCenter);  
  19.   
  20.    
  21. //散射半径,他影响着天空的颜色(默认是45000,通常情况下是一个真实的天空)  
  22. SetScatteringRadius(float scatteringRadius);  
  23. LerpScatteringRadius(float scatteringRadius, float milliseconds);  
  24.   
  25.    
  26. //主大气层的覆盖是(-5到5)  
  27. void SetCloudCover(float cloudCover);  
  28. LerpCloudCover(float cloudCover, float milliseconds);  
  29.   
  30.    
  31. //黑暗的云层,不是暴风雨的大气层。(2-0,越低,云层颜色越深)  
  32. SetPrecipitationLevel(float precipitationLevel);  
  33. LerpPrecipitationLevel(float precipitationLevel, float milliseconds);  
  34.   
  35.    
  36. //暴风雨的覆盖层级是(建议是-3.5到-1.0)  
  37. SetStormCloudCover(float cloudCover);  
  38. LerpStormCloudCover(float cloudCover, float milliseconds);  
  39.   
  40.    
  41. //雨的数量。没有大暴雨的,但是都是小雨。(任何地方都是从0-1000)  
  42. SetRainLevel(float rainLevel, float sfxLevel);  
  43. LerpRainLevel(float rainLevel, float sfxLevel, float milliseconds);  
  44.    
  45. //特大暴雨。一片都有雨。(任何地方都是从0-200)  
  46. SetStormLevel(float stormLevel, float sfxLevel);  
  47. LerpStormLevel(float stormLevel, float sfxLevel, float milliseconds);  
  48.    
  49. //太阳的亮度或浓度。由于黑暗中阴天或暴风雨天(0到0.5)淡入淡出  
  50. SetSunIntensity(float intensity);  
  51. LerpSunIntensity(float intensity, float milliseconds);  
  52.    
  53. //设置内置雾(0到0.03)  
  54. SetFogLevel(float fogLevel);  
  55. LerpFogLevel(float fogLevel, float milliseconds);  
  56.   
  57.    
  58. //设置屏幕图像上滴落的速率(0到5)  
  59. SetDropletLevel(float dropletLevel);  
  60. LerpDropletLevel(float dropletLevel, float milliseconds);  
  61.   
  62.   
  63.   
  64. //设置云的方向,加速度和演化的速度。X=x方向和加速度,Y=y方向和加速度,Z=演化速度(0到1)  
  65. SetCloudSpeedDirection(Vector3 speedDirection);  
  66. LerpCloudSpeedDirection(Vector3 speedDirection, float milliseconds);  
  67. //光晕差异。设置云边缘的照明强度(0到10)。提示:设置为0左右,会有一个很好的卷云效果。  
  68. SetGlowVariance(float glowVariance);  
  69. LerpGlowVariance(float glowVariance, float milliseconds);  
  70.    
  71. //设置云褪色到大气中的距离(0到20)  
  72. SetViewDistance(float viewDistance);  
  73. LerpViewDistance(float viewDistance, float milliseconds);  
  74.    
  75. //调节默认颜色。建议RGB的默认值(1.5,1.5,1.5)。如果你想要粉红色或绿色的云:D  
  76. SetColorVariance(Vector3 colorVariance);  
  77. LerpColorVariance(Vector3 colorVariance, milliseconds);  
  78.   
  79. //如果你想时间自动循环,这个是时间流逝的速度。(0到0.1)  
  80. SetSpeedOfTime(float speedOfTime);  
  81.   
  82.   
  83.   
  84. //改变光散射到云中,只影响云的颜色。最好是保持他的默认值,40.(-20到100)  
  85. SetRayleighLevel(float rayleighLevel);  
  86. //如果启用该选项,一天中时间的播放器与系统时钟同步。  
  87. SetUseSystemTime(bool enabled);  
  88.   
  89.    
  90. //改变环境光的颜色。我建议在一天的时间内进行插值或混浊。  
  91. SetAmbientLighting(Color ambientLevel);  
  92. LerpAmbientLighting(Color ambientLevel, float milliseconds);  
  93.   
  94.    
  95. //设置月亮的大小  
  96. SetMoonSize(float moonSize);  
  97.    
  98. //清除雾滴缓存  
  99. ClearDropletBuffer();  
  100.    
  101. //设置打雷闪电的频率。越高越频繁(0到100)。  
  102. SetLightningFrequency(float  
  103. frequency);  

此时点击运行,便会发现UniSky已经基本成型了:



当然想过不是很理想,因为没有地面。。

如果需要下雨的效果,只需要加上这么一行代码:

[javascript] view plain copy
  1. uniSky.SetStormLevel(1000,1000);  
或者是:

[javascript] view plain copy
  1. uniSky.SetRainLevel(1000,1000);  

就可以实现了:

完整的学习案例还是参照插件中自带的案例UniSky Example Scene,效果非常好:

当然啦,如果只是要简单的下雨特效,推荐这款插件:Rain.package

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引