Готовый перевод C# Game Programming Cookbook for Unity 3D / Программирование игр на Unity 3D C#. Книга готовых решений.: 8.1.1 Разбор скрипта
using UnityEngine;
using System.Collections;
public class SoundObject
{
public AudioSource source;
public GameObject sourceGO;
public Transform sourceTR;
public AudioClip clip;
public string name;
public SoundObject(AudioClip aClip, string aName, float aVolume)
{ // in this (the constructor) we create a new audio source // and store the details of the sound itself sourceGO= new GameObject("AudioSource_"+aName);
sourceTR= sourceGO.transform;
source= sourceGO.AddComponent<AudioSource>();
source.name= "AudioSource_"+aName;
source.playOnAwake= false;
source.clip= aClip;
source.volume= aVolume;
clip= aClip;
name= aName;
}
public void PlaySound(Vector3 atPosition)
{
sourceTR.position= atPosition;
source.PlayOneShot(clip);
}
}
public class BaseSoundController : MonoBehavior
{
public static BaseSoundController Instance;
public AudioClip[] GameSounds;
private int totalSounds;
private ArrayList soundObjectList;
private SoundObject tempSoundObj;
public float volume= 1;
public string gamePrefsName= "DefaultGame"; // DO NOT FORGET TO SET
// THIS IN THE EDITOR!!
public void Awake()
{
Instance= this;
}
void Start ()
{
Mac OS X: ~LibraryPreferencesUnityWebPlayerPrefs
Windows: %APPDATA%\Unity\WebPlayerPrefs
// we will grab the volume from PlayerPrefs when this script
// first starts
volume= PlayerPrefs.GetFloat(gamePrefsName+"_SFXVol");
soundObjectList=new ArrayList();
foreach(AudioClip theSound in GameSounds)
{
tempSoundObj= new SoundObject(theSound,
theSound.name, volume);
soundObjectList.Add(tempSoundObj);
totalSounds++;
}
}
public void PlaySoundByIndex(int anIndexNumber, Vector3 aPosition)
{
if(anIndexNumber>soundObjectList.Count)
{
Debug.LogWarning("BaseSoundController>Trying to do
PlaySoundByIndex with invalid index number. Playing
last sound in array, instead.");
anIndexNumber= soundObjectList.Count-1;
}
A temporary AudioClip is made to hold the required sound as it is played with
AudioSource.PlaySound():
tempSoundObj= (SoundObject)soundObjectList[anIndexNumber];
tempSoundObj.PlaySound(aPosition);
}
}
Внимание! Этот перевод, возможно, ещё не готов.
Его статус: идёт перевод
Переведено на Нотабеноиде
http://notabenoid.org/book/55263/242521
Переводчики: ilia1395
Настройки
Готово:
