找回密码
 立即注册
楼主: 多米诺

[特效] 闪光灯源码 拖到脚本即可

[复制链接]
发表于 2012-6-11 22:56 | 显示全部楼层 |阅读模式
  1. <pre> using UnityEngine;  
  2. using System.Collections;  
  3. [RequireComponent( typeof( Light ) )]  
  4. public class  shanguangdeng: 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.    
  41.       // Use this for initialization  
  42.       void Start () {  
  43.         
  44.       }  
  45.         
  46.       // Update is called once per frame  
  47.       void Update () {  
  48.           switch( flickeringLightStyle )  
  49.           {  
  50.               // If Flickering Style is Campfire  
  51.               case flickerinLightStyles.CampFire:  
  52.                   // If campfire method is Intesity OR Both  
  53.                   if( campfireMethod == campfireMethods.Intensity || campfireMethod == campfireMethods.Both )  
  54.                   {  
  55.                       // If Intensity style is Sine  
  56.                       if( campfireIntesityStyle == campfireIntesityStyles.Sine )  
  57.                       {  
  58.                           // Cycle the Campfire angle  
  59.                           CampfireSineCycleIntensity += CampfireSineCycleIntensitySpeed;  
  60.                           if( CampfireSineCycleIntensity > 360.0f ) CampfireSineCycleIntensity = 0.0f;  
  61.                           // Base + Values  
  62.                           light.intensity = CampfireIntensityBaseValue + ( ( Mathf.Sin( CampfireSineCycleIntensity * Mathf.Deg2Rad ) * ( CampfireIntensityFlickerValue / 2.0f ) ) + ( CampfireIntensityFlickerValue / 2.0f ) );  
  63.                       }  
  64.                       else light.intensity = CampfireIntensityBaseValue + Random.Range( 0.0f, CampfireIntensityFlickerValue );  
  65.                   }  
  66.                   // If campfire method is Range OR Both  
  67.                   if( campfireMethod == campfireMethods.Range || campfireMethod == campfireMethods.Both )  
  68.                   {  
  69.                       // If Range style is Sine  
  70.                       if( campfireRangeStyle == campfireRangeStyles.Sine )  
  71.                       {  
  72.                           // Cycle the Campfire angle  
  73.                           CampfireSineCycleRange += CampfireSineCycleRangeSpeed;  
  74.                           if( CampfireSineCycleRange > 360.0f ) CampfireSineCycleRange = 0.0f;  
  75.                           // Base + Values  
  76.                           light.range = CampfireRangeBaseValue + ( ( Mathf.Sin( CampfireSineCycleRange * Mathf.Deg2Rad ) * ( CampfireSineCycleRange / 2.0f ) ) + ( CampfireSineCycleRange / 2.0f ) );  
  77.                       }  
  78.                       else light.range = CampfireRangeBaseValue + Random.Range( 0.0f, CampfireRangeFlickerValue );  
  79.                   }  
  80.                   break;  
  81.               // If Flickering Style is Fluorescent  
  82.               case flickerinLightStyles.Fluorescent:  
  83.                   if( Random.Range( 0.0f, 1.0f ) > FluorescentFlicerPercent )  
  84.                   {  
  85.                       light.intensity = FluorescentFlickerMin;  
  86.                       // Check Audio - NOT IMPLEMENTED YET  
  87.                       if( FluorescentFlickerPlaySound )  
  88.                       {  
  89.                       }  
  90.                   }  
  91.                   else light.intensity = FluorescentFlickerMax;  
  92.                   break;  
  93.               default:  
  94.                   // You should not be here.  
  95.                   break;  
  96.           }  
  97.         
  98.       }  
  99.   }  </pre>
复制代码
发表于 2014-5-8 14:18 | 显示全部楼层
好东西 学习学习
发表于 2017-2-23 16:59 | 显示全部楼层
很不错
发表于 2017-2-23 17:02 | 显示全部楼层
顶顶多好
发表于 2017-2-23 16:32 | 显示全部楼层
真心顶
发表于 2017-2-23 16:22 | 显示全部楼层
很好哦
发表于 2017-2-23 17:01 | 显示全部楼层
不错不错
发表于 2017-7-3 16:49 | 显示全部楼层
楼主是超人
发表于 2017-7-3 17:05 | 显示全部楼层
真心顶
发表于 2017-7-3 17:18 | 显示全部楼层
难得一见的好帖
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-28 05:40 , Processed in 0.091002 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表