Spine Skeletal Component References

The Spine Skeleton components support the data format exported by the official Spine tools, and renders and plays Spine assets.

spine

Select the node and choose Add Component -> Spine -> Skeleton on the Inspector panel to add the Skeleton component to the node.

  • For information on using the Spine Skeleton component, please refer to the Spine Skeleton example case for details.
  • For Spine Skeleton component related scripting interfaces, please refer to the Spine Skeleton API for details.

Spine Properties

PropertyDescription
CustomMaterialCustom materials that can be used to achieve rendering effects such as dissolve, external glow, etc. Please refer to the Custom Materials documentation for details.
ColorSet the skeleton animation color.
SkeletonDataSkeleton information data, drag and drop the skeleton assets exported from Spine. into this property
Default SkinSelect the default skin.
AnimationThe name of the currently playing animation.
Animation Cache ModeRendering mode, the default is REALTIME mode.
1. REALTIME mode, real-time computing, supports all Spine features.
2. SHARED_CACHE mode, caches, and shares skeletal animations and texture data, equivalent to pre-baked skeletal animations. SHARED_CACHE mode has higher performance, but does not support motion fusion, motion overlay, and only supports motion start and end events. As for memory, when creating N (N>=3) animations with the same skeleton and the same action, the memory advantage is obvious. In summary, SHARED_CACHE mode is suitable for scene animations, effects, replica monsters, NPCs, etc., and can greatly improve frame rates and reduce memory consumption.
3. PRIVATE_CACHE mode, similar to SHARED_CACHE, but does not share animation and texture data, and will occupy extra memory, there is only a performance advantage, and it may cause stutter if using this mode a lot to play animation. Use PRIVATE_CACHE to take advantage of the high performance of the cache mode and to also implement texture replacement (which cannot share the texture data).
LoopWhether to loop the current animation.
Premultiplied AlphaWhether to enable premultiplied alpha for the image, default is True.
This item needs to be disabled when the transparent area of the image appears as a color block, and enabled when the translucent area of the image turns black.
Time ScaleThe time scale of all animations in the current skeleton.
Debug SlotsWhether to show debug information of slots.
Debug BonesWhether to show debug information of skeletons.
Debug MeshWhether to show debug information of mesh.
Use TintWhether to turn on the tinting effect, off by default.
SocketsUsed to attach certain external nodes to the specified skeleton joints. The value of the property indicates the number of attachment points. For details, please refer to the description below.

Notes:

  1. The Anchor and Size properties on the Node component in the Inspector panel are invalid when using the Skeleton component.
  2. The Spine Skeletal component is a UI rendering component, and the Canvas node is the rendering root for UI rendering, the node with this component must be a child of the Canvas node (or a node with a RenderRoot2D component) to be displayed properly in the scene.
  3. When using the Spine Skeleton component, since it has the UseTint property, its custom material needs to have two color information, otherwise the Spine staining effect may be wrong. Please refer to the builtin-spine.effect example to achieve this.

Spine ReplaceTexture

Here is an example of how Spine replaces the texture.

spine-cloth

  1. First, create a new Canvas node in the Hierarchy panel, and then create a new empty node and name it to girl under the Canvas node. Select the girl node and add the Skeleton component in the Inspector panel, drag and drop the asset into the SkeletonData property box of the Skeleton component. The Animation property of the Skeleton component can be changed to set the animation that the developer wants to play.

    spine-cloth

  2. Create a new TypeScript script and name it SpineSkin in the Assets panel to write the component’s script. The script code is as follows:

    ```ts import { _decorator, Component, sp } from ‘cc’; const { ccclass, property } = _decorator;

    @ccclass(‘SpineSkin’) export class SpineSkin extends Component {

  1. @property({ type:sp.Skeleton })
  2. spine: sp.Skeleton | null = null;
  3. skinId: number = 0;
  4. start () {
  5. // Your initialization goes here.
  6. sp.Skeleton }
  7. change() {
  8. const skins = ['girl', 'boy', 'girl-blue-cape', 'girl-spring-dress'].map(x=> `full-skins/${x}`);
  9. this.skinId = (this.skinId + 1) % skins.length;
  10. this.skin!.setSkin(skins[this.skinId]);
  11. }
  12. // update (deltaTime: number) {
  13. // // Your update function goes here.
  14. // }
  15. }
  16. ```
  1. Next, attach the SpineSkin script to the Canvas node, i.e. drag and drop the script into the Inspector panel of the Canvas node. Drag and drop the girl node in the Assets panel into the property boxes corresponding to the SpineSkin script component and save the scene.

    spine-jscom

  2. Next, use the Button component’s click event to trigger the change callback in the SpineSkin script to replace the texture by clicking the button.

    Create a new Button node under the Canvas node in the Assets panel and name it change_skin. Adjust its position, size, text display, and other properties as needed.

    Set the click event of the change_skin node in the Inspector panel, drag the Canvas node attached with the SpineSkin script component into the cc.Node property box of the ClickEvents property, specify script component as SpineSkin, and set the callback to change.

    spine-cloth

  3. Adjust the scene structure as needed, save the scene and click the Preview button at the top of the editor. Click the change skin button to see that the character skin has been replaced.

    spine-cloth

    Note: if the scene is not displayed when previewing, check that the Layer property of each node is consistent with that of the Camera node.

    layer

