/* ///////////////////////////////////////////////////////////////////////////////////////////////// CUAVolume 2024-05-09 by MacGruber Just a volume slider that applies to all AudioSources inside a CUA. https://www.patreon.com/MacGruber_Laboratory Licensed under CC BY-SA. (see https://creativecommons.org/licenses/by-sa/4.0/) ///////////////////////////////////////////////////////////////////////////////////////////////// */ using UnityEngine; namespace MacGruber { public class CUAVolume : MVRScript { private JSONStorableFloat myVolume; private Transform myRoot; private Transform myCUA; private bool myVolumeChanged = true; public override void Init() { myVolume = SetupSliderFloat(this, "Volume", 1.0f, 0.0f, 1.0f, false); myVolume.setCallbackFunction += (float v) => { myVolumeChanged = true; }; } private void Update() { // Update only if volume or CUA changed if (myVolumeChanged || myCUA == null) { if (myRoot == null) myRoot = containingAtom.reParentObject.Find("object/rescaleObject"); if (myRoot == null || myRoot.childCount == 0) return; myCUA = myRoot.GetChild(0); UpdateVolume(myCUA); myVolumeChanged = false; } } private void UpdateVolume(Transform t) { // Apply recursively to all AudioSources in the CUA. AudioSource audio = t.GetComponent(); if (audio != null) audio.volume = myVolume.val; for (int i=0; i