The question is answered, right answer was accepted
Change On GUI Texture to a UI Texture Help
I have this Audio Waveform visualizer script that uses the old On GUI texture which is next to impossible to get to line up properly with the new UI system and a multiple UI canvases & cameras kind of set up in a single scene, Can anyone tell me how I can modify this script to make it use the UI image instead which is much easier to position in my canvas layouts?
Anyone??
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
//[RequireComponent(typeof(AudioSource))]
[RequireComponent(typeof(GUITexture))]
public class AudioWaveFormVisualizer : MonoBehaviour
{
public AudioSource MusicPlayer;
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);
GetComponent<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)
MusicPlayer.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 ();
}
}
Answer by KnightRiderGuy · Dec 20, 2015 at 10:14 PM
This fixed it.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AudioWaveFormVisualizer : MonoBehaviour
{
public AudioSource MusicPlayer;
public int width = 305; // texture width
public int height = 234; // 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 rawimage;
public RawImage Texture2D;
private float[] samples; // audio samples array
IEnumerator Start ()
{
// create the samples array
samples = new float[size];
// create the texture and assign to the guiTexture:
rawimage = new Texture2D (width, height);
GetComponent<RawImage>().texture = rawimage;
// 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
rawimage.SetPixels (blank, 0);
// get samples from channel 0 (left)
MusicPlayer.GetOutputData (samples, 0);
// draw the waveform
for (int i = 0; i < size; i++) {
rawimage.SetPixel ((int)(width * i / size), (int)(height * (samples [i] + 1f) / 2f), waveformColor);
} // upload to the graphics card
rawimage.Apply ();
}
}
Good night friend, I'm having the same problem that you had with your script in a very similar, at least the situation is similar. I've tried to update everything that is my script and I couldn't. Could you help me, please? Thank you very much.
Follow this Question
Related Questions
What Am I doing Wrong Here 1 Answer
Scaling an image sent to UI Image 0 Answers
Reset Raw UI Image For Audio Spectrum Data 0 Answers
2d unity game Camera issue 0 Answers
Hotbar appears when a certain gameobject is selected? 1 Answer