Jump to content

SpriterDotNet.Unity


loodakrawa

Recommended Posts

UnitySpriterAnimator.cs

protected override void ApplySpriteTransform(Sprite sprite, SpriterObject info)
{
    GameObject child = childData.Sprites[index];
    GameObject pivot = childData.SpritePivots[index];
    child.SetActive(true);
    pivot.SetActive(true);
    SpriteRenderer renderer = renderers[index];

    float ppu = sprite.pixelsPerUnit;

    renderer.sprite = sprite;
    Vector3 size = sprite.bounds.size;
    float spritePivotX = sprite.pivot.x / ppu / size.x;
    float spritePivotY = sprite.pivot.y / ppu / size.y;

    float deltaX = (spritePivotX - info.PivotX) * size.x * info.ScaleX;
    float deltaY = (spritePivotY - info.PivotY) * size.y * info.ScaleY;

    renderer.color = new Color(1.0f, 1.0f, 1.0f, info.Alpha);
    pivot.transform.localEulerAngles = new Vector3(0, 0, info.Angle);
    pivot.transform.localPosition = new Vector3(info.X / ppu, info.Y / ppu, 0);
    child.transform.localPosition = new Vector3(deltaX, deltaY, child.transform.localPosition.z);
    child.transform.localScale = new Vector3(info.ScaleX, info.ScaleY, 1);

    ++index;
}

Why here need this line?

renderer.color = new Color(1.0f, 1.0f, 1.0f, info.Alpha);

This line resets the color of the component Sprite Renderer.

But even if I delete this line, then when I swapping the image (just two eyes and adding 4 stars) seen such strangeness:566efa86f306e_2015-12-1422-12-09.png.07f

Source:

566efa87e5183_2015-12-1422-20-11.png.0d4

Bomber.scml

Link to comment
Share on other sites

4 hours ago, GolfNorth said:

UnitySpriterAnimator.cs

Why here need this line?


renderer.color = new Color(1.0f, 1.0f, 1.0f, info.Alpha);

This line resets the color of the component Sprite Renderer.

This line sets the alpha value from spriter. However, it does reset the colour and that's wrong. I'll fix it to just set the alpha without changing the RGB values.

Link to comment
Share on other sites

Hello,

I bought Spriter Pro and created a Box to test the BoxCollider2D in Unity. However, after imported to Unity the BoxCollider2D generated seem always disabled due to a too small size (~0.00001). I guess this is intentional, to enable only BoxCollider2Ds that are being used in the current playing animation. But, even after switching the Animation to the one with the Box the BoxCollider2D size is still the same.

Any help would be appreciated :D.

Link to comment
Share on other sites

Hi,

I'm new to Spriter and even more to the Unity plugin. I was wondering if it's possible to play more than one animation at the same time if those animations don't affect the same bones ? To be honest I ask this question because I'm used to work with Spine and it's a feature that I really like. 

About Spriter, is it possible to create nested animations ? If not, is it possible to create separate animations, instantiate both in Unity and then make one animation follow another animation bone ?

Thank you

Link to comment
Share on other sites

3 hours ago, WormLice said:

Hello,

I bought Spriter Pro and created a Box to test the BoxCollider2D in Unity. However, after imported to Unity the BoxCollider2D generated seem always disabled due to a too small size (~0.00001). I guess this is intentional, to enable only BoxCollider2Ds that are being used in the current playing animation. But, even after switching the Animation to the one with the Box the BoxCollider2D size is still the same.

Any help would be appreciated :D.

Hi,

It seems that this happens due to a bug in Spriter where the size of the rectangle has a negative value. Take a look here. If this is what's happening in your case you can manually edit the scml as a quick fix and remove the minus from the offending entry. If this is caused by something else, let me know and I'll investigate.

Link to comment
Share on other sites

3 hours ago, BTA said:

Hi,

I'm new to Spriter and even more to the Unity plugin. I was wondering if it's possible to play more than one animation at the same time if those animations don't affect the same bones ? To be honest I ask this question because I'm used to work with Spine and it's a feature that I really like. 

About Spriter, is it possible to create nested animations ? If not, is it possible to create separate animations, instantiate both in Unity and then make one animation follow another animation bone ?

Thank you

SpriterDotNet allows to blend any 2 animations as long as the hierarchy is identical.

