Skip to content

Instantly share code, notes, and snippets.

@sebgeelen
Created September 20, 2016 09:47
Show Gist options
  • Save sebgeelen/516d404e2b57d007ce5a14ee986a3713 to your computer and use it in GitHub Desktop.
Save sebgeelen/516d404e2b57d007ce5a14ee986a3713 to your computer and use it in GitHub Desktop.
Add attachment dynamicaly into spine : Canvas + WebGL
/// create an empty BitmapData of the desired Label size
_spinLabelData = new BitmapData(120, 60, true, 0x00000000);
/// create a Label of the same bounding size with the correct style and text
_spinLabel = new Label(_spinLabelData.width, _spinLabelData.height)
..text = spinTranslation
..style = getDefaultStyle()
..align = Align.center
..x = _spinLabelData.width/2
..y = _spinLabelData.height/2;
/// create a "out of scope" container to render the label correctly
_spinLabelContainer = new Sprite();
_spinLabelContainer.addChild(_spinLabel);
/// draw the container ( containing the label) in the bitmap data
_spinLabelData.draw(_spinLabelContainer);
/// name of the slot element in spine
/// + name of the new attachment we will create
String name = 'spin_texte';
/// create new Attachment
/// scaleX and ScaleY = 1.5 are necessary to compensate the 2/3 scale of the SkeletonAnimation object
RegionAttachment attachment = new RegionAttachment(name, _spinLabelData)
..width = _spinLabelData.width
..height = _spinLabelData.height
..scaleX = 1.5
..scaleY = 1.5
..updateUVs()
..updateOffset();
/// find index of the slot that will contain our attachment
int slotIndex = assets.skeleton.findSlotIndex(name);
/// add our attachment in the first skin of the spine
/// !! may not work if more than 1 skin
assets.skeleton
..findSlot(name).attachmentName = name
..skins.first.addAttachment(slotIndex, name, attachment);
/// create and addChild the skeleton animation
_spineAnim = new SkeletonAnimation(assets.skeleton)
..scaleX = 2 / 3
..scaleY = 2 / 3;
addChild(_spineAnim);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment