Search Unity

Mov as texture!

Discussion in 'Scripting' started by robertseadog, Oct 3, 2005.

  1. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Ok this is not working, have you changed something in Unity 1.1 or what?

    Code (csharp):
    1.  
    2. //-- Using .mov files as textures on obj:
    3.  
    4. var framesPerSecond = 15;
    5.  
    6. function Update () {
    7.  
    8.    //-- Calculate the frame to use by repeating time when it exceeds playLength
    9.    
    10.    var FrameCount = Texture.frameCount;
    11.    var playLength = FrameCount / framesPerSecond;
    12.    var timeMe = Mathf.Repeat (Time.time, playLength) / playLength;
    13.    var frame : int = timeMe * FrameCount;
    14.    
    15.    frame = Mathf.Clamp (frame, 0, Texture.frameCount -1);
    16.    Texture.frame = frame;
    17.    
    18.         if (frame == FrameCount) {
    19.         //do something!
    20.         }
    21. }
    22.  
     
  2. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    I get this error:

    1.frameCount is not a menber of Unity Texture!

    2.frame is not a member of Unity Texture!
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  4. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Ah, the "var texture : FileTexture;" wasn't there when I last checked.. Working nicely now though ;)
     
  5. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Ok, now why doesn't this work as expected?

    Code (csharp):
    1.  
    2. //-- Using .mov files as textures on obj:
    3.  
    4. var texture : FileTexture;
    5.  
    6. var framesPerSecond = 15.0;
    7.  
    8.  
    9. private var stopMovie = true;
    10.  
    11.  
    12. function Update () {
    13.  
    14.  
    15. startMovie ();
    16.  
    17.  
    18.  
    19.     if (stopMovie == false) {
    20.  
    21.     frameCount = texture.frameCount;
    22.  
    23.  
    24.  
    25.     playLength = frameCount / framesPerSecond;
    26.  
    27.  
    28.  
    29.     time = Mathf.Repeat (Time.time, playLength) / playLength;
    30.  
    31.  
    32.  
    33.     var frame : int = time * frameCount;
    34.  
    35.     frame = Mathf.Clamp (frame, 0, texture.frameCount -1);
    36.  
    37.  
    38.  
    39.     texture.frame = frame;
    40.     }
    41.    
    42.         if (frame > 39) {
    43.         //do something!
    44.        
    45.         stopMovie = true;
    46.         finishMe ();
    47.         }
    48. }
    49. function startMovie () {
    50. yield WaitForSeconds (1.2);
    51. stopMovie = false;
    52. }
    53.  
    54. function finishMe () {
    55.  
    56. yield WaitForSeconds (3.4);
    57. Application.LoadLevel("Menu");
    58.  
    59. }
    60.  
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Can't really follow the code easily. It would probably be a good idea to nicely indent the code, thus gaining more clarity.

    anyway one mistake is that you call:
    startMovie ();

    But startMovie (); is a coroutine, thus you have to use StartCoroutine or SendMessage to start it.
     
  7. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Is coroutines the same as functions in Unity? How is regular functions called?
     
  8. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    A coroutine is a function that has a yield statement somewhere in it.

    Update and FixedUpdate can not be coroutines.
     
  9. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    It doesn't seem that I can call coroutine inside Update.. Is there no equalent to a function in Unity? (I just need to call a function inside Update, thats all!).
     
  10. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Of course Unity has functions.

    But the function you are calling contains a yield. That's the problem. You can not call coroutines directly only normal functions that do not contain a yield.
     
  11. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Hmmm... could this be what you are trying to do?

    Code (csharp):
    1.  
    2. //-- Using .mov files as textures on obj:
    3.  
    4. var texture : FileTexture;
    5. var framesPerSecond = 15.0;
    6. var stopAfterFrame = 39;
    7. private var stopMovie = true;
    8.  
    9.  
    10. yield WaitForSeconds (1.2);
    11. stopMovie = false;
    12.  
    13. function Update () {
    14.  
    15.     if (stopMovie == false) {
    16.    
    17.         playLength = texture.frameCount / framesPerSecond;
    18.    
    19.         time = Mathf.Repeat (Time.time, playLength) / playLength;
    20.         var frame : int = time * frameCount;
    21.         frame = Mathf.Clamp (frame, 0, texture.frameCount -1);
    22.         texture.frame = frame;
    23.         if (frame > stopAfterFrame) {
    24.             stopMovie = true;
    25.             StartCoroutine("FinishMe");
    26.        
    27.         }
    28.     }
    29. }
    30.  
    31. function FinishMe () {
    32.     yield WaitForSeconds (3.4);
    33.     Application.LoadLevel("Menu");
    34.  
    35. }
    36.  
     
  12. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    ah! I'll just use a timeLeft variable then. Thanks for clearing up.
     
  13. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    That was just the thing! I ended up using a timeleft var and subtracted from that by deltaTime, but your solution is much more elegant and just what I wanted to do!

    Thanks a bunch!