Example #1
0
        /**
         * @language zh_CN
         * @beta
         * 通过动画配置来播放动画。
         * @param animationConfig 动画配置。
         * @returns 对应的动画状态。
         * @see DragonBones.AnimationConfig
         * @see DragonBones.AnimationState
         * @version DragonBones 5.0
         */
        public AnimationState PlayConfig(AnimationConfig animationConfig)
        {
            if (animationConfig == null)
            {
                DragonBones.Assert(false, DragonBones.ARGUMENT_ERROR);
                return(null);
            }

            var animationName = !string.IsNullOrEmpty(animationConfig.animationName) ? animationConfig.animationName : animationConfig.name;
            var animationData = _animations.ContainsKey(animationName) ? _animations[animationName] : null;

            if (animationData == null)
            {
                DragonBones.Assert(false,
                                   "Non-existent animation.\n" +
                                   "DragonBones name: " + _armature.armatureData.parent.name +
                                   " Armature name: " + _armature.name +
                                   " Animation name: " + animationName
                                   );

                return(null);
            }

            _isPlaying = true;

            if (animationConfig.playTimes < 0)
            {
                animationConfig.playTimes = (int)animationData.playTimes;
            }

            if (animationConfig.fadeInTime < 0.0f || float.IsNaN(animationConfig.fadeInTime))
            {
                if (_lastAnimationState != null)
                {
                    animationConfig.fadeInTime = animationData.fadeInTime;
                }
                else
                {
                    animationConfig.fadeInTime = 0.0f;
                }
            }

            if (animationConfig.fadeOutTime < 0.0f || float.IsNaN(animationConfig.fadeOutTime))
            {
                animationConfig.fadeOutTime = animationConfig.fadeInTime;
            }

            if (animationConfig.timeScale <= -100.0f || float.IsNaN(animationConfig.timeScale)) //
            {
                animationConfig.timeScale = 1.0f / animationData.scale;
            }

            if (animationData.duration > 0.0f)
            {
                if (float.IsNaN(animationConfig.position))
                {
                    animationConfig.position = 0.0f;
                }
                else if (animationConfig.position < 0.0f)
                {
                    animationConfig.position %= animationData.duration;
                    animationConfig.position  = animationData.duration - animationConfig.position;
                }
                else if (animationConfig.position == animationData.duration)
                {
                    animationConfig.position -= 0.000001f;
                }
                else if (animationConfig.position > animationData.duration)
                {
                    animationConfig.position %= animationData.duration;
                }

                if (animationConfig.duration > 0.0f && animationConfig.position + animationConfig.duration > animationData.duration)
                {
                    animationConfig.duration = animationData.duration - animationConfig.position;
                }

                if (animationConfig.duration == 0.0f)
                {
                    animationConfig.playTimes = 1;
                }
                else if (animationConfig.playTimes < 0)
                {
                    animationConfig.playTimes = (int)animationData.playTimes;
                }
            }
            else
            {
                animationConfig.playTimes = 1;
                animationConfig.position  = 0.0f;
                animationConfig.duration  = 0.0f;
            }

            _fadeOut(animationConfig);

            _lastAnimationState = BaseObject.BorrowObject <AnimationState>();
            _lastAnimationState._init(_armature, animationData, animationConfig);
            _animationStates.Add(_lastAnimationState);
            _animationStateDirty = true;
            _cacheFrameIndex     = -1;

            if (_animationStates.Count > 1)
            {
                _animationStates.Sort(_sortAnimationState);
            }

            // Child armature play same name animation.
            var slots = _armature.GetSlots();

            for (int i = 0, l = slots.Count; i < l; ++i)
            {
                var childArmature = slots[i].childArmature;
                if (
                    childArmature != null && childArmature.inheritAnimation &&
                    childArmature.animation.HasAnimation(animationName) &&
                    childArmature.animation.GetState(animationName) == null
                    )
                {
                    childArmature.animation.FadeIn(animationName); //
                }
            }

            if (animationConfig.fadeInTime <= 0.0f) // Blend animation state, update armature.
            {
                _armature.AdvanceTime(0.0f);
            }

            return(_lastAnimationState);
        }