Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

(SOLVED) Texture only shows after ALT+TAB

Discussion in 'General Graphics' started by CDAfonso, Nov 20, 2019.

  1. CDAfonso

    CDAfonso

    Joined:
    Sep 16, 2019
    Posts:
    26
    I have this script that generates a texture wave form from an audioclip in audiosource.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7.  
    8.  
    9. public class wave : MonoBehaviour
    10.  
    11. {
    12. [SerializeField] public AudioClip audio;
    13.  
    14. [SerializeField] public AudioClip CloneAudioClip;
    15.  
    16. [SerializeField] public Image waveform;
    17. public Texture tex;
    18.  
    19.  
    20. public void WaveTexture() {
    21.    
    22.     this.audio = GameObject.Find("Controller").GetComponent<AudioSource>().clip;
    23.        
    24.      
    25.         AudioClip newAudioClip = AudioClip.Create(name, audio.samples, audio.channels, audio.frequency, false);
    26.         float[] copyData = new float[audio.samples * audio.channels];
    27.         audio.GetData(copyData, 0);
    28.         List<float> monoData = new List<float>();
    29.         for (int i = 0; i < copyData.Length; i+=2)
    30.         {
    31.             monoData.Add(copyData[i]);
    32.         }
    33.         newAudioClip.SetData(monoData.ToArray(), 0);
    34.      
    35.  
    36. Texture2D PaintWaveformSpectrum(AudioClip audio, float saturation, int width, int height, Color col) {
    37.      Texture2D tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
    38.      float[] samples = new float[audio.samples];
    39.      float[] waveform = new float[width];
    40.      audio.GetData(samples, 0);
    41.      int packSize = ( audio.samples / width ) + 1;
    42.      int s = 0;
    43.      for (int i = 0; i < audio.samples; i += packSize) {
    44.          waveform[s] = Mathf.Abs(samples[i]);
    45.          s++;
    46.      }
    47.      for (int x = 0; x < width; x++) {
    48.          for (int y = 0; y < height; y++) {
    49.              tex.SetPixel(x, y, Color.black);
    50.          }
    51.      }
    52.      for (int x = 0; x < waveform.Length; x++) {
    53.          for (int y = 0; y <= waveform[x] * ((float)height * .75f); y++) {
    54.              tex.SetPixel(x, ( height / 2 ) + y, col);
    55.              tex.SetPixel(x, ( height / 2 ) - y, col);
    56.          }
    57.      }
    58.    
    59.      tex.Apply();
    60.    
    61.      return tex;
    62. }
    63.  
    64.  
    65.  
    66.  
    67.  
    68.  
    69. var color = new Color32(47, 157, 43, 255);
    70.  
    71. waveform.material.mainTexture = PaintWaveformSpectrum(newAudioClip,1, 5400,400,color);
    72.  
    73.  
    74.  
    75. }
    76.  
    77.  
    78. }
    79.  

    The Audioclip is obtained from this script:

    Code (CSharp):
    1. IEnumerator GetAudioClip()
    2.  
    3.     {
    4.  
    5.         var path = "file://" + SongData.fileName;
    6.         // Start downloading
    7.          using (var download = new WWW(path))
    8.         {
    9.            GameObject.Find("waveform").GetComponent<RectTransform>().localScale = new Vector2(0,0);
    10.             // Wait for download to finish
    11.             yield return download;
    12.             // Create ogg vorbis file
    13.             var clip = download.GetAudioClip();
    14.             // Play it
    15.             if (clip != null)
    16.             {
    17.                AudioClip song = clip;
    18.                AudioSource.clip = song;
    19.  
    20.                 Debug.Log ("File Loaded!");
    21.  
    22.  
    23.             GameObject.Find("slider").GetComponent<Slider>().minValue = 0;
    24.             GameObject.Find("slider").GetComponent<Slider>().maxValue = AudioSource.clip.length;
    25.             GameObject.Find("waveform").GetComponent<RectTransform>().localScale = new Vector2(1,1);
    26.  
    27.             GameObject.Find("waveform").GetComponent<wave>().Invoke("WaveTexture", 1);
    28.  
    29.             }
    30.             else     // Handle error
    31.             {
    32.                 Debug.Log("Ogg vorbis download failed. (Incorrect link?)");
    33.  
    34.                 AudioClip song = Resources.Load<AudioClip>("badliar");
    35.                 AudioSource.clip = song;
    36.  
    37.  
    38.             GameObject.Find("slider").GetComponent<Slider>().minValue = 0;
    39.             GameObject.Find("slider").GetComponent<Slider>().maxValue = AudioSource.clip.length;
    40.             GameObject.Find("waveform").GetComponent<RectTransform>().localScale = new Vector2(1,1);
    41.  
    42.  
    43.             }
    44.  
    45.         }
    46.  
    47.     }
    48.  
    Somehow the texture only apply when i hit ALT+TAB and return to the editor. In Built mode, it doesnt show at all.

    Any suggestions?
     
  2. CDAfonso

    CDAfonso

    Joined:
    Sep 16, 2019
    Posts:
    26
    Update:
    When i put an imported audio (from assets) into the audiosource, this doesn't happen.

    I tried to invoke the waveTexture() function with 5 / 10 seconds delay to wait for the file to read, but it doesn't work.
    ALT+TAB immediately after the load or after works every-time..

    Anyone can help me?
     
  3. CDAfonso

    CDAfonso

    Joined:
    Sep 16, 2019
    Posts:
    26
    SOLVED!

    Refreshing the image solved the problem:

    /// . enable = false;
    /// . enable = true;