There is a feature called Sub Entities that allows you to do exactly that and it's supported in SpriterDotNet. However, it is not well documented yet so I suggest to search the forum a little bit because I remember there was mention of it.

Link to comment
Share on other sites

2 hours ago, loodakrawa said:

Hi,

It seems that this happens due to a bug in Spriter where the size of the rectangle has a negative value. Take a look here. If this is what's happening in your case you can manually edit the scml as a quick fix and remove the minus from the offending entry. If this is caused by something else, let me know and I'll investigate.

It seems the negative width/height is intentional, not a bug. It's basically the direction the scaling goes.
Here's what happens when I set all widths/heights to positive:
X33NJuF.png
It should be pretty easy to fix through.
EDIT: Fixed. Changed the ApplyBoxTransform method at UnitySpriterAnimator.cs to:

protected override void ApplyBoxTransform(SpriterObjectInfo objInfo, SpriterObject info)
        {
            GameObject child = childData.Boxes[boxIndex];
            GameObject pivot = childData.BoxPivots[boxIndex];
            child.SetActive(true);
            pivot.SetActive(true);

            float w = objInfo.Width / DefaultPPU;
            float h = objInfo.Height / DefaultPPU;

            BoxCollider2D collider = child.GetComponent<BoxCollider2D>();

            collider.size = new Vector2(Mathf.Abs(w), Mathf.Abs(h));

            child.name = objInfo.Name;

            float deltaX = (DefaultPivot - info.PivotX) * w * info.ScaleX;
            float deltaY = (DefaultPivot - info.PivotY) * h * info.ScaleY;

            pivot.transform.localEulerAngles = new Vector3(0, 0, info.Angle);
            pivot.transform.localPosition = new Vector3(info.X / DefaultPPU, info.Y / DefaultPPU, 0);
            child.transform.localPosition = new Vector3(deltaX, deltaY, child.transform.localPosition.z);
            child.transform.localScale = new Vector3(info.ScaleX * w >= 0.0f ? 1.0f : -1.0f, info.ScaleY * h >= 0.0f ? 1.0f : -1.0f, 1);
            ++boxIndex;
        }

 

Link to comment
Share on other sites

2 hours ago, WormLice said:

It seems the negative width/height is intentional, not a bug. It's basically the direction the scaling goes.
Here's what happens when I set all widths/heights to positive:
X33NJuF.png
It should be pretty easy to fix through.
EDIT: Fixed. Changed the ApplyBoxTransform method at UnitySpriterAnimator.cs to:


protected override void ApplyBoxTransform(SpriterObjectInfo objInfo, SpriterObject info)
        {
            GameObject child = childData.Boxes[boxIndex];
            GameObject pivot = childData.BoxPivots[boxIndex];
            child.SetActive(true);
            pivot.SetActive(true);

            float w = objInfo.Width / DefaultPPU;
            float h = objInfo.Height / DefaultPPU;

            BoxCollider2D collider = child.GetComponent<BoxCollider2D>();

            collider.size = new Vector2(Mathf.Abs(w), Mathf.Abs(h));

            child.name = objInfo.Name;

            float deltaX = (DefaultPivot - info.PivotX) * w * info.ScaleX;
            float deltaY = (DefaultPivot - info.PivotY) * h * info.ScaleY;

            pivot.transform.localEulerAngles = new Vector3(0, 0, info.Angle);
            pivot.transform.localPosition = new Vector3(info.X / DefaultPPU, info.Y / DefaultPPU, 0);
            child.transform.localPosition = new Vector3(deltaX, deltaY, child.transform.localPosition.z);
            child.transform.localScale = new Vector3(info.ScaleX * w >= 0.0f ? 1.0f : -1.0f, info.ScaleY * h >= 0.0f ? 1.0f : -1.0f, 1);
            ++boxIndex;
        }

 

The fix is not really complex but it seems that's really a bug. Take a look here. If this becomes resolved as desired behaviour, I'll fix it in the library.

Link to comment
Share on other sites

Quote

EDIT:The problem of the boxes happens when you draw the box with the alt from the bottom side to the top, i already reported it

Thanks for the help!

http://brashmonkey.com/forum/index.php?/topic/4166-spriterdotnet-an-implementation-for-all-c-frameworks/&do=findComment&comment=14302

It does seem like a bug. The Box origin changes depending on the direction you set the width/height of the Box. It might be intentional too, it's hard to tell. The code that I sent you before should work in all the cases. In my opinion, "fixing" this method in the library would be a good idea, to support scml from versions that have the apparent bug. I don't think that this fix will break anything in the future.

P.S: Is there a practical way of getting a collider by name?

EDIT: When width/height is negative the Boxes seem to be a little offset. Not sure why that happens.

Edited by WormLice
Link to comment
Share on other sites

6 hours ago, WormLice said:

http://brashmonkey.com/forum/index.php?/topic/4166-spriterdotnet-an-implementation-for-all-c-frameworks/&do=findComment&comment=14302

It does seem like a bug. The Box origin changes depending on the direction you set the width/height of the Box. It might be intentional too, it's hard to tell. The code that I sent you before should work in all the cases. In my opinion, "fixing" this method in the library would be a good idea, to support scml from versions that have the apparent bug. I don't think that this fix will break anything in the future.

P.S: Is there a practical way of getting a collider by name?

EDIT: When width/height is negative the Boxes seem to be a little offset. Not sure why that happens.

If that were a feature it would be an easy fix. This way I'm not sure so I'd like to avoid weird behaviour (like the small offset you mention).

As for getting colliders - I'm not using them in my projects so I haven't tried in a real use-case. I'd like to hear your feedback in this regard so I can make it more user friendly. I guess that ATM the easiest way of getting the collider is using Unity's Find functionality but I'm not sure how that affects performance. I have a couple of potential ideas but I'd like to hear your feedback / suggestions if you have any.

Link to comment
Share on other sites

8 minutes ago, loodakrawa said:

If that were a feature it would be an easy fix. This way I'm not sure so I'd like to avoid weird behaviour (like the small offset you mention).

As for getting colliders - I'm not using them in my projects so I haven't tried in a real use-case. I'd like to hear your feedback in this regard so I can make it more user friendly. I guess that ATM the easiest way of getting the collider is using Unity's Find functionality but I'm not sure how that affects performance. I have a couple of potential ideas but I'd like to hear your feedback / suggestions if you have any.

Here are some ideas (not only for colliders):
- Animation Start event
- Practical way to wait for animation to end before starting a new one
- Automatic BoxCollider2D generation
- Add some examples to the Github project for each fearture shown
- Name the Sprites/Boxes to their name in Spriter (so you can use the Find method)

Link to comment
Share on other sites

35 minutes ago, WormLice said:

Here are some ideas (not only for colliders):
- Animation Start event
- Practical way to wait for animation to end before starting a new one
- Automatic BoxCollider2D generation
- Add some examples to the Github project for each fearture shown
- Name the Sprites/Boxes to their name in Spriter (so you can use the Find method)

- Animation Start event - I'll add it

- SpriterAnimator has an exposed event for handling Animation end (AnimationFinished). Or did you have something else in mind?

- BoxColliders2D do get generated for every box in Spriter. They sit under Metadata/Boxes

- The GitHub unity example package has examples of every feature supported

- Boxes do get their name set as in Spriter (although this still needs a small fix to ensure consistency). Sprites on the other hand don't get names since the library reuses them between animations (potentially changing names - e.g. the sprite used for rendering "head" might be used for rendering the "torso" in another animation). Just curious - why would you need to find them by name?

Link to comment
Share on other sites

On 12/21/2015 at 8:59 PM, loodakrawa said:

- SpriterAnimator has an exposed event for handling Animation end (AnimationFinished). Or did you have something else in mind?

A method that plays an animation and make sure the animation is finished playing N times before starting a new one (after calling Play/Transtion again).

On 12/21/2015 at 8:59 PM, loodakrawa said:

BoxColliders2D do get generated for every box in Spriter. They sit under Metadata/Boxes

I wasn't very clear, sorry. I meant creating them based on the Sprite image width/height, as an option when importing the scml. That would save a lot of time creating boxes in Spriter.

On 12/21/2015 at 8:59 PM, loodakrawa said:

- The GitHub unity example package has examples of every feature supported

Adding some snippets to README.md would be a nice idea.

On 12/21/2015 at 8:59 PM, loodakrawa said:

- Boxes do get their name set as in Spriter (although this still needs a small fix to ensure consistency). Sprites on the other hand don't get names since the library reuses them between animations (potentially changing names - e.g. the sprite used for rendering "head" might be used for rendering the "torso" in another animation). Just curious - why would you need to find them by name?

It would be useful to change animations dynamically.

An idea I had recently:
- Ability to play different animations simultaneously (eg. arm.Play("A0"); body.Play("A1");)

Edited by WormLice
Link to comment
Share on other sites

Hi,

I just downloaded the Unity plugin and copy-past my Spriter project into my Unity project. The plugin did generated a prefab as expected but when I try to get a reference to the SpriterDotNetBehaviour Animator property it returns null. My code is pretty simple :

public class DevilBoyController : MonoBehaviour {

	private UnitySpriterAnimator spriterAnimator;

	// Use this for initialization
	void Start () {
		
		SpriterDotNetBehaviour sdnb = gameObject.GetComponentInChildren<SpriterDotNetBehaviour>();
		
		spriterAnimator = sdnb.Animator;
		
		spriterAnimator.Play("Attack1");
	}
}

The script is attached to a game object which contains a prefab instance as a child. The sdnb variable it's not null - it means that the component is correctly retrieved.

Any idea why the Animator is null ?

Thank you

 

EDIT : actually this issue was solved by using the same tips as the previous issue I had - Reimporting the assets ! I don't know why it just does not work the first time - but in my case I had to use the "reimport" option twice in order to have the character correctly displayed and the Animator correctly created.

Link to comment
Share on other sites

10 hours ago, BTA said:

Hi,

I just downloaded the Unity plugin and copy-past my Spriter project into my Unity project. The plugin did generated a prefab as expected but when I try to get a reference to the SpriterDotNetBehaviour Animator property it returns null. My code is pretty simple :


public class DevilBoyController : MonoBehaviour {

	private UnitySpriterAnimator spriterAnimator;

	// Use this for initialization
	void Start () {
		
		SpriterDotNetBehaviour sdnb = gameObject.GetComponentInChildren<SpriterDotNetBehaviour>();
		
		spriterAnimator = sdnb.Animator;
		
		spriterAnimator.Play("Attack1");
	}
}

The script is attached to a game object which contains a prefab instance as a child. The sdnb variable it's not null - it means that the component is correctly retrieved.

Any idea why the Animator is null ?

Thank you

 

EDIT : actually this issue was solved by using the same tips as the previous issue I had - Reimporting the assets ! I don't know why it just does not work the first time - but in my case I had to use the "reimport" option twice in order to have the character correctly displayed and the Animator correctly created.

That probably happens due to the script execution order - take a look here.

Link to comment
Share on other sites

Found a potential issue. The animations seem to not be starting from the beginning after switching to another animation while the current is playing and switching back to the last animation.

EDIT: Setting Progress to 0.0f before staring a new Animation worked.

Edited by WormLice
Link to comment
Share on other sites

4 hours ago, WormLice said:

Found a potential issue. The animations seem to not be starting from the beginning after switching to another animation while the current is playing and switching back to the last animation.

EDIT: Setting Progress to 0.0f before staring a new Animation worked.

That's exactly what I'm doing when playing a new animation:
https://github.com/loodakrawa/SpriterDotNet/blob/master/SpriterDotNet/SpriterAnimator.cs#L150

Did you mean while transitioning (blending from one to the other)?

Link to comment
Share on other sites

2 minutes ago, loodakrawa said:

That's exactly what I'm doing when playing a new animation:
https://github.com/loodakrawa/SpriterDotNet/blob/master/SpriterDotNet/SpriterAnimator.cs#L150

Did you mean while transitioning (blending from one to the other)?

I'm calling the Transtion method. What I meant is that after quickly switching through them the animation seems plays faster.

Link to comment
Share on other sites

On 21/12/2015 at 10:23 AM, WormLice said:

A method that plays an animation and make sure the animation is finished playing N times before starting a new one (after calling Play/Transtion again).

I wasn't very clear, sorry. I meant creating them based on the Sprite image width/height, as an option when importing the scml. That would save a lot of time creating boxes in Spriter.

Adding some snippets to README.md would be a nice idea.

It would be useful to change animations dynamically.

An idea I had recently:
- Ability to play different animations simultaneously (eg. arm.Play("A0"); body.Play("A1");)

Just realised I haven't answered yet to this:

You should be able to achieve that behaviour by using the AnimationFinished event. It gets triggered when a non-looping animation finishes and a looping one loops.

About boxes - I'll add the option to the importer to generate boxes for every sprite. Cool suggestion!

I will expand the documentation along with the development but for some things it's much easier to just provide code examples than to actually describe it.

Changing animations dynamically - I just added support for changing the data source for the animator. Take a look here + the two implementations.

It is possible to play multiple animations simultaneously but not on such a granular level - only entire animations.

 

4 minutes ago, WormLice said:

I'm calling the Transtion method. What I meant is that after quickly switching through them the animation seems plays faster.

The transition method doesn't reset the current animation on purpose but transitions to the next one from the current time. Otherwise, if you try transitioning from the middle of an animation and it resets immediately it's not smooth - it just "jumps" to the starting point.
Let me get this straight - are you saying that after a couple of quick transitions the animation just speeds up? Even after you stop transitioning?

Link to comment
Share on other sites

32 minutes ago, loodakrawa said:

You should be able to achieve that behaviour by using the AnimationFinished event. It gets triggered when a non-looping animation finishes and a looping one loops.

Just got that working :D.

32 minutes ago, loodakrawa said:

About boxes - I'll add the option to the importer to generate boxes for every sprite. Cool suggestion!

Cool! Btw, it would be nice for the user to be able to choose which sprites he wants collision on and the ones he don't.

32 minutes ago, loodakrawa said:

I will expand the documentation along with the development but for some things it's much easier to just provide code examples than to actually describe it.

Awesome news :D.

32 minutes ago, loodakrawa said:

It is possible to play multiple animations simultaneously but not on such a granular level - only entire animations.

Didn't know it's possible to play multiple animations simultaneously already. How would I do so, even in higher levels?

32 minutes ago, loodakrawa said:

Changing animations dynamically - I just added support for changing the data source for the animator. Take a look here + the two implementations.

Awesome I'll try it out and let you know how it goes.

 

32 minutes ago, loodakrawa said:

The transition method doesn't reset the current animation on purpose but transitions to the next one from the current time. Otherwise, if you try transitioning from the middle of an animation and it resets immediately it's not smooth - it just "jumps" to the starting point.
Let me get this straight - are you saying that after a couple of quick transitions the animation just speeds up? Even after you stop transitioning?

I think I got it now. It's not really a bug. Since the animation doesn't get the progress reset the transition happens to the progress of the current animation, making the animation seem to play faster.

Keep up the awesome work :).

Link to comment
Share on other sites

I tried the GetFrameData method but it doesn't seen to be doing anything at all.
I'm probably using it wrong, but I'm clueless what is wrong.
Here's my current code:

if (BodyAnimator.CurrentAnimation.Name.Equals("Attack.W0"))
{
    Vector2 mousePosition = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
    BodyAnimator.DataProvider.GetFrameData(BodyAnimator.Time, Time.deltaTime, 0.0f, BodyAnimator.CurrentAnimation).SpriteData[5].Angle = Mathf.Acos(mousePosition.x / mousePosition.magnitude);
}

SpriteData[5] is the number that my sprite is located when I'm running the program playing my target animation.
The idea is setting the angle to the mouse position so the sprite always face the mouse.

Link to comment
Share on other sites

10 hours ago, WormLice said:

I tried the GetFrameData method but it doesn't seen to be doing anything at all.
I'm probably using it wrong, but I'm clueless what is wrong.
Here's my current code:


if (BodyAnimator.CurrentAnimation.Name.Equals("Attack.W0"))
{
    Vector2 mousePosition = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
    BodyAnimator.DataProvider.GetFrameData(BodyAnimator.Time, Time.deltaTime, 0.0f, BodyAnimator.CurrentAnimation).SpriteData[5].Angle = Mathf.Acos(mousePosition.x / mousePosition.magnitude);
}

SpriteData[5] is the number that my sprite is located when I'm running the program playing my target animation.
The idea is setting the angle to the mouse position so the sprite always face the mouse.

If you want to do something like this I suggest extending the DefaultAnimationDataProvider, setting it on the Animator.DataProvider and overriding the GetFrameData method and modifying the data before returning it.

Link to comment
Share on other sites

  • 2 weeks later...

So this seems to have gone by me completely unnoticed. How does this tool compare to my Spriter2Unity?

It doesn't use MecAnim? I wonder if this is preferable over Spriter2Unity or whether the two tools are so different that different people might prefer one over the other.

Does it suffer from any of the stupid bugs and issues that Spriter2Unity suffers from?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...