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. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Play Movie Texture in Material on Unity 5?

Discussion in 'Editor & General Support' started by Xander-Davis, Aug 22, 2015.

  1. Xander-Davis

    Xander-Davis

    Joined:
    Apr 23, 2011
    Posts:
    441
    Having trouble getting a video to play as a texture in a material. Advice?

    The Unity documentation: http://docs.unity3d.com/Manual/class-MovieTexture.html says add a script (presumably JS in the start() function and presumably to the GameObject with the material featuring the movie texture. In my material, I dropped the movie into the Albedo of the Standard Shader in Unity 5)

    ((MovieTexture)GetComponent<Renderer>().material.mainTexture).Play();​

    So I did that and attached the script to the object with the material:
    #pragmastrict

    functionStart () {
    ((MovieTexture)GetComponent<Renderer>().material.mainTexture).Play();
    }

    functionUpdate () {

    }​


    Then I get the following errors:



    Okay....

    So what might be the issue?
     
  2. Xander-Davis

    Xander-Davis

    Joined:
    Apr 23, 2011
    Posts:
    441
    Anyone? Playing a movie should be pretty simple right? What am I missing?
     
  3. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  4. Xander-Davis

    Xander-Davis

    Joined:
    Apr 23, 2011
    Posts:
    441
    Caught a Unity doc typo. Nice. Also curious Unity or MonoDevelop couldn't be bothered to throw an error or warning from any of that then.

    Also 'if I switch to JS'? That was JS-- created from within the Unity editor and opened in MonoDevelop. Did it do something to the formatting?

    Tell you what though, it DID throw an error when I added the period after GetComponent.



    And since you're from Unity, perhaps pass it on up that there should just be a toggle on materials using movie textures for playback. This is pretty in the weeds for something so simple, yeah?
     
  5. Xander-Davis

    Xander-Davis

    Joined:
    Apr 23, 2011
    Posts:
    441
    Unexpected char 's'. Cool. There isn't an 's' in any of this code. Sweet.

    So I tried deleting that script, creating a new UnityScript from within MonoDevelop and wrote in the line after Start again. Got this:



    Nice.

    I remove the ). Got:



    Okay... Added two semicolons even though that's obviously not the solution. No change from Unity. What about dropping the semicolon, even though that's also obviously not it.



    Brilliant.
     
    Last edited: Aug 29, 2015
  6. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    The error message says that line 1 has an 's' that's unexpected. Isn't that because there is no space between pragma and strict?
     
  7. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    I just tried the code example from the documentation page and it's:

    Code (csharp):
    1. #pragma strict
    2.  
    3. var movTexture : MovieTexture;
    4.  
    5. function Start () {
    6.     GetComponent.<Renderer>().material.mainTexture = movTexture;
    7.     movTexture.Play();
    8. }
    Which compiles with no problems... so think it's the way you can get your code working. Bear in mind that Javascript uses the "as" keyword as casting. You can do your one-liner like this:

    Code (csharp):
    1. #pragma strict
    2.  
    3. function Start () {
    4.     (GetComponent.<Renderer>().material.mainTexture as MovieTexture).Play();
    5. }
     
  8. Xander-Davis

    Xander-Davis

    Joined:
    Apr 23, 2011
    Posts:
    441
    That worked-- thanks! In the future, it would've been easier to just post that on the doc... The doc does NOT say to do that simple script. Instead it just says "paste this line of code that doesn't work and then here's a further example of how you could use it by attaching key commands instead of just showing how it could simply work."

    But I'm glad you've resolved it here. Maybe paste exactly the last code example in your post above over this one in the doc and save others the trouble of having to figure out what the doc actually meant:

    // this line of code will make the Movie Texture begin playing
    ((MovieTexture)GetComponent<Renderer>().material.mainTexture).Play();


    I've noticed these docs tend to give half-answers. Why not just explicitly post the solution if it's a doc trying to provide an example solution? Again this isn't anything complex-- this is playing a movie. Frankly, this should be built into the engine directly via the shader UI.

    Pardon the frustration here-- again, thank you for your assistance.
     
    kkrg001 likes this.
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The code in the docs works fine, but it's C#. The issue is that the scripting docs have a button to use C# or Unityscript, but the manual does not—some of the code is C#, some of it's Unityscript.

    --Eric
     
  10. Xander-Davis

    Xander-Davis

    Joined:
    Apr 23, 2011
    Posts:
    441
    That's starting to make more sense to me then... I think I assumed it was JavaScript because this shouldn't be so complicated we need to go into C#... lol Plus all previous examples I have seen were in JavaScript, which no longer work in Unity 5. ...And nowhere in the doc does it say what language we're in if you're coming in cold off a search.
     
    Last edited: Aug 30, 2015
  11. magonicolas

    magonicolas

    Joined:
    Dec 4, 2013
    Posts:
    30
    On What object do you attach this script?
     
  12. Xander-Davis

    Xander-Davis

    Joined:
    Apr 23, 2011
    Posts:
    441
    The object with your material that uses a movie for a texture.