- Home /
Is it possible to Render Spectrum Data onto a UI Image?
Is it possible to render spectrum data onto a UI image in the same way that this screen capture of GUI windows is done? And if so how would this be done? I know I can do it with a GUI image. But UI is better for layout management. This you would THINK would have an answer by now?
Possible? Yes. It's even easier if you understand the data. I'd search for a good graphing plugin.
Thanks Smoggach, Well I have Visualizer Studio, it's where the screen cap comes from, but having trouble translating the old GUI window stuff to the new UI way of doing things.
I can get it to work with this only I need it to work on a UI image or UI Raw image, whichever is best suitable for this type of thing.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
//[RequireComponent(typeof(AudioSource))]
[RequireComponent(typeof(GUITexture))]
public class AudioWaveFormVisualizer : $$anonymous$$onoBehaviour
{
public AudioSource $$anonymous$$usicPlayer;
public int width = 500; // texture width
public int height = 100; // texture height
public Color backgroundColor = Color.black;
public Color waveformColor = Color.green;
public int size = 2048; // size of sound segment displayed in texture
private Color[] blank; // blank image array
private Texture2D texture;
private float[] samples; // audio samples array
IEnumerator Start ()
{
// create the samples array
samples = new float[size];
// create the texture and assign to the guiTexture:
texture = new Texture2D (width, height);
guiTexture.texture = texture;
// create a 'blank screen' image
blank = new Color[width * height];
for (int i = 0; i < blank.Length; i++) {
blank [i] = backgroundColor;
}
// refresh the display each 100mS
while (true) {
GetCurWave ();
yield return new WaitForSeconds (0.1f);
}
}
void GetCurWave ()
{
// clear the texture
texture.SetPixels (blank, 0);
// get samples from channel 0 (left)
$$anonymous$$usicPlayer.GetOutputData (samples, 0);
// draw the waveform
for (int i = 0; i < size; i++) {
texture.SetPixel ((int)(width * i / size), (int)(height * (samples [i] + 1f) / 2f), waveformColor);
} // upload to the graphics card
texture.Apply ();
}
}
I can render the waveform spectrum data to a GUI image and then parent the game object it's on to a canvas or panel but results would be better if the same method could be used on either a UI image or UI raw image??? $$anonymous$$ust be a way to do this???
well you have to work with a texture at first, then you apply the texture to your UI image
RawImage can take a "raw" texture (RawImage.texture) or you have to create a sprite from your texture with Sprite.Create() then set this sprite on your image.
Your answer
![](https://koobas.hobune.stream/wayback/20220613182909im_/https://answers.unity.com/themes/thub/images/avi.jpg)