public void ApplySoundStateOptions(Actor actor, AudioSource soundInstance, SoundStateOptions options, Guid id, bool startSound) { if (options != null) { //pause must happen before other sound state changes if (options.paused != null && options.paused.Value == true) { if (_unpausedSoundInstances.RemoveAll(x => x.id == id) > 0) { soundInstance.Pause(); } } if (options.Volume != null) { soundInstance.volume = options.Volume.Value; } if (options.Pitch != null) { //convert from halftone offset (-12/0/12/24/36) to pitch multiplier (0.5/1/2/4/8). soundInstance.pitch = Mathf.Pow(2.0f, (options.Pitch.Value / 12.0f)); } if (options.Looping != null) { soundInstance.loop = options.Looping.Value; } if (options.Doppler != null) { soundInstance.dopplerLevel = options.Doppler.Value; } if (options.Spread != null) { soundInstance.spread = options.Spread.Value * 180.0f; } if (options.RolloffStartDistance != null) { soundInstance.minDistance = options.RolloffStartDistance.Value; soundInstance.maxDistance = options.RolloffStartDistance.Value * 1000000.0f; } //unpause must happen after other sound state changes if (!startSound) { if (options.paused != null && options.paused.Value == false) { if (!_unpausedSoundInstances.Exists(x => x.id == id)) { soundInstance.UnPause(); _unpausedSoundInstances.Add(new SoundInstance(id, actor)); } } } } }
public AudioSource TryAddSoundInstance(Actor actor, Guid id, Guid soundAssetId, SoundStateOptions options, float?startTimeOffset) { var obj = MREAPI.AppsAPI.AssetCache.GetAsset(soundAssetId); var audioClip = MREAPI.AppsAPI.AssetCache.GetAsset(soundAssetId) as AudioClip; if (audioClip != null) { float offset = startTimeOffset.GetValueOrDefault(); if (options.Looping != null && options.Looping.Value) { offset = offset % audioClip.length; } if (offset < audioClip.length) { var soundInstance = actor.gameObject.AddComponent <AudioSource>(); soundInstance.clip = audioClip; soundInstance.time = offset; soundInstance.spatialBlend = 1.0f; soundInstance.spread = 90.0f; //only affects multichannel sounds. Default to 50% spread, 50% stereo. soundInstance.minDistance = 1.0f; soundInstance.maxDistance = 1000000.0f; ApplySoundStateOptions(actor, soundInstance, options, id, true); if (options.paused != null && options.paused.Value == true) { //start as paused soundInstance.Play(); soundInstance.Pause(); } else { //start as unpaused _unpausedSoundInstances.Add(new SoundInstance(id, actor)); soundInstance.Play(); } return(soundInstance); } } return(null); }