Author Topic: Unity sprite animation  (Read 16290 times)

aigam

  • Guest
Unity sprite animation
« on: December 11, 2013, 04:12:23 AM »
Hi!
Is possible to expose the sprite property on the unity sprite component? Currently we can not animate the sprite (changing the sprite with the animation tool)

Thks!

thormond

  • Guest
Re: Unity sprite animation
« Reply #1 on: December 11, 2013, 07:04:51 AM »
You can animate sprite if you use Sprite Renderer but then you will have problems with putting that element between other NGUI elements.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unity sprite animation
« Reply #2 on: December 12, 2013, 01:56:43 AM »
Animate UI2DSprite.nextSprite and it will work as you expect.

thormond

  • Guest
Re: Unity sprite animation
« Reply #3 on: December 12, 2013, 05:06:38 AM »
Make sure that you have your NGUI on version 3.0.6 f6 or higher.

Caio.Lib

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 26
    • View Profile
Re: Unity sprite animation
« Reply #4 on: December 31, 2013, 02:04:50 PM »
Hello,
I'm trying to use sprite sheet animation, I've created an animation but i don't know where should i use it with UI2DSprite, could you help me?
How can i use UI2DSprite.nextSprite? I can't see it from Editor. Should i create a script, expose a public variable for animation to set UI2DSprite.nextSprite?
*Can i use Unity's Animator with UI2DSprite too?
*I'm using Unity 4.3 and I've updated NGUI today.

Thank you for your attention!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unity sprite animation
« Reply #5 on: December 31, 2013, 03:42:43 PM »
'nextSprite' field shows up in the animation window. You should use it when creating a Unity animation using 2D sprites.

Caio.Lib

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 26
    • View Profile
Re: Unity sprite animation
« Reply #6 on: December 31, 2013, 05:01:48 PM »
Sorry to bother you, but i still can't find it. I'm new to Animation system:
- I've added a print screen.
- I clicked 'Add Curve'.

Thank you ArenMook for your quick reply!!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unity sprite animation
« Reply #7 on: January 01, 2014, 10:06:30 PM »
I just had a look... it seems UnityEngine.Sprite references don't show up in the current version of Unity. Curious. Perhaps in 4.5?

Well, looks like just another limitation of the Unity's sprite system for the time being. 4.3's implementation of sprites is frustratingly limited. 4.5's implementation is much better, and I would strongly advise you to wait for 4.5 before using 2D sprites with NGUI. Some things are simply not possible to do correctly with 4.3 -- such as making sprites positioned properly when drawing from a sprite sheet. Unity cuts away empty pixels, but leaves no info on how much it cut off. Good luck trying to figure out how to position the sprite properly after that... Meh.

Well, my ramblings aside, you can use the following script to animate a 2D sprite:
  1. using UnityEngine;
  2.  
  3. public class UI2DSpriteAnimation : MonoBehaviour
  4. {
  5.         public int framerate = 20;
  6.         public bool ignoreTimeScale = true;
  7.         public UnityEngine.Sprite[] frames;
  8.  
  9.         UnityEngine.SpriteRenderer mUnitySprite;
  10.         UI2DSprite mNguiSprite;
  11.         int mIndex = 0;
  12.         float mUpdate = 0f;
  13.  
  14.         void Start ()
  15.         {
  16.                 mUnitySprite = GetComponent<UnityEngine.SpriteRenderer>();
  17.                 mNguiSprite = GetComponent<UI2DSprite>();
  18.                 if (framerate > 0) mUpdate = (ignoreTimeScale ? RealTime.time : Time.time) + 1f / framerate;
  19.         }
  20.  
  21.         void Update ()
  22.         {
  23.                 if (framerate != 0 && frames != null && frames.Length > 0)
  24.                 {
  25.                         float time = ignoreTimeScale ? RealTime.time : Time.time;
  26.  
  27.                         if (mUpdate < time)
  28.                         {
  29.                                 mUpdate = time;
  30.                                 mIndex = NGUIMath.RepeatIndex(framerate > 0 ? mIndex + 1 : mIndex - 1, frames.Length);
  31.                                 mUpdate = time + Mathf.Abs(1f / framerate);
  32.  
  33.                                 if (mUnitySprite != null)
  34.                                 {
  35.                                         mUnitySprite.sprite = frames[mIndex];
  36.                                 }
  37.                                 else if (mNguiSprite != null)
  38.                                 {
  39.                                         mNguiSprite.nextSprite = frames[mIndex];
  40.                                 }
  41.                         }
  42.                 }
  43.         }
  44. }

Caio.Lib

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 26
    • View Profile
Re: Unity sprite animation
« Reply #8 on: January 01, 2014, 11:25:48 PM »
I'm using sprite sheets for background with no alpha, hope it works.
For now i'm using your script, but for the next projects I'll probably have to wait for Unity 4.5.

Thank you for your time and code ArenMook!!
Have a great year!

Denwery

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Unity sprite animation
« Reply #9 on: February 14, 2014, 06:29:35 AM »
I have found that this script for organizing animations with UI2DSpriteAnimation works fine too:

public class UI2DSpriteAnimation : MonoBehaviour
{
   
    private UnityEngine.SpriteRenderer mUnitySprite;
    private UI2DSprite mNguiSprite;
   
    private void Start()
    {
        mUnitySprite = GetComponent<UnityEngine.SpriteRenderer>();
        mNguiSprite = GetComponent<UI2DSprite>();
    }

    void Update()
    {
        mNguiSprite.nextSprite = mUnitySprite.sprite;
    }

(But if write "mNguiSprite.sprite2D = mUnitySprite.sprite;" visual effect of sprite changing is visible.)
« Last Edit: February 14, 2014, 03:45:57 PM by Denwery »