• Runtimes
  • 使用 SpinePlayer,如何获取动画的大小和位置?

我用 Spine player 一段时间了,在 Web 上展示 Spine 动画。

但是一直有个问题,Web 如何知道动画的大小?

  • 动画设计师不知道如何查看大小
  • Web 得到导出文件(json、atlas、png),也不知道动画的大小。

为什么想要知道动画的大小呢?因为 Spine 动画会在视口(https://zh.esotericsoftware.com/spine-player#%E8%A7%86%E5%8F%A3)中自动填充,如果 Spine container 设置的大,则动画就大,反之就小。但是我们想要的动画展示大小肯定是确定的。

我不知道编辑器中是否可以查看动画大小。谢谢解答!

Related Discussions
...

在使用 Spine Player 时,动画的大小和位置通常是通过视口(viewport)来控制的。Spine Player 会根据当前动画自动计算视口的大小,但你也可以手动设置一个固定的视口大小,以确保动画在不同的设备上显示一致。

要获取动画的大小,你可以在运行时通过计算动画的最大边界来实现。这通常需要遍历动画的每一帧,并计算每一帧的边界。Spine 的 Web Player 提供了一些工具和 API,可以帮助你在运行时计算动画的边界。

在编辑器中,动画的大小并不是直接显示的,因为它会根据骨骼的姿势和动画的不同而变化。然而,你可以通过在编辑器中调整骨骼的缩放比例来影响动画的显示大小。

如果你需要在 Web 上展示一个固定大小的动画,可以通过设置一个固定的视口大小来实现。这样,无论动画的实际大小如何,它都会被缩放以适应你指定的视口。

As Spinebot mentioned, the player determines the animation size by "calculating the maximum boundary of the animation at runtime." This function is used for that purpose.

If you need precise sizing, it's better to pass the viewport coordinates and dimensions to the player, as described here.

If you want an animation to have a specific size—for example, 1920x1080—you can add a 1920x1080 (or larger, but still 16:9) rectangle placeholder to your Spine project. This placeholder won't be exported.

Place the rectangle strategically so that it's easier later to pass the correct viewport coordinates and size to the player.

Animators should keep their work within this visual guide. Then, simply pass the visual guide's dimensions to the player's viewport settings. If the placeholder is centered on the origin, your x and y values will be -width/2 and -height/2.

This discussion might be useful too.