Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
For Unity, create a script that will take an audio file as input then display a…
- User
For Unity, create a script that will take an audio file as input then display a visualizer in time with the music.
- Assistant
Here is an example of a simple audio visualizer script in Unity:
csharpusing UnityEngine; using UnityEngine.UI; public class AudioVisualizer : MonoBehaviour { public AudioSource audioSource; public RectTransform visualizer; private float[] samples = new float[64]; void Update() { audioSource.GetSpectrumData(samples, 0, FFTWindow.BlackmanHarris); float maxSample = 0f; foreach (float sample in samples) { if (sample > maxSample) { maxSample = sample; } } visualizer.localScale = new Vector3(visualizer.localScale.x, maxSample * 100f, visualizer.localScale.z); } }This script assumes you have an
AudioSourcecomponent attached to your game object, and aRectTransformcomponent for the visualizer. TheGetSpectrumDatamethod is used to get the audio samples from the audio clip, and theforeachloop is used to find the maximum sample value. ThelocalScaleof the visualizer is then set to a height proportional to the maximum sample.