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

sprite animation

Discussion in 'Editor & General Support' started by longsword469, Jan 4, 2010.

  1. longsword469

    longsword469

    Joined:
    Sep 1, 2009
    Posts:
    41
    Hello every one,

    the sprite manager for unity is good. but I have a question can I create sprite animation with out using the SM2 in Unity, if its possible please tell me the way.

    I rendered a rotate sequence of a coin, and I create sprite atlas for that and now I want to put my animated sprite on a billboard in Unity, How is it possible?

    Thank You very much,

    Enjoy.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If you can make the animation into a movie then a MovieTexture might be the best way to go. You can also set an object's texture from a script (with renderer.material.mainTexture), so you could change frames on a regular basis in the Update function.
     
  3. longsword469

    longsword469

    Joined:
    Sep 1, 2009
    Posts:
    41
    First Thank you very much, andeee for your replay.

    I tried out movie texture also but problem with alpha channel, I mean I want coin look not a square look.

    is movie texture support alpha channel?

    Thanks,

    Enjoy
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If you need alpha (or other image effects) you can handle the frame updates for the animation directly. Add an array of Texture2D to your script to hold the frames. Then, in the Update method, progress between frames periodically:-
    Code (csharp):
    1. var frames: Texture2D[];    // Set this from the inspector
    2. var frameTime: float;
    3.  
    4. private var lastFrameTime: float;
    5.  
    6. function Start() {
    7.     lastFrameTime = Time.time;
    8. }
    9.  
    10.  
    11. var currFrame: integer;
    12.  
    13. function Update() {
    14.     if ((Time.time - lastFrameTime) >= frameTime) {
    15.         currFrame = (currFrame + 1) % frames.Length;
    16.         renderer.material.mainTexture = frames[currFrame];
    17.     }
    18. }
     
  5. Razieln64

    Razieln64

    Joined:
    May 3, 2008
    Posts:
    129
    Hi,

    You can use the Animation view in unity 2.6.1 to call a function that will change the quad's texture or play a sound or whatever you need to do next. This saves you a lot of fps by not calling the update function. For more info you should check the documentation about the Animation view. The only downside is that the Animation view in Unity iPhone is obsolete and doesn't let you modify the animations. Build your animations in Unity and then import them into Unity iPhone and it will work.
     
  6. chicknstu

    chicknstu

    Joined:
    May 4, 2010
    Posts:
    11
    Awesome example andeeee, did exactly what it says on the tin. Although there's a line missing, you need to update the last frame time after you change images with 'lastFrameTime = Time.time;'

    Here's a c# version...
    Code (csharp):
    1.  
    2. public class UpdateAnimation : MonoBehaviour {
    3.  
    4. public Texture2D[] frames;    // Set this from the inspector
    5. public float frameTime;
    6. private float lastFrameTime;
    7. int currFrame = 0;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         lastFrameTime = Time.time;
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         if ((Time.time - lastFrameTime) >= frameTime)
    18.         {
    19.             Debug.Log("ChangeFrame");
    20.             currFrame = (currFrame + 1) % frames.Length;
    21.             renderer.material.mainTexture = frames[currFrame];
    22.             lastFrameTime = Time.time;
    23.         }
    24.     }
    25. }
     
    Last edited: Feb 22, 2011
  7. sheva

    sheva

    Joined:
    Nov 22, 2010
    Posts:
    157
    guys, is there any website where I can download some sprites for unity with a fluid animation?
     
  8. sebazistan

    sebazistan

    Joined:
    May 28, 2010
    Posts:
    1
    Hi.. I'm a noob.
    Is this is a way to play a series of images (like a PNG sequence with alpha)? How do I set "var frames: Texture2D[];" from the inspector? What are the steps to make this work (after I have the image sequence and a plane in Unity)? Thanks so much.. wish Unity supported movies with alpha.
     
  9. matis1989

    matis1989

    Joined:
    Mar 23, 2011
    Posts:
    162
    @Chicknstu very nice edited andeeee script thanks a lot.
    works great!
     
  10. patrick_vargas

    patrick_vargas

    Joined:
    Oct 13, 2011
    Posts:
    11
    tnk you man...that is just that i need...
    god bless you =)
     
  11. JoeVoxel

    JoeVoxel

    Joined:
    Sep 28, 2012
    Posts:
    127
    yahoooooooooo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Thanks a lot guys !!!! this i s beautifulllllll !!!!!!!!
    It works !!!