Spine Vertex Effect

The vertex effect is only available when Spine’s Animation Cache Mode is in the REALTIME mode. Here is an example of how to set the vertex effect in Spine.

  1. First, create a new Canvas node in the Hierarchy panel, and then create a new empty node under the Canvas node. Select the node and add the Skeleton component in the Inspector panel, drag and drop the asset to the SkeletonData property box of the Skeleton component, and set the Skeleton component properties.

  2. Create a new TypeScript script in the Assets panel and name it SpineExample, write the component script. The script code is as follows:

    1. import { _decorator, Component, sp } from 'cc';
    2. const { ccclass, property } = _decorator;
    3. @ccclass('SpineExample')
    4. export class SpineExample extends Component {
    5. @property({ type:sp.Skeleton })
    6. skeleton: sp.Skeleton | null = null;
    7. private _jitterEffect?:sp.VertexEffectDelegate;
    8. start () {
    9. this._jitterEffect = new sp.VertexEffectDelegate();
    10. // Set the jitter parameters.
    11. this._jitterEffect.initJitter(20, 20);
    12. // Call the 'setVertexEffectDelegate' method of the Skeleton component to set the effect.
    13. this.skeleton!.setVertexEffectDelegate(this._jitterEffect!);
    14. }
    15. };
  3. Next, attach the SpineExample script to the Canvas node, i.e. drag and drop the script into the node’s Inspector panel. Drag the Spine node with the Skeleton component attached in the Hierarchy panel to the corresponding Skeleton property box of the script component, and save the scene.

  4. Click the Preview button at the top of the editor to see the effect of vertex jitter of the Spine animation. For example, please refer to the SpineMesh example.

Spine Attachment

When using skeleton animation, nodes are often attached on a certain part of the skeleton animation to achieve the effect of linkage between the nodes and the skeleton animation.

Spine Attachments can be implemented by using both editor and script. Here is an example of how Spine uses an attachment to attach a star to the dragon’s tail, and shake it with the dragon’s tail.

attach0

Implementing via the editor

  1. First, create a new Canvas node in the Hierarchy panel, and then create a new empty node and name it to Spine under the Canvas node. Select Spine and add the Skeleton component to the Inspector panel. Drag and drop the asset into the SkeletonData property box of the Skeleton component and set the Skeleton component properties.

  2. Second, right-click on the Spine node in the Hierarchy panel and select Create -> 2D Objects -> Sprite to add a child node for it and name it star. Drag the star asset to the SpriteFrame property of the Sprite component in the Inspector panel.

    attach1

  3. Select the Spine node in the Hierarchy panel, and set the Skeleton component’s Sockets property to 1 in the Inspector panel (the value of the Sockets property represents the number of attachments).

    attach2

  4. Next, set the Path and Target properties of the Sockets. The Path drop-down box will list all the skeletons, select the target bone you want to attach, here take the dragon’s tail as an example, drag the star node to the Target property box. Notice the star attached on the dragon’s tail in the Scene panel.

    attach3

  5. Finally, save the scene and click the Preview button on top of the editor to see the star hanging on the dragon’s tail and swaying along with it. Please refer to the SpineAttach example for details.

