Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

WWW and MovieTexture uses

Discussion in 'Scripting' started by Georges Abitbol, Jan 28, 2013.

  1. Georges Abitbol

    Georges Abitbol

    Joined:
    Jan 28, 2013
    Posts:
    10
    Just an explain to avoid memory leaks using WWW and MovieTexture :

    I have a component who load movies on a texture. At the end of the first one it loads the second, etc ...
    And I had a lot of memory leaks.

    To correct it you had to destroy the MovieTexture on your object material before affect it the WWW.movie.

    Sample :

    Code (csharp):
    1.  
    2. public class TestTV : MonoBehaviour
    3. {
    4.     private string url = "";
    5.     private WWW request = null;
    6.     private bool play = false;
    7.  
    8.     public Renderer texture;
    9.  
    10.     void Start()
    11.     {
    12.         url = Application.dataPath + "/Test.ogg";
    13.         if (!Application.dataPath.Contains("http:")  !Application.dataPath.Contains("file:"))
    14.         {
    15.             url = "file://" + Application.dataPath + "/Test.ogg";
    16.         }
    17.         request = new WWW(url);
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if (request != null)
    23.         {
    24.             if (request.isDone)
    25.             {
    26.                 Destroy(texture.material.mainTexture); // To avoir memory leaks
    27.                 texture.material.mainTexture = request.movie;
    28.                 request = null;
    29.                 play = false;
    30.             }
    31.         }
    32.  
    33.         if ((texture != null)  (texture.material.mainTexture is MovieTexture))
    34.         {
    35.             if (((MovieTexture)texture.material.mainTexture).isReadyToPlay  !((MovieTexture)texture.material.mainTexture).isPlaying)
    36.             {
    37.                 if (!play)
    38.                 {
    39.                     play = true;
    40.                     ((MovieTexture)texture.material.mainTexture).Play();
    41.                 }
    42.                 else
    43.                 {
    44.                     play = false;
    45.                     ((MovieTexture)texture.material.mainTexture).Stop();
    46.                     request = new WWW(url);
    47.                 }
    48.             }
    49.         }
    50.     }
    51.  
     
  2. tofitouf

    tofitouf

    Joined:
    Jan 10, 2013
    Posts:
    1
    There is a great lack of documentation for the movietexture.
    We can't figure out how the memory will react, or how the decodign thread really work...

    Does we have to destroy images ? when ?
    Does the garbage collector will do it ?

    No Oficial answers to these questions on the forums, answers and in the documentation