• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Dec 20, 2015 at 10:21 PM by KnightRiderGuy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by KnightRiderGuy · Dec 20, 2015 at 05:33 PM · c#uiaudioui imagewave

What Am I doing Wrong Here

OK I have tried numerous ways to convert this blasted thing to use the new UI text what the heck am I missing here?

 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 
     public 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);
         Texture = new Texture();
 
         //GetComponent<GUITexture>().texture = texture;
         Texture2D.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 ();
     } 
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image beppim · Dec 20, 2015 at 06:15 PM 0
Share

Can you tell exactly what you need and what error do you get?

avatar image KnightRiderGuy beppim · Dec 20, 2015 at 06:24 PM 0
Share

@beppim, Thanks I put it back to this because I was getting nowhere with it. Frustrating. But all I simply wanted to do was convert the crappy on GUI to use a UI image which is much easier to position onto a canvas.

This is the original untouched script not the one with the messing about I had done.

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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by KnightRiderGuy · Dec 20, 2015 at 10:13 PM

This solved the issue!

 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 ();
     } 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

49 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change On GUI Texture to a UI Texture Help 1 Answer

Level Selector - Level Images for Brick Breaker 2 Answers

Scaling an image sent to UI Image 0 Answers

AUDIO DATA to STATIC WAVEFORM DISPLAY? 1 Answer

Unity 5 change source image of UI image c# script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges