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

SM2 - 2D made easy and efficient!

Discussion in 'iOS and tvOS' started by Brady, Nov 4, 2009.

  1. Gaspedal

    Gaspedal

    Joined:
    Mar 29, 2009
    Posts:
    376
    @Brady: When the game is finished and I don't need longer the source frames for re-creating the sprite atlases, then I want delete the source files because the memory usage of the source frames (with automatically checked get/set pixel RGBA32 Bit) is too high.

    This would be useful before building the final version of the game for appstore. A checkbox for final building would be great that disabled the source files for final version of the game. I think that we don't need longer the source frames when we already builded the sprite atlases.

    I must now edit the frames manually and this very hard and time intensive. (with 80 Source Frames!)

    I get memory warnings from GBA when I start my App with SM2 some Objects.
     
  2. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    I'm planning on adding a custom build menu option that will do the build and automatically move the source textures out of the assets folder, and then back in when the build is complete. However, it won't be time intensive if you make sure that you organize all your source textures under a single folder. So you could put them somewhere like: Assets/Source Textures

    Then, when you build, just drag the "Source Textures" folder out of Assets, build, then you can drag them back in. So that way it is just a single step before your build and a single step after.

    The custom build menu option will do this for you, however, once implemented.
     
  3. Gaspedal

    Gaspedal

    Joined:
    Mar 29, 2009
    Posts:
    376
    moving the sprites out of asset folder is a good idea. I thought that SM2 would bring out a error message when the files are missing. I will this try now. :) All my sprites are in the same folder assets/textures/sprites/ ...player ...enemies/
     
  4. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    I have a few questions:

    With SpriteManager 2 and the Sprite Timeline Editor I can create a texture out of multiple animation frames that only uses one draw call. Very nice.

    1. If I fill up a 1024x1024 texture with animation frames for one character/Game Object, can I use another 1024x1024 texture for a different type of character/Game Object, and then I will only have two draw calls in my scene?

    2. Can I separate animations for one Game Object between two textures, i.e. a walk animation is on one texture and a run animation is on a second texture?
     
  5. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    aerende:
    1. Yes, exactly.

    2. Not at the moment, but this might be possible in the future. At present, the way you would handle this is if you needed additional textures for all your animations, when you go to display an animation that uses a different texture than the current one, you would disable the currently displaying sprite and enable the sprite with the desired material. But I may be able to have the sprite change textures automatically in the future.
     
  6. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    Not sure if this is the best place to ask this question but since I am using Sprite Manager 2 I figured I'd start here.

    Unity has animation.crossFade. Is this something that I can use with animations that are used in Sprite Manager 2? Or is animation.crossFade only useful for 3D animations where there are rigged skeletons, etc?
     
  7. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Unity's animation is bone-based, affecting vertices. So the crossfade feature blends the weighting of two different vertex influences. So I suppose you could use this with SM2, sort of, but I'm not sure how useful doing so would be. Sprite animation is, after all, a fundamentally different thing from vertex animation. SM2 deals with sprite animation.
     
  8. cooli2010

    cooli2010

    Joined:
    May 16, 2009
    Posts:
    17
    How to use java Script call it? :roll:
     
  9. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    You will find a sample Javascript script here:
    http://forum.unity3d.com/viewtopic.php?p=229451#229451

    Make sure you are logged into the forum and you should see the attachment. This will be included in future releases. Also make sure if you are calling SM2 from Javascript that you put the SM2 scripts themselves under a subfolder in your "Assets" folder called "Plugins". This will make sure that Unity compiles the SM2 scripts first so that they are accessible from Javascript.
     
  10. Razieln64

    Razieln64

    Joined:
    May 3, 2008
    Posts:
    129
    Hi,

    I'd like to know if SM2 supports calling a delegate's function at a specific frame, not only at the end of the animation. I actually use SM1 and I modded it a bit to do so. My use for this is for example, I have a neon light that is flashing on and off. I want to play a sound every time the light goes on. I need to call a function that plays a sound on every odd frame. My actual implementation of SM1 does that. Can SM2 do that? If not, can I modify the SM2 to do so? I'm really interested in acquiring SM2 but before I do, I want to make sure that it will fulfill my needs.

    Thanks
     
  11. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    That is a feature I plan to add in a (near-)future release. But since you get the sourcecode to the whole thing, yes, you could definitely modify it to do this very easily. And depending on how you did it in SM1, you may be able to simply copy-paste your mod into SM2.
     
  12. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    I have a question about SM2.

    Lets say I have a character sprite. I want this character to hold a lot of different weapons.

    Is it possible to have one polygon with the base character sprite and their animations run and then have a separate polygon that contains the current weapon with its own animations?

    Is it possible to animate both at the same time, but keep the ability to animate just one or the other?
     
  13. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Yes, you should definitely be able to do that. The only issue I foresee is that if you had a character holding a gun and you have a running animation for that character, you'd want that gun to move with the character's hands when running, which would be different than, say, a standing animation. That would mean having a "run" animation for both the character and the gun. Though you might be able to keyframe position the gun polygon separately using Unity's animation editor (SM2 handles sprite animation as opposed to transform animation).

    Plus, for something like a rifle, the character would need one hand in front of the gun relative to the camera, and the other hand behind, so I'm not 100% sure how you would do that with only 2 polygons - perhaps the hand on the camera side would be part of the gun sprite?

    I hope that helps.
     
  14. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    Thankfully no guns to worry about :)

    For this game 'character' is a bit excessive. Imagine a colored blob who has a weapon that kind of floats in front of him. There isnt actually a connecting arm or hand to worry about.

    I think that I will be ok assuming I can parent whatever equipment the character has to their root body.

    For instance say the character moves x distance.

    I play "move" for the body animation and the body animates, and both the body and the weapon physically move x together.

    And yeah I imagine both the weapons and the body have an 'attack' animation. That way the body has 1 (very simple game) and I can also then call the attack animation of whatever weapon they are currently holding.

    P.S. I bought SM2 and have to say bravo up to this point.

    In the span of this evening (about an hours worth of work) I have been able to use Pixen to draw and animate a character, import this character into Unity iPhone, and using SM2 build to my dev device my character and his animation. Any time you tap the screen the animation plays.

    Thanks!
     
  15. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    I'm glad to hear everything's going great. In the case you described, you should have no problem at all doing that with SM2.
     
  16. outtoplay

    outtoplay

    Joined:
    Apr 29, 2009
    Posts:
    741
    Brady,

    Any chance of posting a simple iphone example of triggering a sprite animation with SM2? Say, running the cow anim on touch?

    I love the product, but this small example would be mighty appreciated.

    B.
     
  17. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    brendang:
    Sure.

    Probably the simplest possible example would be something like (goes in Update(), I won't supply the function declaration itself just to avoid confusion since that is language-dependent):

    Code (csharp):
    1.  
    2. // This works for Unity desktop as well:
    3. if(Input.GetMouseButtonDown(0))
    4. {
    5.    cowSprite.PlayAnim(0);
    6. }
    7.  
    Let's say, however, you're using touches and want to see if a specific button was pressed, and if it was, to trigger a run animation (animation of a character running) for as long as that button is being pressed:

    Code (csharp):
    1.  
    2. /* NOTE: myBtn is a reference to a GameObject that has a collider attached that I wish to use as a button: */
    3.  
    4. if(iPhoneInput.touchCount > 0)
    5. {
    6.    // Normally you would want to loop
    7.    // through all the touches available
    8.    // but this is for clarity's sake:
    9.  
    10.    touch = iPhoneInput.GetTouch(0);
    11.  
    12.    // Now test if the touch "hit" our button.
    13.    // NOTE: "hit" is an object of type RaycastHit.
    14.    // (NOTE: omit the "out" keyword if using JS):
    15.    if(Physics.Raycast(Camera.main.ScreenPointToRay(touch.position), out hit, Mathf.Infinity))
    16.    {
    17.       if(hit.collider.gameObject == myBtn)
    18.       {
    19.          // See if the animation needs to be started.
    20.          // NOTE: runAnim is a reference to the
    21.          // desired animation obtained by using
    22.          // sprite.GetAnim("run"):
    23.  
    24.          // This line checks to see if the desired
    25.          // animation is already the current one and
    26.          // if it is currently playing:
    27.          if(sprite.GetCurAnim() != runAnim || !sprite.IsAnimating())
    28.          {
    29.             sprite.PlayAnim("run");
    30.          }
    31.       }
    32.    }
    33. }
    34.  
    The check to see if the current animation is the desired one or if the sprite is currently animating is important for something like a looping run animation. Without this check, each frame that the button is held down, only the first frame of the run animation will be displayed since PlayAnim() will be called every frame, meaning it will constantly get re-started. This check ensures that if the animation is already set to play, it is allowed to run its course.

    Let me know if you have any questions.
     
  18. outtoplay

    outtoplay

    Joined:
    Apr 29, 2009
    Posts:
    741

    Brady,
    Thanks a bunch. I appreciate you taking the time to run through this. Hopefully other folk benefit as well.

    best,
    B.
     
  19. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    I really appreciate the PlayAnim example. I was just about to post a question about how to do play an animation, and lo and behold the answer was there!

    I have a question about SetWindingOrder. I have an animation that I have facing to the right when the GameObject moves to the right. But when the GameObject moves to the left I would like the GameObject to face to the left. I find I can change the orientation of the GameObject by changing the Winding in the Inspector. But I would like to be able to do this programmatically. So I created the following javascript code:

    Code (csharp):
    1. var SA_PS : PackedSprite;
    2. var SA_SB : SpriteBase;
    3.  
    4. var  cw = SA_SB.WINDING_ORDER.CW;
    5. var ccw = SA_SB.WINDING_ORDER.CCW;
    6.  
    7.      if(Input.GetMouseButtonDown(0) )
    8.      {
    9.        if( (SA_PS.GetCurAnim() != walkAnim) || !SA_PS.IsAnimating()){
    10.          SA_PS.SetWindingOrder(ccw);
    11.          SA_PS.PlayAnim(1);
    12.        }
    13.      }
    14.  
    But now when the MouseButton is down, the animation disappears. Can you tell me what I am missing?
     
  20. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    There is probably no need for me to mention this, but just to be safe incase I hadn't made something clear: just be sure that the animation you are playing with PlayAnim(1) contains what you're wanting, since an index of '1' refers to the second animation in the animation list (it is 0-based, since it is an array).

    But I'm betting the issue is being caused by changing the winding order. The only reason you would want to change the winding order is if your camera begins to face the other side of the sprite. It won't change the orientation of the sprite, just the side that is visible (if your shader has backface culling). It sounds like you're wanting the sprite to appear to face a different direction (left vs. right). In that case, I would recommend simply inverting the sprite's scale on the axis in question. So if my camera is looking down the Z-axis and I want my little character to face the opposite direction along the X-axis, I'd just set my character's scale to -1,1,1 (thus reversing the scale on the X-axis).
     
  21. Django

    Django

    Joined:
    Nov 25, 2008
    Posts:
    120
    Hello,

    I have a question regarding SM1 and SM2. Right now I am using SM1 but I am considering switching to SM2.

    I have a sprite that needs to be rotated as part of an animation (with the Unity animation editor) but the pivot point needs to be at a specific place because I don't want the object rotating on itself at its center which is its default place. I know this is on a wish list for Unity but I was wondering if it is possible to do that with SM1 or SM2 ?

    With 3D modeling software you can burn the pivot point where you want before importing in Unity. Using the SM, it is however impossible to do so. I have found a workaround for this but I lose texture space. The idea is to include a number of blank (transparent) pixels in the texture, so that the texture is larger in reality but you don't see it so the pivot point is not at the center.
     
  22. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Actually, if transform animation is the only consideration (and not sprite animation - i.e. the changing of "frames"), then this can be accomplished using both SM1 and SM2 using the "offset" property. Offset tells the sprite to create its geometry offset in local space from the GameObject's origin. That should accomplish what you're wanting.

    Also, you could do this by making the GameObject with the attached sprite a child of another GameObject, offsetting it a bit, and then rotate the parent.

    The benefit in SM2 is that the anchor point can be used for sprite animation along with auto-sizing to allow non-uniform sprite frames and allow resizing without stretching/warping.
     
  23. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    Animation 0 is 'idle', animation 1 is 'walk'.

    If I change the winding number before play and hit play, then the sprite disappears. If I change the winding number while playing, then the sprite reverses direction and faces left.

    I tried changing the X scale of the GameObject's transform from 3 to -3, but then the sprite disappears. Any ideas why?

    I also tried changing the X scale of the GameObject's transform from 3 to -3 while the scene was playing, and Unity wouldn't let me change the X scale to a negative value. Any ideas why?
     
  24. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    The confusion is probably occurring because Unity often does not update the scene or game views with the latest information until something is changed, so it is often 1 step behind. For example, if you change the winding order, nothing may appear to happen, then if you change the scale, it will suddenly apply the winding order change, causing the sprite to dissapear even though the latest thing you changed was the scale. So if it is disappearing when you change the scale it is probably because it is applying something else (like a winding order change) late. It's sort of a Unity quirk.

    Try keeping everything set to default (or whatever winding order works for you out of the box). Then just change the X scale and then hit "Play". That should reverse the sprite. There's no reason why Unity shouldn't let you change the X scale to a negative number at runtime, unless something somewhere else is constantly re-setting the transform separately, immediately undoing your changes.
     
  25. Django

    Django

    Joined:
    Nov 25, 2008
    Posts:
    120
    Thanks for the help!
     
  26. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    I am running into the following error when trying to use the sprite timeline functionality. The window launches but doesnt look correct and none of the fields are editable. I can pretty much just play the animation.

    Code (csharp):
    1. ----SpriteTimelineEditor:ShowWindow() (at Assets/Editor/SpriteTimelineEditor.cs:109)
    2. Unable to find required resource at 'Editor Default Resources/Builtin Skins/Lucida Grande.ttf'
    Did I not create my project correctly to include all the resources it needs?

    This is iPhone 1.5.1
     
  27. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    That messages is the result of a Unity bug. For some reason, Unity can't find its own built-in font when using Editor GUI stuff. It can be safely ignored, and hopefully they'll have it fixed in the next iPhone release.
     
  28. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    ok but how does that explain the odd looking animation timeline window?

    I cant change the framerate or length of the aniation in the animation window. Nor can I duplicate/add frames.
     
  29. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Sorry, I somehow missed that part. Can you send me a sample project via PM or e-mail? I can't think of a reason why that would be happening right off.
     
  30. hxx2010

    hxx2010

    Joined:
    Aug 31, 2009
    Posts:
    155
    SM2 - 2D
    Is there js version of the?
     
  31. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    hxx2010:
    I'm not 100% sure what you're asking. While SM2 is written in C#, it can be called from JS just fine, and there is a JS sample script which demonstrates most of the features and how to use them. I hope that helps answer your question. If not, please let me know.
     
  32. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    In order to give you my project do i just zip up the root folder?
     
  33. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    You could do that, or you can go to Assets->Export Package.
     
  34. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    ok well its telling me my attachment folder is full on the forums so where can i email you the unityPackage?
     
  35. hxx2010

    hxx2010

    Joined:
    Aug 31, 2009
    Posts:
    155
    The second animation("Shock") does not play

    Code (csharp):
    1.  
    2.     var funkySprite : Sprite;
    3.  
    4.     function Start ()
    5.     {
    6.         funkySprite.SetAnimCompleteDelegate(SwimmingAnimDone);
    7.         funkySprite.PlayAnimInReverse("Swimming");
    8.         InvokeRepeating("PauseUnpauseFunkyAnim", 1, 1);
    9.  
    10.     }
    11.  
    12.  
    13.     function SwimmingAnimDone()
    14.     {
    15.         // Let us know when the munching is done:
    16.         funkySprite.SetAnimCompleteDelegate(ShockAnimDone);
    17.        
    18.         // Switch to the munching animation:
    19.         funkySprite.PlayAnim("Shock");
    20.     }
    21.  
    22.     function ShockAnimDone()
    23.     {
    24.         funkySprite.SetAnimCompleteDelegate(SwimmingAnimDone);
    25.         funkySprite.PlayAnimInReverse("Swimming");
    26.     }
    27.  
    28.     function PauseUnpauseFunkyAnim()
    29.     {
    30.         if (funkySprite.IsAnimating())
    31.             funkySprite.PauseAnim();
    32.         else
    33.             funkySprite.UnpauseAnim();
    34.     }
    35.  
     

    Attached Files:

  36. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    ^ I think it might, you have it set to 100 frames per second =P
     
  37. hxx2010

    hxx2010

    Joined:
    Aug 31, 2009
    Posts:
    155
    element 0 : frmerate 1
    element 1 : frmerate 1


    In this case, can play the second animation, but has been circulating, unable to return to the first animation
     
  38. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,364
    I would pay lot of money for some sort of enhanced Sprite GUI! Something similar to that SM2 with sliders, buttons, checkboxes, textboxes, etc!
    Any plans in the future for this?
    :)
     
  39. hxx2010

    hxx2010

    Joined:
    Aug 31, 2009
    Posts:
    155
    Run the game, how to modify the animation framerate in the script


    Code (csharp):
    1. funkySprite.framerate = 15;
    This error
     
  40. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    Lokken:
    It's the support e-mail address you received with your order. I'll PM it to you to make sure you have it.

    hxx2010:
    From what I can see here, it should be playing. But I suspect the problem may be a carriage return or other such character in the animation name. It was recently discovered that EditorGUI.TextField, unlike GUI.TextField, has a bug that causes it to allow multi-line input even though it's only supposed to be a single-line control. That means if you were entering an animation name and pressed "enter" thinking that would commit the change, it actually added a carriage return to the name. So when you later reference it in-code using "Shock", it won't match since there's also a carriage return character in the string. Try re-naming the animations in question and be sure not to press enter. I have a fix for this issue already implemented in my internal version which will be part of the next release.

    Also, the only reason I can think that it would continue circulating is if loop cycles was set to -1 (loop infinitely). However, that doesn't seem to be set. If the above suggestion doesn't work, please e-mail me a sample project and I'll take a look at it.

    As for the framerate issue, the framerate is specified per-animation rather than per-sprite. And currently, there isn't a way to just change the animation's framerate while it is playing. However, you can change an animation's framerate and then call PlayAnim() again, and it will take effect. To modify an animation's framerate in-code, just get a reference to it like so:

    Code (csharp):
    1.  
    2. mySprite.GetAnim("My Anim").framerate = 20f;
    3.  
    tatoforever:
    Actually, I'm currently working on this very thing, and it's going to be really easy to use. I think you'll really like it. Stay tuned. :)
     
  41. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    No worries, I didnt even think to check that email.

    I have sent the unityPackage to the email that you referenced.

    Thanks!
     
  42. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,364
    Omg, we are working on a big title (Forgotten Memories)
    And it would be nice to integrate that GUI system on this project. Any approximate release date? :)
    Cheers,
     
  43. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    No release date at the moment, but I'm hoping within the next month.
     
  44. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,364
    Great news, this GUI system will be a nice addition to Unity, suffering right now this kind of feature.
    Cheers,
     
  45. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    Will that be part of SM2 or a separate purchase?
     
  46. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    The GUI system will probably be a separate add-on.
     
  47. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    @Brady:

    Currently in SM2 the available anchors are [UPPER, MIDDLE, BOTTOM] x [LEFT, CENTER, RIGHT]. Is it possible to have the anchor in positions other than these 9 combinations?
     
  48. tau

    tau

    Joined:
    Dec 15, 2008
    Posts:
    113
    Brady,

    I'm using PackedSprite v1.0 RC5 and noticed a weird timing issue with sprite animation: the frame per second animation speeds up twice during the gameplay but shows fine (normal defined speed) in the Sprite Timeline editor. Just wanted to give a heads up, seems to be happening from time to time...
     
  49. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,474
    aerende:
    Currently, those are the only supported anchor points. There is also the offset feature which can often accomplish the same goal, however. And I have one user who easily modded SM2 to use 0-1 values for the anchor point. This is actually a modification that is on my priority ToDo list for an up-coming release.

    tau:
    Could you send me a sample project that demonstrates this behavior with instructions to reproduce it? If so, please e-mail it to the support address on your order confirmation and I'll take a look at it.
     
  50. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    Is there a way to ensure that two separate PackedSprites animate in complete unison?

    I have a body and eyes that both have the same 'jump' animation. Same frame number. Same FPS setting. But playing them continuously leads to them getting out of sync with eachother.

    My current method is just checking to see if IsAnimating is true for each and if both aren't animating i set them both to play the animation again.