Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Geddem · Jul 24, 2015 at 09:08 AM · animationunity 5animator

2D Sprite Animation & 2D Image component

  • I have many (100+) sprite sheets, each of which contain many frames of a characters in 2D preforming various animations.

  • Every sprite frame has different dimensions (the frame size around an attacking unit, is different than that of him laying down dead)

  • These have been imported into Unity as sprite assets.

  • The pivots have been setup, such that when an animation plays using a sprite renderer, the sprite will position itself properly (pivot is on the feet, even if the animation is leaning forward or back).

I can render these as sprite animations on a SpriteRenderer, and everything works fine. However I want to make use of the Unity5 uGUI for this game. It looks like I need to have these render as an Image rather than a SpriteRenderer, and thats no problem for a single frame....

... however for animations the Image component will keep the dimensions based on the original frame in the animation. If I try and set each frame to "native size" every time the frame changes, the Animator will interpolate between the different frame sizes, which of course looks horrible.

TLDR; I have sprites with frames of different sizes. I want to display an animation in an Image component on a canvas, and by default Image does not handle animations where the image changes size. How can this be done?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Geddem · Jul 25, 2015 at 03:56 PM

So I wound up figuring this out. Oddly enough there is little to no documentation on how to go about creating Animators via script. So hopefully this helps the next person out some.

The overview is when I create Animator, I also have a script that lives on the same GameObject with the Image component.

 [ExecuteInEditMode]
 public class ImageSprite : MonoBehaviour
 {
     public bool SpriteChanged = false;
 
     /// <summary>
     /// Lates the update.
     /// </summary>
     public void LateUpdate()
     {
         if (SpriteChanged)
             OnSpriteChanged ();
         
     }
 
     /// <summary>
     /// Raises the sprite changed event.
     /// </summary>
     public void OnSpriteChanged()
     {
         Image.rectTransform.pivot = GetCurrentPivot();
         Image.SetNativeSize ();
         SpriteChanged = false;
     }
 
     /// <summary>
     /// Accecssor getter
     /// </summary>
     /// <value>The image.</value>
     private Image Image
     {
         get
         {
             return GetComponent<Image>();
         }
     }
 
     /// <summary>
     /// Gets the current pivot.
     /// </summary>
     /// <returns>The current pivot.</returns>
     private Vector2 GetCurrentPivot()
     {
         var i = Image;
         var pivot = i.sprite.pivot;
         var bounds = i.sprite.bounds;
 
         pivot.x /= bounds.size.x * i.sprite.pixelsPerUnit;
         pivot.y /= bounds.size.y * i.sprite.pixelsPerUnit;
 
         return pivot;
     }
 }

When I create the animator (via script), I have the animator tell the script to update the Image component. I could have done this w/ an event (they are easier to script), however whats not in the documentation is that Animator events are not executed in the editor! However, PPtrCurve bindings are, and for some unknown reason, Image.SetNativeSize () only works in LateUpdate anyway...

         /// <summary>
         /// Creates the sprite animation clip.
         /// </summary>
         /// <returns>The sprite animation clip.</returns>
         /// <param name="raw">Raw.</param>
         /// <param name="sprites">Sprites.</param>
         /// <param name="fps">Fps.</param>
         private static AnimationClip CreateSpriteAnimationClip(RawAnimationData raw, List<Sprite> sprites, int fps)
         {
             AnimationClip clip = new AnimationClip();
 
             // Setup the clip params
             clip.frameRate    = fps;
             clip.name         = raw.name;
             
             AnimationUtility.GetAnimationClipSettings(clip).loopTime = true;
             
             // Addthe initial sprite keyframes
             var frames = AddSpriteKeyframes (clip, raw, sprites);
 
             // Add the sprite changed flags
             AddSpriteChangedKeyframes (clip, frames);
 
             // Set the wrapping mode
             clip.wrapMode = raw.loop?WrapMode.Loop:WrapMode.Once;
             
             return clip;
         }
 
         /// <summary>
         /// Adds the sprite keyframes.
         /// </summary>
         /// <returns>The sprite keyframes.</returns>
         /// <param name="clip">Clip.</param>
         /// <param name="raw">Raw.</param>
         private static ObjectReferenceKeyframe[] AddSpriteKeyframes(AnimationClip clip, RawAnimationData raw, List<Sprite> sprites)
         {
             // Get the number of frames
             var framecount = raw.Frames.Count;
 
             // Init the binding
             EditorCurveBinding curveBinding = new EditorCurveBinding();
             curveBinding.type = typeof(Image);
             curveBinding.propertyName = "m_Sprite";
             
             ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[framecount];
             
             float time = 0.0f;
             for (int i = 0; i < framecount; i++)
             {
                 var frameId = raw.Frames[i].x;
                 var duration = ((float)raw.Frames[i].y)/1000f;
                 
                 ObjectReferenceKeyframe kf = new ObjectReferenceKeyframe();
                 
                 kf.time    = time;
                 kf.value= sprites[frameId];
                 keyFrames[i] = kf;
                 time += duration;
             }
             
             AnimationUtility.SetObjectReferenceCurve(clip, curveBinding, keyFrames);
 
             return keyFrames;
         }
 
         /// <summary>
         /// Adds the sprite changed keyframes.
         /// </summary>
         /// <param name="clip">Clip.</param>
         /// <param name="keyFrames">Key frames.</param>
         private static void AddSpriteChangedKeyframes(AnimationClip clip, ObjectReferenceKeyframe[] keyFrames)
         {
             var props = EditorCurveBinding.PPtrCurve ("", typeof(ImageSprite), "SpriteChanged");
             
             Keyframe [] frames = new Keyframe[keyFrames.Length];
             
             for (var i=0; i<keyFrames.Length; i++)
             {
                 var frame = keyFrames[i];
                 
                 var e = new Keyframe();
                 e.time = frame.time;
                 e.value = 1.0f;
                 
                 frames[i] = e;
             }
             AnimationUtility.SetEditorCurve (clip, props, new AnimationCurve (frames));
         }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Button animator not playing. 0 Answers

Animator is not playing an AnimatorController 1 Answer

root motion on in place animation 0 Answers

Dynamic way to adjust animated sprite length 0 Answers

Door animation bugs and stays open unity 3D 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges