Convert Old On Gui to UI Imahe

How can I convert this sos that it uses the new UI system and not the old On Gui system?

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 ();
    }
}

Instead of using GUITexture, you need to use a RawImage. There are very few changes needed as far as I can see, just the places where GUITexture appears.

I could use some help on where I need to make my changes because i’m getting nowhere with it, I’ve tried all kinds of variations and not having any luck with it.

This is just the untested code adjustments that are needed as far as I can see:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

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 raw image:
     texture = new Texture2D (width, height);
     GetComponent<RawImage>().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 ();
   }
}

The script itself has to be place in a game object containing a RawImage component and that raw image needs to be part of the Unity UI hierarchy of game objects you have set up. That means it has to be a child of a canvas. However, if you struggle with that, you may have a look at the learning section of Unity where you find quite a few tutorials about the Unity UI.

All you did was change the GetComponent().texture = texture;
I had already done that in my early experiments, and it did not work so there must be a lot more to it than just that?

What exactly is not working? Are you getting error messages or is just nothing happening?

No error messages, just not working at all.

Do you see the texture? Is any texture being used?

Yes the texture is in my scene and as near as I can tell it is being used… I can see in the inspect the Raw image is being changed to black but no waveform is being displayed?? I would upload a screen capture but there is no option here.
I have tried doing this with the code and I still get Jack!

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

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 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 ();
    }
}

My UI Raw Image is 305 X 234 And I changed that in the code to match at the top, What I get is the whole Raw images flashes The color of the waveform but does it the whole image?

What is the UV Rect values of your RawImage?

Edit: It should be
X: 0
Y: 0
W: 1
H: 1

Ahhh Fuuuuuuuu… Meeee… thats what it was… thanks man Fixed :slight_smile: