|  | 
 
| 创建游戏控制器,使之能很好地为游戏中诸多的操作服务,比如控制警报系统的警报灯C#脚本
 JS脚本复制代码using UnityEngine;
using System.Collections;
public class LastPlayerSighting : MonoBehaviour
{
    public Vector3 position = new Vector3(1000f, 1000f, 1000f);       // 玩家没有被发现时候的视野初始坐标.
    public Vector3 resetPosition = new Vector3(1000f, 1000f, 1000f);  // 为判断是否安全预设的一个坐标.
    public float lightHighIntensity = 0.25f;                         // 警报灯没响时方向光的强度最大值.
    public float lightLowIntensity = 0f;                              // 警报灯响起的时候方向光的最小值
    public float fadeSpeed = 7f;                                        // 光从低到高亮渐隐的速度
    public float musicFadeSpeed = 1f;                                 // 音乐渐隐的速度
        
    private AlarmLight alarm;                                          // 预设一个AlarmLight脚本变量.
    private Light mainLight;                                            // 预设一个main light 的light变量.
    private AudioSource panicAudio;                                   // 预设一个AudioSources脚本变量紧张音乐.
    private AudioSource[] sirens;                                       // 预设一组sirens 声音变量.
        
    void Awake ()
    {
        // 设置alarm的引用通过Tag管理器找到Light_directional_alarm中的AlarmLight脚本.
        alarm = GameObject.FindGameObjectWithTag(Tags.alarmLight).GetComponent<AlarmLight>();
        
        // 设置mainLight的引用通过Tag管理器找到Light_directional_main中的light .
        mainLight = GameObject.FindGameObjectWithTag(Tags.mainLight).light;
        
        // 设置panicAudio的引用通过Tag管理器找到secondaryMusic中的audio.
        panicAudio = transform.Find("secondaryMusic").audio;
        
        // 获取返回多个大喇叭的数组.
        GameObject[] sirenGameObjects = GameObject.FindGameObjectsWithTag(Tags.siren);
        
        // 创建一个跟sirenGameObjects数组个数一样的一个新数组.
        sirens = new AudioSource[sirenGameObjects.Length];
        
        // 把六个大喇叭的组件中的声音存在新数组中.
        for(int i = 0; i < sirens.Length; i++)
        {
            sirens[i] = sirenGameObjects[i].audio;
        }
    }    
    
    void Update ()
    {
        // 更替背景音乐.
        SwitchAlarms();
              MusicFading();
    }
    
    void SwitchAlarms ()
    {
        // 判断警报是否该响起.
        alarm.alarmOn = position != resetPosition;
        //灯光强度去向变量
        float newIntensity;
        
        // 如果position与resetPosition不相符,即警报响起的时候...
        if(position != resetPosition)
            // ... 设置将要显示的光强度为弱光.
            newIntensity = lightLowIntensity;
        else
            // 设置当将要显示的强度为强光.
            newIntensity = lightHighIntensity;
        
        // 给场景中的主光源从当前光强做将要光强的插值.
        mainLight.intensity = Mathf.Lerp(mainLight.intensity, newIntensity, fadeSpeed * Time.deltaTime);
        
        // 循环遍历大喇叭
        for(int i = 0; i < sirens.Length; i++)
        {
            // ... 如果警报被激活了,警报铃大喇叭还没响起的话就响
            if(position != resetPosition && !sirens[i].isPlaying)
                sirens[i].Play();
            // 如果警报没被激活,警报铃大喇叭就不出声.
            else if(position == resetPosition)
                sirens[i].Stop();
        }
    }    
    
    void MusicFading ()
    {
        // 如果警报被激活...
        if(position != resetPosition)
        {
            // ... 普通背景音乐淡出...
            audio.volume = Mathf.Lerp(audio.volume, 0f, musicFadeSpeed * Time.deltaTime);
            
            // ... 紧张节奏的音乐渐渐奏起.
            panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
        }
        else
        {
            // 紧张背景音乐淡出,普通背景音乐渐响
            audio.volume = Mathf.Lerp(audio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
            panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0f, musicFadeSpeed * Time.deltaTime);
        }
    }
}
复制代码#pragma strict
public var position : Vector3 = new Vector3(1000f, 1000f, 1000f);       // The last global sighting of the player.
public var resetPosition : Vector3 = new Vector3(1000f, 1000f, 1000f);  // The default position if the player is not in sight.
public var lightHighIntensity : float = 0.25f;                          // The directional light's intensity when the alarms are off.
public var lightLowIntensity : float = 0f;                              // The directional light's intensity when the alarms are on.
public var fadeSpeed : float = 7f;                                      // How fast the light fades between low and high intensity.
public var musicFadeSpeed : float = 1f;                                 // The speed at which the 
private var alarm : AlarmLight;                                     // Reference to the AlarmLight script.
private var mainLight : Light;                                          // Reference to the main light.
private var panicAudio : AudioSource;                                       // Reference to the AudioSource of the panic msuic.
private var sirens : AudioSource[];                                     // Reference to the AudioSources of the megaphones.
function Awake ()
{
    // Setup the reference to the alarm light.
    alarm = GameObject.FindGameObjectWithTag(Tags.alarm).GetComponent(AlarmLight);
    
    // Setup the reference to the main directional light in the scene.
    mainLight = GameObject.FindGameObjectWithTag(Tags.mainLight).light;
    
    // Setup the reference to the additonal audio source.
    panicAudio = transform.Find("secondaryMusic").audio;
    
    // Find an array of the siren gameobjects.
    var sirenGameObjects : GameObject[] = GameObject.FindGameObjectsWithTag(Tags.siren);
    
    // Set the sirens array to have the same number of elements as there are gameobjects.
    sirens = new AudioSource[sirenGameObjects.Length];
    
    // For all the sirens allocate the audio source of the gameobjects.
    for(var i = 0; i < sirens.Length; i++)
    {
        sirens[i] = sirenGameObjects[i].audio;
    }
}
function Update ()
{
    // Switch the alarms and fade the music.
    SwitchAlarms();
    MusicFading();
}
function SwitchAlarms ()
{
    // Set the alarm light to be on or off.
    alarm.alarmOn = position != resetPosition;
    
    // Create a new intensity.
    var newIntensity : float;
    
    // If the position is not the reset position...
    if(position != resetPosition)
        // ... then set the new intensity to low.
        newIntensity = lightLowIntensity;
    else
        // Otherwise set the new intensity to high.
        newIntensity = lightHighIntensity;
    
    // Fade the directional light's intensity in or out.
    mainLight.intensity = Mathf.Lerp(mainLight.intensity, newIntensity, fadeSpeed * Time.deltaTime);
    
    // For all of the sirens...
    for(var i = 0; i < sirens.Length; i++)
    {
        // ... if alarm is triggered and the audio isn't playing, then play the audio.
        if(position != resetPosition && !sirens[i].isPlaying)
            sirens[i].Play();
        // Otherwise if the alarm isn't triggered, stop the audio.
        else if(position == resetPosition)
            sirens[i].Stop();
    }
}
function MusicFading ()
{
    // If the alarm is not being triggered...
    if(position != resetPosition)
    {
        // ... fade out the normal music...
        audio.volume = Mathf.Lerp(audio.volume, 0f, musicFadeSpeed * Time.deltaTime);
        
        // ... and fade in the panic music.
        panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
    }
    else
    {
        // Otherwise fade in the normal music and fade out the panic music.
        audio.volume = Mathf.Lerp(audio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
        panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0f, musicFadeSpeed * Time.deltaTime);
    }
}
 | 
 |