DragonBones / DragonBonesCSharp

DragonBones C# Runtime
MIT License
537 stars 198 forks source link

Proper way to change DisplayIndex via API #31

Closed nanopony closed 7 years ago

nanopony commented 7 years ago

Hello!

I am wondering what is the proper way to change DisplayIndex of a slot via API. For instance, I have several hairstyles or hats for my character and would like to swap them on the fly via code. Intuitively I use

_hairstyleSlot.displayIndex = INDEX;

But this value will be overriden back to default when next frame of animation will be played because of

  var displayIndex = _currentFrame.displayIndex;
  if (_playState >= 0 && slot.displayIndex != displayIndex)
        {
            slot._setDisplayIndex(displayIndex);
        }

in timeline slot _onArriveAtFrame();

As far as I've understood from sources, every slot's frame now has its display index set, even if it isn't specified in the DragonBones editor:

protected SlotFrameData _parseSlotFrame(Dictionary<string, object> rawData, uint frameStart, uint frameCount)
    {
        var frame = BaseObject.BorrowObject<SlotFrameData>();
        frame.displayIndex = _getNumber(rawData, DISPLAY_INDEX, 0); 

And even if slot is not animated, the default timeline will be created nevertheless, with one frame effectively setting displayIndex to default one.

So, the question is - am I missing something? What is the proper way to set DisplayIndex from my code so it wont be changed back to default value on next playback of animation

Kind regards, Serj

P.S. if you interested, so far to resolve this in a quick and dirty manner I just patched source code by adding new slot bool property displayIndexIsControlledByApi so if it is raised, the animation code wont change displayIndex at all

akdcl commented 7 years ago

Hi,

You can set slot displayController value before set displayIndex value, for exapmple:

slot.displayController = "none";
slot.displayIndex = INDEX;
nanopony commented 7 years ago

Thank you!