Hello Guest

Author Topic: Using Unity 4.3 Animator and 2dtk Sprite  (Read 6104 times)

DeCeOnline

  • Newbie
  • *
  • Posts: 6
    • View Profile
Using Unity 4.3 Animator and 2dtk Sprite
« on: March 01, 2014, 11:00:51 am »
Hi there!

I'm developing a simple Super Mario with 2D Toolkit, using a tilemap and sprites. I'm trying to figure out the best way to combine Unity functions with 2D Toolkit functions. I've read that tiles can't be animated and I'm using sprites to animate certain blocks (like when you push a block containing a mushroom). I'm used to Unity Animator panel and its parameters in order to switch from one animation to another and what I want to know if is there any way to keep using these Unity functions and mix them with the tiles. My main problem is that the spritesheet is not an Unity Sprite so I can't drag the sprites to the Animation panel and I have to use 2DTK Sprite with Animation instead (and the Animation editor too). I should switch from one animation to another by scripts and if I can avoid that it would be more efficient, imo. ∫

Another question is... what's the best practice to follow when I want an specific animation looping? For example, I want a waterfall looping in the background. What's the best less memory consuming practice? 

Thank you!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Using Unity 4.3 Animator and 2dtk Sprite
« Reply #1 on: March 01, 2014, 07:05:17 pm »
You pretty much can't use the unity animator for c# properties just variables. It has been an issue since it was introduced but still isn't resolved. The animator only works for variables, and to get that to work with tk2d would mean sacrificing a whole load of performance. I have posted a few adapter class examples on the forum which will make it work better trading off performance. tk2d works best with a script controller.

About a waterfall - it depends on the size, if its a large image, just create a looping animator that plays on start. Dicing could help reduce atlas sizes, but trades of a little bit of performance. Should be fine if there is only one waterfall though.

TOO DX

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Using Unity 4.3 Animator and 2dtk Sprite
« Reply #2 on: August 05, 2014, 08:15:14 pm »
I wasn't able to find your adapter classes, so I wrote one myself. Here's an example:

Code: [Select]
[ExecuteInEditMode]
[RequireComponent (typeof(tk2dBaseSprite))]
public class tk2dUnityAnimatorSpriteHook : MonoBehaviour {

[SerializeField]
tk2dBaseSprite baseSprite;

public Color animColor;
public float scaleX;
public float scaleY;

public void Update(){
if(baseSprite.color != animColor){
baseSprite.color = animColor;
}
if(baseSprite.scale.x != scaleX && baseSprite.scale.y != scaleY){
baseSprite.scale = new Vector2(scaleX, scaleY);
}
}
}

I'm curious of the performance implications of this. Is there a better way to write this that's a 2dtk way? At the very least, it allows me to animate using the curve editor, which is far preferred to me over using HOTween.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Using Unity 4.3 Animator and 2dtk Sprite
« Reply #3 on: August 05, 2014, 10:33:36 pm »
If you're worried about performance, you can enable and disable the adapter when you need it... adding an update will cost some perf, but it all depends on how many of these are in play... As an optimization if you have loads of these and not all of them are visible, you can use OnBecameVisible / OnBecameInvisible to turn the behaviour on and off.

DarkSlash

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Using Unity 4.3 Animator and 2dtk Sprite
« Reply #4 on: May 14, 2015, 07:55:32 pm »
How should I use this class?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Using Unity 4.3 Animator and 2dtk Sprite
« Reply #5 on: May 14, 2015, 11:14:47 pm »
Drop the behaviour on the sprite, attach the sprite and animate the color property on this instead of the sprite.