1. 程式人生 > >Unity3D實現閃光燈效果

Unity3D實現閃光燈效果

Unity3D閃光燈效果的實現程式碼:

using UnityEngine;
using System.Collections;

//定義一個Light類
public class Light : MonoBehaviour {
    //定義一個時間長度
    public float duration = 1.0F;
    //定義一個紅色(顏色自取)
    public Color colorRed = Color.red;
    //定義一個藍色(顏色自取)
    public Color colorBlue = Color.blue;

    // Update is called once per frame
void Update () { float phi = Time.time / duration * 2 * Mathf.PI; //使用數學函式來實現閃光燈效果 float amplitude = Mathf.Cos(phi) * 0.5F + 0.5F; light.intensity = amplitude; float x = Mathf.PingPong (Time.time, duration) / duration; light.color = Color.Lerp (colorRed, colorBlue, x); } }