查看: 3125|回复: 7
打印 上一主题 下一主题

[经验分享] Unity3D脚本:Unity3D闪烁灯光脚本

[复制链接]

2

主题

8

听众

787

积分

初级设计师

Rank: 3Rank: 3

纳金币
10
精华
0

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

跳转到指定楼层
楼主
发表于 2014-10-5 10:18:05 |只看该作者 |倒序浏览
using UnityEngine;
using System.Collections;
[RequireComponent( typeof( Light ) )]
public class flickeringLight : MonoBehaviour
{
// Flickering Styles
public enum flickerinLightStyles { CampFire = 0, Fluorescent = 1 };
public flickerinLightStyles flickeringLightStyle =flickerinLightStyles.CampFire;
// Campfire Methods
public enum campfireMethods { Intensity = 0, Range = 1, Both = 2 };
public campfireMethods campfireMethod = campfireMethods.Intensity;
// Intensity Styles
public enum campfireIntesityStyles { Sine = 0, Random = 1 };
public campfireIntesityStyles campfireIntesityStyle = campfireIntesityStyles.Random;
// Range Styles
public enum campfireRangeStyles { Sine = 0, Random = 1 };
public campfireRangeStyles campfireRangeStyle = campfireRangeStyles.Random;
// Base Intensity Value
public float CampfireIntensityBaseValue = 0.5f;
// Intensity Flickering Power
public float CampfireIntensityFlickerValue = 0.1f;
// Base Range Value
public float CampfireRangeBaseValue = 10.0f;
// Range Flickering Power
public float CampfireRangeFlickerValue = 2.0f;
// If Style is Sine
private float CampfireSineCycleIntensity = 0.0f;
private float CampfireSineCycleRange = 0.0f;
// "Glow" Speeds
public float CampfireSineCycleIntensitySpeed = 5.0f;
public float CampfireSineCycleRangeSpeed = 5.0f;
public float FluorescentFlickerMin = 0.4f;
public float FluorescentFlickerMax = 0.5f;
publicfloat FluorescentFlicerPercent = 0.95f;
// NOT IMPLEMENTED YET !!!!
public bool FluorescentFlickerPlaySound = false;
public AudioClip FluorescentFlickerAudioClip;
// ------------------------
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
switch( flickeringLightStyle )
{
// If Flickering Style is Campfire
case flickerinLightStyles.CampFire:
// If campfire method is Intesity OR Both
if( campfireMethod == campfireMethods.Intensity || campfireMethod ==campfireMethods.Both )
{
// If Intensity style is Sine
if( campfireIntesityStyle == campfireIntesityStyles.Sine )
{
// Cycle the Campfire angle
CampfireSineCycleIntensity += CampfireSineCycleIntensitySpeed;
if( CampfireSineCycleIntensity > 360.0f ) CampfireSineCycleIntensity = 0.0f;
// Base + Values
light.intensity = CampfireIntensityBaseValue + ( ( Mathf.Sin(CampfireSineCycleIntensity * Mathf.Deg2Rad ) * ( CampfireIntensityFlickerValue/ 2.0f ) ) + ( CampfireIntensityFlickerValue / 2.0f ) );
}
else light.intensity = CampfireIntensityBaseValue + Random.Range( 0.0f,CampfireIntensityFlickerValue );
}
// If campfire method is Range OR Both
if( campfireMethod == campfireMethods.Range || campfireMethod ==campfireMethods.Both )
{
// If Range style is Sine
if( campfireRangeStyle == campfireRangeStyles.Sine )
{
// Cycle the Campfire angle
CampfireSineCycleRange += CampfireSineCycleRangeSpeed;
if( CampfireSineCycleRange > 360.0f ) CampfireSineCycleRange = 0.0f;
// Base + Values
light.range = CampfireRangeBaseValue + ( ( Mathf.Sin( CampfireSineCycleRange *Mathf.Deg2Rad ) * ( CampfireSineCycleRange / 2.0f ) ) + (CampfireSineCycleRange / 2.0f ) );
}
else light.range = CampfireRangeBaseValue + Random.Range( 0.0f,CampfireRangeFlickerValue );
}
break;
// If Flickering Style is Fluorescent
case flickerinLightStyles.Fluorescent:
if( Random.Range( 0.0f, 1.0f ) > FluorescentFlicerPercent )
{
light.intensity = FluorescentFlickerMin;
// Check Audio - NOT IMPLEMENTED YET
if( FluorescentFlickerPlaySound )
{
}
}
else light.intensity = FluorescentFlickerMax;
break;
default:
// You should not be here.
break;
}
}
}

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

0

主题

2

听众

4092

积分

中级设计师

Rank: 5Rank: 5

纳金币
530
精华
0

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

沙发
发表于 2014-10-6 12:50:42 |只看该作者
感谢分享!
回复

使用道具 举报

0

主题

2

听众

1346

积分

助理设计师

Rank: 4

纳金币
505
精华
0
板凳
发表于 2014-10-6 15:37:08 |只看该作者
感谢分享与指导 刚好需用上脚本
回复

使用道具 举报

0

主题

1

听众

178

积分

设计实习生

Rank: 2

纳金币
2
精华
0

最佳新人

地板
发表于 2014-10-6 15:40:11 |只看该作者
不如提供一个文件下载
回复

使用道具 举报

hyui    

1

主题

2

听众

6671

积分

高级设计师

Rank: 6Rank: 6

纳金币
2715
精华
0

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

5#
发表于 2014-10-9 16:41:40 |只看该作者
Thanks for sharing this !
回复

使用道具 举报

0

主题

1

听众

1104

积分

助理设计师

Rank: 4

纳金币
-2
精华
0
6#
发表于 2015-1-14 14:59:37 |只看该作者
不错的帖子 赞一个!
回复

使用道具 举报

0

主题

1

听众

939

积分

初级设计师

Rank: 3Rank: 3

纳金币
330
精华
0

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

7#
发表于 2015-1-16 13:15:13 |只看该作者
怎么不提供一个截图效果啊           
回复

使用道具 举报

0

主题

1

听众

141

积分

设计实习生

Rank: 2

纳金币
8
精华
0

最佳新人

8#
发表于 2015-5-27 17:20:20 |只看该作者
感谢分享
回复

使用道具 举报

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

关闭

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

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

GMT+8, 2024-5-4 00:46 , Processed in 0.083296 second(s), 27 queries .

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

© 2008-2019 Narkii Inc.

回顶部