查看: 1603|回复: 5
打印 上一主题 下一主题

[其他] 闪烁灯光脚本

[复制链接]

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2014-6-26 17:03:11 |只看该作者 |倒序浏览
  1. using UnityEngine;
  2. using System.Collections;
  3. [RequireComponent( typeof( Light ) )]
  4. public class flickeringLight : MonoBehaviour
  5. {
  6.     // Flickering Styles
  7.     public enum flickerinLightStyles { CampFire = 0, Fluorescent = 1 };
  8.     public flickerinLightStyles flickeringLightStyle = flickerinLightStyles.CampFire;
  9.     // Campfire Methods
  10.     public enum campfireMethods { Intensity = 0, Range = 1, Both = 2 };
  11.     public campfireMethods campfireMethod = campfireMethods.Intensity;
  12.     // Intensity Styles
  13.     public enum campfireIntesityStyles { Sine = 0, Random = 1 };
  14.     public campfireIntesityStyles campfireIntesityStyle = campfireIntesityStyles.Random;
  15.     // Range Styles
  16.     public enum campfireRangeStyles { Sine = 0, Random = 1 };
  17.     public campfireRangeStyles campfireRangeStyle = campfireRangeStyles.Random;
  18.     // Base Intensity Value
  19.     public float CampfireIntensityBaseValue = 0.5f;
  20.     // Intensity Flickering Power
  21.     public float CampfireIntensityFlickerValue = 0.1f;
  22.     // Base Range Value
  23.     public float CampfireRangeBaseValue = 10.0f;
  24.     // Range Flickering Power
  25.     public float CampfireRangeFlickerValue = 2.0f;
  26.    
  27.     // If Style is Sine
  28.     private float CampfireSineCycleIntensity = 0.0f;
  29.     private float CampfireSineCycleRange = 0.0f;
  30.     // "Glow" Speeds
  31.     public float CampfireSineCycleIntensitySpeed = 5.0f;
  32.     public float CampfireSineCycleRangeSpeed = 5.0f;
  33.     public float FluorescentFlickerMin = 0.4f;
  34.     public float FluorescentFlickerMax = 0.5f;
  35.     public float FluorescentFlicerPercent = 0.95f;
  36.     // NOT IMPLEMENTED YET !!!!
  37.     public bool FluorescentFlickerPlaySound = false;
  38.     public AudioClip FluorescentFlickerAudioClip;
  39.     // ------------------------


  40.         // Use this for initialization
  41.         void Start () {
  42.         
  43.         }
  44.         
  45.         // Update is called once per frame
  46.         void Update () {
  47.         switch( flickeringLightStyle )
  48.         {
  49.             // If Flickering Style is Campfire
  50.             case flickerinLightStyles.CampFire:
  51.                 // If campfire method is Intesity OR Both
  52.                 if( campfireMethod == campfireMethods.Intensity || campfireMethod == campfireMethods.Both )
  53.                 {
  54.                     // If Intensity style is Sine
  55.                     if( campfireIntesityStyle == campfireIntesityStyles.Sine )
  56.                     {
  57.                         // Cycle the Campfire angle
  58.                         CampfireSineCycleIntensity += CampfireSineCycleIntensitySpeed;
  59.                         if( CampfireSineCycleIntensity > 360.0f ) CampfireSineCycleIntensity = 0.0f;
  60.                         // Base + Values
  61.                         light.intensity = CampfireIntensityBaseValue + ( ( Mathf.Sin( CampfireSineCycleIntensity * Mathf.Deg2Rad ) * ( CampfireIntensityFlickerValue / 2.0f ) ) + ( CampfireIntensityFlickerValue / 2.0f ) );
  62.                     }
  63.                     else light.intensity = CampfireIntensityBaseValue + Random.Range( 0.0f, CampfireIntensityFlickerValue );
  64.                 }
  65.                 // If campfire method is Range OR Both
  66.                 if( campfireMethod == campfireMethods.Range || campfireMethod == campfireMethods.Both )
  67.                 {
  68.                     // If Range style is Sine
  69.                     if( campfireRangeStyle == campfireRangeStyles.Sine )
  70.                     {
  71.                         // Cycle the Campfire angle
  72.                         CampfireSineCycleRange += CampfireSineCycleRangeSpeed;
  73.                         if( CampfireSineCycleRange > 360.0f ) CampfireSineCycleRange = 0.0f;
  74.                         // Base + Values
  75.                         light.range = CampfireRangeBaseValue + ( ( Mathf.Sin( CampfireSineCycleRange * Mathf.Deg2Rad ) * ( CampfireSineCycleRange / 2.0f ) ) + ( CampfireSineCycleRange / 2.0f ) );
  76.                     }
  77.                     else light.range = CampfireRangeBaseValue + Random.Range( 0.0f, CampfireRangeFlickerValue );
  78.                 }
  79.                 break;
  80.             // If Flickering Style is Fluorescent
  81.             case flickerinLightStyles.Fluorescent:
  82.                 if( Random.Range( 0.0f, 1.0f ) > FluorescentFlicerPercent )
  83.                 {
  84.                     light.intensity = FluorescentFlickerMin;
  85.                     // Check Audio - NOT IMPLEMENTED YET
  86.                     if( FluorescentFlickerPlaySound )
  87.                     {
  88.                     }
  89.                 }
  90.                 else light.intensity = FluorescentFlickerMax;
  91.                 break;
  92.             default:
  93.                 // You should not be here.
  94.                 break;
  95.         }
  96.         
  97.         }
  98. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

115

主题

3

听众

5676

积分

高级设计师

Rank: 6Rank: 6

纳金币
7268
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2014-6-26 18:08:42 |只看该作者
Thanks for this !
回复

使用道具 举报

hyui    

1

主题

2

听众

6671

积分

高级设计师

Rank: 6Rank: 6

纳金币
2715
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2014-6-26 18:39:21 |只看该作者
Good to know!
回复

使用道具 举报

0

主题

2

听众

4092

积分

中级设计师

Rank: 5Rank: 5

纳金币
530
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2014-6-27 07:44:31 |只看该作者
感谢分享
回复

使用道具 举报

2

主题

2

听众

972

积分

初级设计师

Rank: 3Rank: 3

纳金币
14
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2014-6-27 09:53:37 |只看该作者
Thanks for share !
回复

使用道具 举报

wucnj    

1

主题

1

听众

3160

积分

中级设计师

Rank: 5Rank: 5

纳金币
1065
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2014-6-27 14:18:47 |只看该作者
感谢分享!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-18 22:50 , Processed in 0.087769 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部