Implementing via code

  1. The first two steps are the same as those implemented via the editor.

  2. Create a new TypeScript script in the Explorer panel and name it SpineAttach. Write the component script. The script code is as follows:

    1. import { _decorator, Component, sp, Label, Node } from 'cc';
    2. const { ccclass, property } = _decorator;
    3. @ccclass('SpineAttach')
    4. export class SpineAttach extends Component {
    5. @property({ type: sp.Skeleton })
    6. skeleton: sp.Skeleton = null!;
    7. @property({ type: Node })
    8. attachNode: Node = null!;
    9. start () {
    10. var socket = new sp.SpineSocket("root/hip/tail1/tail2/tail3/tail4/tail5/tail6/tail7/tail8/tail9/tail10", this.attachNode); // The first incoming parameter is the target bone of the attachment, and the second incoming parameter is the node of the attachment.
    11. this.skeleton!.sockets.push(socket);
    12. this.skeleton!.sockets = this.skeleton!.sockets;
    13. }
    14. }

    If the name of the target bone is unknown, set the Sockets property in the Skeleton component to 1 and then look for the name of the desired target bone in the Path drop-down box. When the search is complete, restore the Sockets property to 0.

  3. Next, attach the SpineAttach script to the Canvas node, i.e. drag and drop the script into the node’s Inspector panel. Drag and drop the Spine node with the Skeleton component attached and the star node in the Hierarchy panel to the Skeleton property box and the AttachNode property box of the script component, respectively, and save the scene.

    attach4

  4. Click the Preview button at the top of the editor to see the star hanging from the dragon’s tail and shaking along with the dragon’s tail.

    attach-ts

Spine Collision Detection

The Spine attachment function allows for the detection of a collision of a part of the skeleton animation. The following is an example of how Spine implements collision detection, by determining whether the character’s feet are in contact with the ground or not to dynamically change the ground color when the character is running.

collider

  1. First, set the 2D Physics System to Builtin 2D Physics System in the Project -> Project Settings -> Feature Cropping of the editor menu bar.

    collider

  2. Create the Spine node and its children (an empty node and named frontFoot), as well as the Sprite node as the ground (named Ground), and set the position, size, and other properties, as in the first two steps for the Spine attachment.

    collider

  3. Select the frontFoot node in the Hierarchy panel, click Add Component -> Physics2D -> Colliders -> Polygon Collider2D in the Inspector panel to add a collision component, and set the collision component parameters.

    collider

    Referring to steps 3 and 4 of the Spine attachment, attach the frontFoot node to the target bone (“foot” for example) of the Sprite node. The frontFoot node will then move along with the skeletal animation, and thus the collision component’s bounding box will be synchronized with the skeletal animation in real time.

    collider

  4. Select the Ground node in the Hierarchy panel, click Add Component -> Physics2D -> Colliders -> BoxCollider2D in the Inspector panel to add a collision component, and set the collision component parameters.

  5. Create a new TypeScript script in the Assets panel and name it SpineCollider. Then attach the script on the Ground node. The script code is as follows:

    1. import { _decorator, Component, Node, PhysicsSystem2D, Contact2DType, Collider2D, Color, Sprite, ParticleSystem2D, EPhysics2DDrawFlags } from 'cc';
    2. const { ccclass } = _decorator;
    3. @ccclass('SpineCollider')
    4. export class SpineCollider extends Component {
    5. touchingCountMap : Map < Node, number > = new Map;
    6. private debugDrawFlags : number = 0;
    7. start () {
    8. // Your initialization goes here.
    9. PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
    10. PhysicsSystem2D.instance.on(Contact2DType.END_CONTACT, this.onEndContact, this);
    11. this.debugDrawFlags = PhysicsSystem2D.instance.debugDrawFlags;
    12. }
    13. onEnable () {
    14. PhysicsSystem2D.instance.debugDrawFlags = this.debugDrawFlags | EPhysics2DDrawFlags.Shape;
    15. }
    16. onDisable () {
    17. PhysicsSystem2D.instance.debugDrawFlags = this.debugDrawFlags;
    18. }
    19. addContact (c: Collider2D) {
    20. let count = this.touchingCountMap.get(c.node) || 0;
    21. this.touchingCountMap.set(c.node, ++count);
    22. let sprite = c.getComponent(Sprite);
    23. if (sprite) {
    24. sprite.color = Color.RED;
    25. }
    26. }
    27. removeContact (c: Collider2D) {
    28. let count = this.touchingCountMap.get(c.node) || 0;
    29. --count;
    30. if (count <= 0) {
    31. this.touchingCountMap.delete(c.node);
    32. let sprite = c.getComponent(Sprite);
    33. if (sprite) {
    34. sprite.color = Color.WHITE;
    35. }
    36. } else {
    37. this.touchingCountMap.set(c.node, count);
    38. }
    39. }
    40. onBeginContact (a: Collider2D, b: Collider2D) {
    41. this.addContact(a);
    42. this.addContact(b);
    43. }
    44. onEndContact (a: Collider2D, b: Collider2D) {
    45. this.removeContact(a);
    46. this.removeContact(b);
    47. }
    48. }
  6. Click the Preview button at the top of the editor to see the effect. For details, please refer to the SpineCollider example.

    Note: the collision detection based on attachment has a delay of one frame due to the implementation mechanism of the attachment.

    collider