/// <summary> /// - Play animation with a specific animation config. /// The API is still in the experimental phase and may encounter bugs or stability or compatibility issues when used. /// </summary> /// <param name="animationConfig">- The animation config.</param> /// <returns>The playing animation state.</returns> /// <see cref="DragonBones.AnimationConfig"/> /// <beta/> /// <version>DragonBones 5.0</version> /// <language>en_US</language> /// <summary> /// - 通过指定的动画配置来播放动画。 /// 该 API 仍在实验阶段,使用时可能遭遇 bug 或稳定性或兼容性问题。 /// </summary> /// <param name="animationConfig">- 动画配置。</param> /// <returns>播放的动画状态。</returns> /// <see cref="DragonBones.AnimationConfig"/> /// <beta/> /// <version>DragonBones 5.0</version> /// <language>zh_CN</language> public AnimationState PlayConfig(AnimationConfig animationConfig) { var animationName = animationConfig.animation; if (!(this._animations.ContainsKey(animationName))) { Helper.Assert(false, "Non-existent animation.\n" + "DragonBones name: " + this._armature.armatureData.parent.name + "Armature name: " + this._armature.name + "Animation name: " + animationName ); return(null); } var animationData = this._animations[animationName]; if (animationConfig.fadeOutMode == AnimationFadeOutMode.Single) { foreach (var aniState in this._animationStates) { if (aniState._animationData == animationData) { return(aniState); } } } if (this._animationStates.Count == 0) { animationConfig.fadeInTime = 0.0f; } else if (animationConfig.fadeInTime < 0.0f) { animationConfig.fadeInTime = animationData.fadeInTime; } if (animationConfig.fadeOutTime < 0.0f) { animationConfig.fadeOutTime = animationConfig.fadeInTime; } if (animationConfig.timeScale <= -100.0f) { animationConfig.timeScale = 1.0f / animationData.scale; } if (animationData.frameCount > 1) { 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; // Play a little time before end. } 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.playTimes < 0) { animationConfig.playTimes = (int)animationData.playTimes; } } else { animationConfig.playTimes = 1; animationConfig.position = 0.0f; if (animationConfig.duration > 0.0) { animationConfig.duration = 0.0f; } } if (animationConfig.duration == 0.0f) { animationConfig.duration = -1.0f; } this._FadeOut(animationConfig); var animationState = BaseObject.BorrowObject <AnimationState>(); // Debug.Log("ArmatureAnim: "+_armature.name); animationState.Init(this._armature, animationData, animationConfig); this._animationDirty = true; this._armature._cacheFrameIndex = -1; if (this._animationStates.Count > 0) { var added = false; for (int i = 0, l = this._animationStates.Count; i < l; ++i) { if (animationState.layer >= this._animationStates[i].layer) { } else { added = true; this._animationStates.Insert(i, animationState); break; } } if (!added) { this._animationStates.Add(animationState); } } else { this._animationStates.Add(animationState); } // Child armature play same name animation. foreach (var slot in this._armature.GetSlots()) { var childArmature = slot.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. this._armature.AdvanceTime(0.0f); } this._lastAnimationState = animationState; return(animationState); }
/** * @private */ override protected Slot _generateSlot(BuildArmaturePackage dataPackage, SlotDisplayDataSet slotDisplayDataSet, Armature armature) { var slot = BaseObject.BorrowObject <UnitySlot>(); var slotData = slotDisplayDataSet.slot; var displayList = new List <object>(); slot.name = slotData.name; slot._rawDisplay = new GameObject(); slot._meshDisplay = slot._rawDisplay; (slot._rawDisplay as GameObject).AddComponent <SpriteRenderer>(); (slot._rawDisplay as GameObject).name = slot.name; foreach (var displayData in slotDisplayDataSet.displays) { switch (displayData.type) { case DisplayType.Image: if (displayData.texture == null) { displayData.texture = this._getTextureData(dataPackage.dataName, displayData.name); } displayList.Add(slot._rawDisplay); break; case DisplayType.Mesh: if (displayData.texture == null) { displayData.texture = this._getTextureData(dataPackage.dataName, displayData.name); } displayList.Add(slot._meshDisplay); break; case DisplayType.Armature: var childArmature = this.BuildArmature(displayData.name, dataPackage.dataName); if (childArmature != null) { if (!slot.inheritAnimation) { var actions = slotData.actions.Count > 0 ? slotData.actions : childArmature.armatureData.actions; if (actions.Count > 0) { foreach (var actionData in actions) { childArmature._bufferAction(actionData); } } else { childArmature.animation.Play(); } } // hide var childArmatureDisplay = childArmature.display as GameObject; var armatureComponent = childArmatureDisplay.GetComponent <UnityArmatureComponent>(); if (armatureComponent != null && EditorApplication.isPlayingOrWillChangePlaymode) { childArmatureDisplay.transform.parent = _hiddenObject.transform; } else { childArmatureDisplay.transform.parent = (armature.display as GameObject).transform; childArmatureDisplay.SetActive(false); } // (childArmature.display as GameObject).name = slot.name; displayData.armature = childArmature.armatureData; // } displayList.Add(childArmature); break; default: displayList.Add(null); break; } } slot._setDisplayList(displayList); return(slot); }
/** * @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.001f; } else if (animationConfig.position > animationData.duration) { animationConfig.position %= animationData.duration; } if (animationConfig.position + animationConfig.duration > animationData.duration) { animationConfig.duration = animationData.duration - animationConfig.position; } } else { animationConfig.position = 0.0f; animationConfig.duration = -1.0f; } var isStop = animationConfig.duration == 0.0f; if (isStop) { animationConfig.playTimes = 1; animationConfig.duration = -1.0f; animationConfig.fadeInTime = 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); } if (isStop) { _lastAnimationState.Stop(); } return(_lastAnimationState); }
/** * @private */ public override TextureData CreateTexture() { return(BaseObject.BorrowObject <UnityTextureData>()); }
protected void _mergeFrameToAnimationTimeline(float framePostion,List <ActionData> actions,List <EventData> events) { var frameStart = (int)Math.Floor(framePostion * _armature.frameRate); // uint() var frames = _animation.frames; if (frames.Count == 0) { var startFrame = BaseObject.BorrowObject <AnimationFrameData>(); // Add start frame. startFrame.position = 0.0f; if (_animation.frameCount > 1) { DragonBones.ResizeList(frames,(int)_animation.frameCount + 1,null); // One more count for zero duration frame. var endFrame = BaseObject.BorrowObject <AnimationFrameData> (); // Add end frame to keep animation timeline has two different frames atleast. endFrame.position = _animation.frameCount / _armature.frameRate; frames[0] = startFrame; frames[(int)_animation.frameCount] = endFrame; } else // TODO { DragonBones.ResizeList(frames,1,null); frames[0] = startFrame; } } AnimationFrameData insertedFrame = null; var replacedFrame = frames[frameStart]; if (replacedFrame != null && (frameStart == 0 || frames[frameStart - 1] == replacedFrame.prev)) // Key frame. { insertedFrame = replacedFrame; } else { insertedFrame = BaseObject.BorrowObject <AnimationFrameData>(); // Create frame. insertedFrame.position = frameStart / _armature.frameRate; frames[frameStart] = insertedFrame; for (int i = frameStart + 1,l = frames.Count; i < l; ++i) // Clear replaced frame. { var frame = frames[i]; if (replacedFrame != null && frame == replacedFrame) { frames[i] = null; } } } if (actions != null) // Merge actions. { foreach (var action in actions) { insertedFrame.actions.Add(action); } } if (events != null) // Merge events. { foreach (var evt in events) { insertedFrame.events.Add(evt); } } // Modify frame link and duration. AnimationFrameData prevFrame = null; AnimationFrameData nextFrame = null; for (int i = 0,l = frames.Count; i < l; ++i) { var currentFrame = frames[i]; if (currentFrame != null && nextFrame != currentFrame) { nextFrame = currentFrame; if (prevFrame != null) { nextFrame.prev = prevFrame; prevFrame.next = nextFrame; prevFrame.duration = nextFrame.position - prevFrame.position; } prevFrame = nextFrame; } else { frames[i] = prevFrame; } } nextFrame.duration = _animation.duration - nextFrame.position; nextFrame = frames[0]; prevFrame.next = nextFrame; nextFrame.prev = prevFrame; }
private void _OnCrossFrame(int frameIndex) { var eventDispatcher = this._armature.proxy; if (this._animationState.actionEnabled) { var frameOffset = this._animationData.frameOffset + this._timelineArray[(this._timelineData as TimelineData).offset + (int)BinaryOffset.TimelineFrameOffset + frameIndex]; var actionCount = this._frameArray[frameOffset + 1]; var actions = this._armature.armatureData.actions; for (var i = 0; i < actionCount; ++i) { var actionIndex = this._frameArray[frameOffset + 2 + i]; var action = actions[actionIndex]; if (action.type == ActionType.Play) { if (action.slot != null) { var slot = this._armature.GetSlot(action.slot.name); if (slot != null) { var childArmature = slot.childArmature; if (childArmature != null) { childArmature._BufferAction(action, true); } } } else if (action.bone != null) { foreach (var slot in this._armature.GetSlots()) { var childArmature = slot.childArmature; if (childArmature != null && slot.parent.boneData == action.bone) { childArmature._BufferAction(action, true); } } } else { this._armature._BufferAction(action, true); } } else { var eventType = action.type == ActionType.Frame ? EventObject.FRAME_EVENT : EventObject.SOUND_EVENT; if (action.type == ActionType.Sound || eventDispatcher.HasEventListener(eventType)) { var eventObject = BaseObject.BorrowObject <EventObject>(); // eventObject.time = this._frameArray[frameOffset] * this._frameRateR; // Precision problem eventObject.time = (float)this._frameArray[frameOffset] / (float)this._frameRate; eventObject.type = eventType; eventObject.name = action.name; eventObject.data = action.data; eventObject.armature = this._armature; eventObject.animationState = this._animationState; if (action.bone != null) { eventObject.bone = this._armature.GetBone(action.bone.name); } if (action.slot != null) { eventObject.slot = this._armature.GetSlot(action.slot.name); } this._armature._dragonBones.BufferEvent(eventObject); } } } } }
private void _AdvanceFadeTime(float passedTime) { var isFadeOut = this._fadeState > 0; if (this._subFadeState < 0) { // Fade start event. this._subFadeState = 0; var eventType = isFadeOut ? EventObject.FADE_OUT : EventObject.FADE_IN; if (this._armature.proxy.HasEventListener(eventType)) { var eventObject = BaseObject.BorrowObject <EventObject>(); eventObject.type = eventType; eventObject.armature = this._armature; eventObject.animationState = this; this._armature._dragonBones.BufferEvent(eventObject); } } if (passedTime < 0.0f) { passedTime = -passedTime; } this._fadeTime += passedTime; if (this._fadeTime >= this.fadeTotalTime) { // Fade complete. this._subFadeState = 1; this._fadeProgress = isFadeOut ? 0.0f : 1.0f; } else if (this._fadeTime > 0.0f) { // Fading. this._fadeProgress = isFadeOut ? (1.0f - this._fadeTime / this.fadeTotalTime) : (this._fadeTime / this.fadeTotalTime); } else { // Before fade. this._fadeProgress = isFadeOut ? 1.0f : 0.0f; } if (this._subFadeState > 0) { // Fade complete event. if (!isFadeOut) { this._playheadState |= 1; // x1 this._fadeState = 0; } var eventType = isFadeOut ? EventObject.FADE_OUT_COMPLETE : EventObject.FADE_IN_COMPLETE; if (this._armature.proxy.HasEventListener(eventType)) { var eventObject = BaseObject.BorrowObject <EventObject>(); eventObject.type = eventType; eventObject.armature = this._armature; eventObject.animationState = this; this._armature._dragonBones.BufferEvent(eventObject); } } }
/** * @private */ internal void _updateFFDTimelineStates() { var time = _time; if (!_animationData.hasAsynchronyTimeline) { time = _timeline._currentTime; } var ffdTimelineStates = new Dictionary <string, FFDTimelineState>(); foreach (var ffdTimelineState in _ffdTimelines) // Create ffd timelines map. { ffdTimelineStates[ffdTimelineState.slot.name] = ffdTimelineState; } foreach (var slot in _armature.GetSlots()) { var slotTimelineName = slot.name; var parentTimelineName = slot.parent.name; if (slot._meshData != null) { var displayIndex = slot.displayIndex; var rawMeshData = displayIndex < slot._displayDataSet.displays.Count ? slot._displayDataSet.displays[displayIndex].mesh : null; if (slot._meshData == rawMeshData) { var ffdTimelineData = _animationData.GetFFDTimeline(_armature._skinData.name, slotTimelineName, displayIndex); if (ffdTimelineData != null && ContainsBoneMask(parentTimelineName)) // && !_isFadeOut { var ffdTimelineState = ffdTimelineStates.ContainsKey(slotTimelineName) ? ffdTimelineStates[slotTimelineName] : null; if (ffdTimelineState != null && ffdTimelineState._timeline == ffdTimelineData) // Remove ffd timeline from map. { ffdTimelineStates.Remove(slotTimelineName); } else // Create new ffd timeline. { ffdTimelineState = BaseObject.BorrowObject <FFDTimelineState>(); ffdTimelineState.slot = slot; ffdTimelineState.FadeIn(_armature, this, ffdTimelineData, time); _ffdTimelines.Add(ffdTimelineState); } } else { for (int i = 0, l = slot._ffdVertices.Count; i < l; ++i) // Clear slot ffd. { slot._ffdVertices[i] = 0.0f; } slot._ffdDirty = true; } } } } foreach (var ffdTimelineState in ffdTimelineStates.Values)// Remove ffd timelines. { //ffdTimelineState.slot._ffdDirty = true; _ffdTimelines.Remove(ffdTimelineState); ffdTimelineState.ReturnToPool(); } }
/// <private/> protected void _UpdateDisplay() { var prevDisplay = this._display != null ? this._display : this._rawDisplay; var prevChildArmature = this._childArmature; // Update display and child armature. if (this._displayIndex >= 0 && this._displayIndex < this._displayList.Count) { this._display = this._displayList[this._displayIndex]; if (this._display != null && this._display is Armature) { this._childArmature = this._display as Armature; this._display = this._childArmature.display; } else { this._childArmature = null; } } else { this._display = null; this._childArmature = null; } // Update display. var currentDisplay = this._display != null ? this._display : this._rawDisplay; if (currentDisplay != prevDisplay) { this._OnUpdateDisplay(); this._ReplaceDisplay(prevDisplay); this._transformDirty = true; this._visibleDirty = true; this._blendModeDirty = true; this._colorDirty = true; } // Update frame. if (currentDisplay == this._rawDisplay || currentDisplay == this._meshDisplay) { this._UpdateFrame(); } // Update child armature. if (this._childArmature != prevChildArmature) { if (prevChildArmature != null) { // Update child armature parent. prevChildArmature._parent = null; prevChildArmature.clock = null; if (prevChildArmature.inheritAnimation) { prevChildArmature.animation.Reset(); } } if (this._childArmature != null) { // Update child armature parent. this._childArmature._parent = this; this._childArmature.clock = this._armature.clock; if (this._childArmature.inheritAnimation) { // Set child armature cache frameRate. if (this._childArmature.cacheFrameRate == 0) { var cacheFrameRate = this._armature.cacheFrameRate; if (cacheFrameRate != 0) { this._childArmature.cacheFrameRate = cacheFrameRate; } } // Child armature action. List <ActionData> actions = null; if (this._displayData != null && this._displayData.type == DisplayType.Armature) { actions = (this._displayData as ArmatureDisplayData).actions; } else if (this._displayIndex >= 0 && this._rawDisplayDatas != null) { var rawDisplayData = this._displayIndex < this._rawDisplayDatas.Count ? this._rawDisplayDatas[this._displayIndex] : null; if (rawDisplayData == null) { rawDisplayData = this._GetDefaultRawDisplayData(this._displayIndex); } if (rawDisplayData != null && rawDisplayData.type == DisplayType.Armature) { actions = (rawDisplayData as ArmatureDisplayData).actions; } } if (actions != null && actions.Count > 0) { foreach (var action in actions) { var eventObject = BaseObject.BorrowObject <EventObject>(); EventObject.ActionDataToInstance(action, eventObject, this._armature); eventObject.slot = this; this._armature._BufferAction(eventObject, false); } } else { this._childArmature.animation.Play(); } } } } }
/** * @private */ override protected Slot _generateSlot(BuildArmaturePackage dataPackage, SkinSlotData skinSlotData, Armature armature) { var slotData = skinSlotData.slot; var slot = BaseObject.BorrowObject <UnitySlot>(); var armatureDisplay = armature.display as GameObject; var transform = armatureDisplay.transform.Find(skinSlotData.slot.name); var gameObject = transform == null ? null : transform.gameObject; if (gameObject == null) { gameObject = new GameObject(slotData.name); gameObject.AddComponent <MeshRenderer>(); gameObject.AddComponent <MeshFilter>(); } slot._init( skinSlotData, gameObject, gameObject ); var displayList = new List <object>(); foreach (var displayData in skinSlotData.displays) { switch (displayData.type) { case DisplayType.Image: if (displayData.texture == null) { displayData.texture = _getTextureData(dataPackage.dataName, displayData.path); } displayList.Add(slot.rawDisplay); break; case DisplayType.Mesh: if (displayData.texture == null) { displayData.texture = _getTextureData(dataPackage.dataName, displayData.path); } displayList.Add(slot.meshDisplay); break; case DisplayType.Armature: var childDisplayName = slotData.name + " (" + displayData.path + ")"; // var childTransform = armatureDisplay.transform.Find(childDisplayName); var childArmature = childTransform == null? BuildArmature(displayData.path, dataPackage.dataName) : BuildArmatureComponent(displayData.path, dataPackage.dataName, null, childTransform.gameObject).armature; if (childArmature != null) { childArmature.inheritAnimation = displayData.inheritAnimation; if (!childArmature.inheritAnimation) { var actions = slotData.actions.Count > 0 ? slotData.actions : childArmature.armatureData.actions; if (actions.Count > 0) { foreach (var actionData in actions) { childArmature._bufferAction(actionData); } } else { childArmature.animation.Play(); } } displayData.armature = childArmature.armatureData; // // Hide var childArmatureDisplay = childArmature.display as GameObject; childArmatureDisplay.name = childDisplayName; childArmatureDisplay.gameObject.hideFlags = HideFlags.HideInHierarchy; childArmatureDisplay.SetActive(false); } displayList.Add(childArmature); break; default: displayList.Add(null); break; } } slot._setDisplayList(displayList); return(slot); }
/// <private/> protected void _UpdateDisplayData() { var prevDisplayData = this._displayData; var prevVerticesData = this._deformVertices != null ? this._deformVertices.verticesData : null; var prevTextureData = this._textureData; DisplayData rawDisplayData = null; VerticesData currentVerticesData = null; this._displayData = null; this._boundingBoxData = null; this._textureData = null; if (this._displayIndex >= 0) { if (this._rawDisplayDatas != null) { rawDisplayData = this._displayIndex < this._rawDisplayDatas.Count ? this._rawDisplayDatas[this._displayIndex] : null; } if (rawDisplayData == null) { rawDisplayData = this._GetDefaultRawDisplayData(this._displayIndex); } if (this._displayIndex < this._displayDatas.Count) { this._displayData = this._displayDatas[this._displayIndex]; } } // Update texture and mesh data. if (this._displayData != null) { if (this._displayData.type == DisplayType.Mesh) { currentVerticesData = (this._displayData as MeshDisplayData).vertices; } else if (this._displayData.type == DisplayType.Path) { currentVerticesData = (this._displayData as PathDisplayData).vertices; } else if (rawDisplayData != null) { if (rawDisplayData.type == DisplayType.Mesh) { currentVerticesData = (rawDisplayData as MeshDisplayData).vertices; } else if (rawDisplayData.type == DisplayType.Path) { currentVerticesData = (rawDisplayData as PathDisplayData).vertices; } } if (this._displayData.type == DisplayType.BoundingBox) { this._boundingBoxData = (this._displayData as BoundingBoxDisplayData).boundingBox; } else if (rawDisplayData != null) { if (rawDisplayData.type == DisplayType.BoundingBox) { this._boundingBoxData = (rawDisplayData as BoundingBoxDisplayData).boundingBox; } } if (this._displayData.type == DisplayType.Image) { this._textureData = (this._displayData as ImageDisplayData).texture; } else if (this._displayData.type == DisplayType.Mesh) { this._textureData = (this._displayData as MeshDisplayData).texture; } } if (this._displayData != prevDisplayData || currentVerticesData != prevVerticesData || this._textureData != prevTextureData) { // Update pivot offset. if (currentVerticesData == null && this._textureData != null) { var imageDisplayData = this._displayData as ImageDisplayData; var scale = this._textureData.parent.scale * this._armature._armatureData.scale; var frame = this._textureData.frame; this._pivotX = imageDisplayData.pivot.x; this._pivotY = imageDisplayData.pivot.y; var rect = frame != null ? frame : this._textureData.region; var width = rect.width; var height = rect.height; if (this._textureData.rotated && frame == null) { width = rect.height; height = rect.width; } this._pivotX *= width * scale; this._pivotY *= height * scale; if (frame != null) { this._pivotX += frame.x * scale; this._pivotY += frame.y * scale; } // Update replace pivot. TODO if (this._displayData != null && rawDisplayData != null && this._displayData != rawDisplayData) { rawDisplayData.transform.ToMatrix(Slot._helpMatrix); Slot._helpMatrix.Invert(); Slot._helpMatrix.TransformPoint(0.0f, 0.0f, Slot._helpPoint); this._pivotX -= Slot._helpPoint.x; this._pivotY -= Slot._helpPoint.y; this._displayData.transform.ToMatrix(Slot._helpMatrix); Slot._helpMatrix.Invert(); Slot._helpMatrix.TransformPoint(0.0f, 0.0f, Slot._helpPoint); this._pivotX += Slot._helpPoint.x; this._pivotY += Slot._helpPoint.y; } if (!DragonBones.yDown) { this._pivotY = (this._textureData.rotated ? this._textureData.region.width : this._textureData.region.height) * scale - this._pivotY; } } else { this._pivotX = 0.0f; this._pivotY = 0.0f; } // Update original transform. if (rawDisplayData != null) { // Compatible. this.origin = rawDisplayData.transform; } else if (this._displayData != null) { // Compatible. this.origin = this._displayData.transform; } else { this.origin = null; } // Update vertices. if (currentVerticesData != prevVerticesData) { if (this._deformVertices == null) { this._deformVertices = BaseObject.BorrowObject <DeformVertices>(); } this._deformVertices.init(currentVerticesData, this._armature); } else if (this._deformVertices != null && this._textureData != prevTextureData) { // Update mesh after update frame. this._deformVertices.verticesDirty = true; } this._displayDirty = true; this._transformDirty = true; } }
/** * @private */ override protected void _updateFrame() { var isMeshDisplay = _meshData != null && _display == _meshDisplay; var currentTextureData = _textureData as UnityTextureData; if (_proxy.isUGUI) { _uiDisplay = _renderDisplay.GetComponent <UnityUGUIDisplay>(); if (_uiDisplay == null) { _uiDisplay = _renderDisplay.AddComponent <UnityUGUIDisplay>(); _uiDisplay.raycastTarget = false; } } else { _renderer = _renderDisplay.GetComponent <MeshRenderer>(); if (_renderer == null) { _renderer = _renderDisplay.AddComponent <MeshRenderer>(); } _meshFilter = _renderDisplay.GetComponent <MeshFilter>(); if (_meshFilter == null) { _meshFilter = _renderDisplay.AddComponent <MeshFilter>(); } } if (_display != null && _displayIndex >= 0 && currentTextureData != null) { var currentTextureAtlasData = currentTextureData.parent as UnityTextureAtlasData; // Update replaced texture atlas. if (_armature.replacedTexture != null && _displayData != null && currentTextureAtlasData == _displayData.texture.parent) { currentTextureAtlasData = _armature._replaceTextureAtlasData as UnityTextureAtlasData; if (currentTextureAtlasData == null) { currentTextureAtlasData = BaseObject.BorrowObject <UnityTextureAtlasData>(); currentTextureAtlasData.CopyFrom(_textureData.parent); if (_proxy.isUGUI) { currentTextureAtlasData.uiTexture = _armature.replacedTexture as Material; } else { currentTextureAtlasData.texture = _armature.replacedTexture as Material; } _armature._replaceTextureAtlasData = currentTextureAtlasData; } currentTextureData = currentTextureAtlasData.GetTexture(currentTextureData.name) as UnityTextureData; } var currentTextureAtlas = _proxy.isUGUI?currentTextureAtlasData.uiTexture :currentTextureAtlasData.texture; if (currentTextureAtlas != null) { var textureAtlasWidth = currentTextureAtlasData.width > 0.0f ? currentTextureAtlasData.width : currentTextureAtlas.mainTexture.width; var textureAtlasHeight = currentTextureAtlasData.height > 0.0f ? currentTextureAtlasData.height : currentTextureAtlas.mainTexture.height; if (_mesh == null) { _mesh = new Mesh(); _mesh.MarkDynamic(); } else { _mesh.Clear(); _mesh.uv = null; _mesh.vertices = null; _mesh.normals = null; _mesh.triangles = null; _mesh.colors32 = null; } if (isMeshDisplay) // Mesh. { if (_uvs == null || _uvs.Length != _meshData.uvs.Count / 2) { _uvs = new Vector2[_meshData.uvs.Count / 2]; } if (_vertices == null || _vertices.Length != _meshData.vertices.Count / 2) { _vertices = new Vector3[_meshData.vertices.Count / 2]; _vertices2 = new Vector3[_vertices.Length]; } for (int i = 0, l = _meshData.uvs.Count; i < l; i += 2) { var iN = i / 2; var u = _meshData.uvs[i]; var v = _meshData.uvs[i + 1]; _uvs[iN] = new Vector2( (currentTextureData.region.x + u * currentTextureData.region.width) / textureAtlasWidth, 1.0f - (currentTextureData.region.y + v * currentTextureData.region.height) / textureAtlasHeight ); _vertices[iN] = new Vector3(_meshData.vertices[i], -_meshData.vertices[i + 1], 0.0f); _vertices2[iN] = _vertices[iN]; } _mesh.vertices = _vertices; // Must set vertices before uvs. _mesh.uv = _uvs; _mesh.triangles = _meshData.vertexIndices.ToArray(); } else // Normal texture. { var pivotY = _pivotY - currentTextureData.region.height * _armature.armatureData.scale; if (_vertices == null || _vertices.Length != 4) { _vertices = new Vector3[4]; _vertices2 = new Vector3[4]; } for (int i = 0, l = 4; i < l; ++i) { var u = 0.0f; var v = 0.0f; switch (i) { case 0: break; case 1: u = 1.0f; break; case 2: u = 1.0f; v = 1.0f; break; case 3: v = 1.0f; break; default: break; } _helpVector2s[i].x = (currentTextureData.region.x + u * currentTextureData.region.width) / textureAtlasWidth; _helpVector2s[i].y = 1.0f - (currentTextureData.region.y + v * currentTextureData.region.height) / textureAtlasHeight; _vertices[i].x = (u * currentTextureData.region.width) * 0.01f - _pivotX; _vertices[i].y = (1.0f - v) * currentTextureData.region.height * 0.01f + pivotY; _vertices[i].z = 0.0f * 0.01f; _vertices2[i] = _vertices[i]; } _mesh.vertices = _vertices; // Must set vertices before uvs. _mesh.uv = _helpVector2s; _mesh.triangles = TRIANGLES; } if (_proxy.isUGUI) { _uiDisplay.material = currentTextureAtlas; _uiDisplay.texture = currentTextureAtlas.mainTexture; _mesh.RecalculateBounds(); _uiDisplay.sharedMesh = _mesh; } else { if (_renderer.enabled) { _mesh.RecalculateBounds(); } _meshFilter.sharedMesh = _mesh; _renderer.sharedMaterial = currentTextureAtlas; } _updateVisible(); return; } } _renderDisplay.SetActive(false); if (_proxy.isUGUI) { _uiDisplay.material = null; _uiDisplay.texture = null; _uiDisplay.sharedMesh = null; } else { _meshFilter.sharedMesh = null; _renderer.sharedMaterial = null; } _helpVector3.x = 0.0f; _helpVector3.y = 0.0f; _helpVector3.z = _renderDisplay.transform.localPosition.z; _renderDisplay.transform.localPosition = _helpVector3; }
/** * @private */ protected override void _UpdateFrame() { var meshData = this._display == this._meshDisplay ? this._meshData : null; var currentTextureData = this._textureData as UnityTextureData; if (_proxy.isUGUI) { _uiDisplay = _renderDisplay.GetComponent <UnityUGUIDisplay>(); if (_uiDisplay == null) { _uiDisplay = _renderDisplay.AddComponent <UnityUGUIDisplay>(); _uiDisplay.raycastTarget = false; } } else { _renderer = _renderDisplay.GetComponent <MeshRenderer>(); if (_renderer == null) { _renderer = _renderDisplay.AddComponent <MeshRenderer>(); } _meshFilter = _renderDisplay.GetComponent <MeshFilter>(); if (_meshFilter == null) { _meshFilter = _renderDisplay.AddComponent <MeshFilter>(); } } if (this._displayIndex >= 0 && this._display != null && currentTextureData != null) { if (this._armature.replacedTexture != null && this._rawDisplayDatas.Contains(this._displayData)) { var currentTextureAtlasData = currentTextureData.parent as UnityTextureAtlasData; if (this._armature._replaceTextureAtlasData == null) { currentTextureAtlasData = BaseObject.BorrowObject <UnityTextureAtlasData>(); currentTextureAtlasData.CopyFrom(currentTextureData.parent); if (_proxy.isUGUI) { currentTextureAtlasData.uiTexture = _armature.replacedTexture as Material; } else { currentTextureAtlasData.texture = _armature.replacedTexture as Material; } this._armature._replaceTextureAtlasData = currentTextureAtlasData; } else { currentTextureAtlasData = this._armature._replaceTextureAtlasData as UnityTextureAtlasData; } currentTextureData = currentTextureAtlasData.GetTexture(currentTextureData.name) as UnityTextureData; } var currentTextureAtlas = _proxy.isUGUI ? currentTextureAtlasData.uiTexture : currentTextureAtlasData.texture; if (currentTextureAtlas != null) { var textureAtlasWidth = currentTextureAtlasData.width > 0.0f ? (int)currentTextureAtlasData.width : currentTextureAtlas.mainTexture.width; var textureAtlasHeight = currentTextureAtlasData.height > 0.0f ? (int)currentTextureAtlasData.height : currentTextureAtlas.mainTexture.height; if (_mesh == null) { _mesh = new Mesh(); _mesh.hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild; _mesh.MarkDynamic(); } var meshDisplay = this._mesh; meshDisplay.Clear(); meshDisplay.uv = null; meshDisplay.vertices = null; meshDisplay.normals = null; meshDisplay.triangles = null; meshDisplay.colors32 = null; var textureScale = _armature.armatureData.scale * currentTextureData.parent.scale; var sourceX = currentTextureData.region.x; var sourceY = currentTextureData.region.y; var sourceWidth = currentTextureData.region.width; var sourceHeight = currentTextureData.region.height; if (meshData != null) { var data = meshData.parent.parent.parent; var intArray = data.intArray; var floatArray = data.floatArray; var vertexCount = intArray[meshData.offset + (int)BinaryOffset.MeshVertexCount]; var triangleCount = intArray[meshData.offset + (int)BinaryOffset.MeshTriangleCount]; int vertexOffset = intArray[meshData.offset + (int)BinaryOffset.MeshFloatOffset]; if (vertexOffset < 0) { vertexOffset += 65536; // Fixed out of bouds bug. } var uvOffset = vertexOffset + vertexCount * 2; if (this._uvs == null || this._uvs.Length != vertexCount) { this._uvs = new Vector2[vertexCount]; } if (this._vertices == null || this._vertices.Length != vertexCount) { this._vertices = new Vector3[vertexCount]; this._vertices2 = new Vector3[vertexCount]; } int[] triangles = new int[triangleCount * 3]; for (int i = 0, iV = vertexOffset, iU = uvOffset, l = vertexCount; i < l; ++i) { this._vertices[i].x = floatArray[iV++] * textureScale; this._vertices[i].y = floatArray[iV++] * textureScale; this._uvs[i].x = (sourceX + floatArray[iU++] * sourceWidth) / textureAtlasWidth; this._uvs[i].y = 1.0f - (sourceY + floatArray[iU++] * sourceHeight) / textureAtlasHeight; this._vertices2[i] = this._vertices[i]; } for (int i = 0; i < triangleCount * 3; ++i) { triangles[i] = intArray[meshData.offset + (int)BinaryOffset.MeshVertexIndices + i]; } // meshDisplay.vertices = this._vertices; meshDisplay.uv = this._uvs;// Must set vertices before uvs. meshDisplay.triangles = triangles; } else { if (_vertices == null || _vertices.Length != 4) { _vertices = new Vector3[4]; _vertices2 = new Vector3[4]; } // Normal texture. for (int i = 0, l = 4; i < l; ++i) { var u = 0.0f; var v = 0.0f; switch (i) { case 0: break; case 1: u = 1.0f; break; case 2: u = 1.0f; v = 1.0f; break; case 3: v = 1.0f; break; default: break; } var scaleWidth = sourceWidth * textureScale; var scaleHeight = sourceHeight * textureScale; var pivotX = _pivotX; var pivotY = scaleHeight - _pivotY; if (currentTextureData.rotated) { var temp = scaleWidth; scaleWidth = scaleHeight; scaleHeight = temp; pivotX = scaleWidth - _pivotX; pivotY = scaleHeight - _pivotY; //uv _helpVector2s[i].x = (sourceX + (1.0f - v) * sourceWidth) / textureAtlasWidth; _helpVector2s[i].y = 1.0f - (sourceY + u * sourceHeight) / textureAtlasHeight; } else { //uv _helpVector2s[i].x = (sourceX + u * sourceWidth) / textureAtlasWidth; _helpVector2s[i].y = 1.0f - (sourceY + v * sourceHeight) / textureAtlasHeight; } //vertices _vertices[i].x = (u * scaleWidth) - pivotX; _vertices[i].y = (1.0f - v) * scaleHeight - pivotY; _vertices[i].z = 0.0f; _vertices2[i] = _vertices[i]; } _mesh.vertices = _vertices; // Must set vertices before uvs. _mesh.uv = _helpVector2s; _mesh.triangles = TRIANGLES; } if (_proxy.isUGUI) { _uiDisplay.material = currentTextureAtlas; _uiDisplay.texture = currentTextureAtlas.mainTexture; _mesh.RecalculateBounds(); _uiDisplay.sharedMesh = _mesh; } else { if (_renderer.enabled) { _mesh.RecalculateBounds(); } _meshFilter.sharedMesh = _mesh; _renderer.sharedMaterial = currentTextureAtlas; } this._blendModeDirty = true; this._colorDirty = true;// Relpace texture will override blendMode and color. this._visibleDirty = true; return; } } _renderDisplay.SetActive(false); if (_proxy.isUGUI) { _uiDisplay.material = null; _uiDisplay.texture = null; _uiDisplay.sharedMesh = null; } else { _meshFilter.sharedMesh = null; _renderer.sharedMaterial = null; } _helpVector3.x = 0.0f; _helpVector3.y = 0.0f; _helpVector3.z = _renderDisplay.transform.localPosition.z; _renderDisplay.transform.localPosition = _helpVector3; }
/** * @private */ private void _advanceFadeTime(float passedTime) { if (passedTime < 0.0f) { passedTime = -passedTime; } _fadeTime += passedTime; var fadeProgress = 0.0f; if (_fadeTime >= fadeTotalTime) // Fade complete. { fadeProgress = _fadeState > 0 ? 0.0f : 1.0f; } else if (_fadeTime > 0.0f) // Fading. { fadeProgress = _fadeState > 0 ? (1 - _fadeTime / fadeTotalTime) : (_fadeTime / fadeTotalTime); } else // Before fade. { fadeProgress = _fadeState > 0 ? 1.0f : 0.0f; } if (_fadeProgress != fadeProgress) { _fadeProgress = fadeProgress; var eventDispatcher = _armature._eventDispatcher; if (_fadeTime <= passedTime) { if (_fadeState > 0) { if (eventDispatcher.HasEventListener(EventObject.FADE_OUT)) { var evt = BaseObject.BorrowObject <EventObject>(); evt.animationState = this; _armature._bufferEvent(evt, EventObject.FADE_OUT); } } else { if (eventDispatcher.HasEventListener(EventObject.FADE_IN)) { var evt = BaseObject.BorrowObject <EventObject>(); evt.animationState = this; _armature._bufferEvent(evt, EventObject.FADE_IN); } } } if (_fadeTime >= fadeTotalTime) { if (_fadeState > 0) { if (eventDispatcher.HasEventListener(EventObject.FADE_OUT_COMPLETE)) { var evt = BaseObject.BorrowObject <EventObject>(); evt.animationState = this; _armature._bufferEvent(evt, EventObject.FADE_OUT_COMPLETE); } } else { _isPausePlayhead = false; _fadeState = 0; if (eventDispatcher.HasEventListener(EventObject.FADE_IN_COMPLETE)) { var evt = BaseObject.BorrowObject <EventObject>(); evt.animationState = this; _armature._bufferEvent(evt, EventObject.FADE_IN_COMPLETE); } } } } }
/** * @private * @internal */ internal void Init(Armature armature, AnimationData animationData, AnimationConfig animationConfig) { if (this._armature != null) { return; } this._armature = armature; this.animationData = animationData; this.resetToPose = animationConfig.resetToPose; this.additiveBlending = animationConfig.additiveBlending; this.displayControl = animationConfig.displayControl; this.actionEnabled = animationConfig.actionEnabled; this.layer = animationConfig.layer; this.playTimes = animationConfig.playTimes; this.timeScale = animationConfig.timeScale; this.fadeTotalTime = animationConfig.fadeInTime; this.autoFadeOutTime = animationConfig.autoFadeOutTime; this.weight = animationConfig.weight; this.name = animationConfig.name.Length > 0 ? animationConfig.name : animationConfig.animation; this.group = animationConfig.group; if (animationConfig.pauseFadeIn) { this._playheadState = 2; // 10 } else { this._playheadState = 3; // 11 } if (animationConfig.duration < 0.0f) { this._position = 0.0f; this._duration = this.animationData.duration; if (animationConfig.position != 0.0f) { if (this.timeScale >= 0.0f) { this._time = animationConfig.position; } else { this._time = animationConfig.position - this._duration; } } else { this._time = 0.0f; } } else { this._position = animationConfig.position; this._duration = animationConfig.duration; this._time = 0.0f; } if (this.timeScale < 0.0f && this._time == 0.0f) { this._time = -0.000001f; // Turn to end. } if (this.fadeTotalTime <= 0.0f) { this._fadeProgress = 0.999999f; // Make different. } if (animationConfig.boneMask.Count > 0) { this._boneMask.ResizeList(animationConfig.boneMask.Count); for (int i = 0, l = this._boneMask.Count; i < l; ++i) { this._boneMask[i] = animationConfig.boneMask[i]; } } this._actionTimeline = BaseObject.BorrowObject <ActionTimelineState>(); this._actionTimeline.Init(this._armature, this, this.animationData.actionTimeline); this._actionTimeline.currentTime = this._time; if (this._actionTimeline.currentTime < 0.0f) { this._actionTimeline.currentTime = this._duration - this._actionTimeline.currentTime; } if (this.animationData.zOrderTimeline != null) { this._zOrderTimeline = BaseObject.BorrowObject <ZOrderTimelineState>(); this._zOrderTimeline.Init(this._armature, this, this.animationData.zOrderTimeline); } }
/** * @private */ internal void _updateTimelineStates() { var time = _time; if (!_animationData.hasAsynchronyTimeline) { time = _timeline._currentTime; } var boneTimelineStates = new Dictionary <string, BoneTimelineState>(); var slotTimelineStates = new Dictionary <string, SlotTimelineState>(); foreach (var boneTimelineState in _boneTimelines) // Creat bone timelines map. { boneTimelineStates.Add(boneTimelineState.bone.name, boneTimelineState); } foreach (var bone in _armature.GetBones()) { var boneTimelineName = bone.name; var boneTimelineData = _animationData.GetBoneTimeline(boneTimelineName); if (boneTimelineData != null && ContainsBoneMask(boneTimelineName)) { var boneTimelineState = boneTimelineStates.ContainsKey(boneTimelineName) ? boneTimelineStates[boneTimelineName] : null; if (boneTimelineState != null) // Remove bone timeline from map. { boneTimelineStates.Remove(boneTimelineName); } else // Create new bone timeline. { boneTimelineState = BaseObject.BorrowObject <BoneTimelineState>(); boneTimelineState.bone = bone; boneTimelineState.FadeIn(_armature, this, boneTimelineData, time); _boneTimelines.Add(boneTimelineState); } } } foreach (var boneTimelineState in boneTimelineStates.Values) // Remove bone timelines. { boneTimelineState.bone.InvalidUpdate(); // _boneTimelines.Remove(boneTimelineState); boneTimelineState.ReturnToPool(); } foreach (var slotTimelineState in _slotTimelines) // Create slot timelines map. { slotTimelineStates[slotTimelineState.slot.name] = slotTimelineState; } foreach (var slot in _armature.GetSlots()) { var slotTimelineName = slot.name; var parentTimelineName = slot.parent.name; var slotTimelineData = _animationData.GetSlotTimeline(slotTimelineName); if (slotTimelineData != null && ContainsBoneMask(parentTimelineName) && _fadeState <= 0) { var slotTimelineState = slotTimelineStates.ContainsKey(slotTimelineName) ? slotTimelineStates[slotTimelineName] : null; if (slotTimelineState != null) // Remove slot timeline from map. { slotTimelineStates.Remove(slotTimelineName); } else // Create new slot timeline. { slotTimelineState = BaseObject.BorrowObject <SlotTimelineState>(); slotTimelineState.slot = slot; slotTimelineState.FadeIn(_armature, this, slotTimelineData, time); _slotTimelines.Add(slotTimelineState); } } } foreach (var slotTimelineState in slotTimelineStates.Values) // Remove slot timelines. { _slotTimelines.Remove(slotTimelineState); slotTimelineState.ReturnToPool(); } _updateFFDTimelineStates(); }
/** * @private * @internal */ internal void UpdateTimelines() { Dictionary <string, List <BoneTimelineState> > boneTimelines = new Dictionary <string, List <BoneTimelineState> >(); foreach (var timeline in this._boneTimelines) { // Create bone timelines map. var timelineName = timeline.bone.name; if (!(boneTimelines.ContainsKey(timelineName))) { boneTimelines[timelineName] = new List <BoneTimelineState>(); } boneTimelines[timelineName].Add(timeline); } foreach (Bone bone in this._armature.GetBones()) { var timelineName = bone.name; if (!this.ContainsBoneMask(timelineName)) { continue; } var timelineDatas = this.animationData.GetBoneTimeline(timelineName); if (boneTimelines.ContainsKey(timelineName)) { // Remove bone timeline from map. boneTimelines.Remove(timelineName); } else { // Create new bone timeline. var bonePose = this._bonePoses.ContainsKey(timelineName) ? this._bonePoses[timelineName] : (this._bonePoses[timelineName] = BaseObject.BorrowObject <BonePose>()); if (timelineDatas != null) { foreach (var timelineData in timelineDatas) { switch (timelineData.type) { case TimelineType.BoneAll: { var timeline = BaseObject.BorrowObject <BoneAllTimelineState>(); timeline.bone = bone; timeline.bonePose = bonePose; timeline.Init(this._armature, this, timelineData); this._boneTimelines.Add(timeline); break; } case TimelineType.BoneTranslate: { var timeline = BaseObject.BorrowObject <BoneTranslateTimelineState>(); timeline.bone = bone; timeline.bonePose = bonePose; timeline.Init(this._armature, this, timelineData); this._boneTimelines.Add(timeline); break; } case TimelineType.BoneRotate: { var timeline = BaseObject.BorrowObject <BoneRotateTimelineState>(); timeline.bone = bone; timeline.bonePose = bonePose; timeline.Init(this._armature, this, timelineData); this._boneTimelines.Add(timeline); break; } case TimelineType.BoneScale: { var timeline = BaseObject.BorrowObject <BoneScaleTimelineState>(); timeline.bone = bone; timeline.bonePose = bonePose; timeline.Init(this._armature, this, timelineData); this._boneTimelines.Add(timeline); break; } default: break; } } } else if (this.resetToPose) { // Pose timeline. var timeline = BaseObject.BorrowObject <BoneAllTimelineState>(); timeline.bone = bone; timeline.bonePose = bonePose; timeline.Init(this._armature, this, null); this._boneTimelines.Add(timeline); } } } foreach (var k in boneTimelines.Keys) { // Remove bone timelines. var timeLines = boneTimelines[k]; foreach (var tiemLine in timeLines) { this._boneTimelines.Remove(tiemLine); tiemLine.ReturnToPool(); } } Dictionary <string, List <SlotTimelineState> > slotTimelines = new Dictionary <string, List <SlotTimelineState> >(); List <int> ffdFlags = new List <int>(); foreach (var timeline in this._slotTimelines) { // Create slot timelines map. var timelineName = timeline.slot.name; if (!(slotTimelines.ContainsKey(timelineName))) { slotTimelines[timelineName] = new List <SlotTimelineState>(); } slotTimelines[timelineName].Add(timeline); } foreach (Slot slot in this._armature.GetSlots()) { var boneName = slot.parent.name; if (!this.ContainsBoneMask(boneName)) { continue; } var timelineName = slot.name; var timelineDatas = this.animationData.GetSlotTimeline(timelineName); if (slotTimelines.ContainsKey(timelineName)) { // Remove slot timeline from map. slotTimelines.Remove(timelineName); } else { // Create new slot timeline. var displayIndexFlag = false; var colorFlag = false; ffdFlags.Clear(); if (timelineDatas != null) { foreach (var timelineData in timelineDatas) { switch (timelineData.type) { case TimelineType.SlotDisplay: { var timeline = BaseObject.BorrowObject <SlotDislayIndexTimelineState>(); timeline.slot = slot; timeline.Init(this._armature, this, timelineData); this._slotTimelines.Add(timeline); displayIndexFlag = true; break; } case TimelineType.SlotColor: { var timeline = BaseObject.BorrowObject <SlotColorTimelineState>(); timeline.slot = slot; timeline.Init(this._armature, this, timelineData); this._slotTimelines.Add(timeline); colorFlag = true; break; } case TimelineType.SlotFFD: { var timeline = BaseObject.BorrowObject <SlotFFDTimelineState>(); timeline.slot = slot; timeline.Init(this._armature, this, timelineData); this._slotTimelines.Add(timeline); ffdFlags.Add((int)timeline.meshOffset); break; } default: break; } } } if (this.resetToPose) { // Pose timeline. if (!displayIndexFlag) { var timeline = BaseObject.BorrowObject <SlotDislayIndexTimelineState>(); timeline.slot = slot; timeline.Init(this._armature, this, null); this._slotTimelines.Add(timeline); } if (!colorFlag) { var timeline = BaseObject.BorrowObject <SlotColorTimelineState>(); timeline.slot = slot; timeline.Init(this._armature, this, null); this._slotTimelines.Add(timeline); } if (slot.rawDisplayDatas != null) { foreach (var displayData in slot.rawDisplayDatas) { uint meshOffset = 0; if (displayData is MeshDisplayData) { meshOffset = (uint)(displayData as MeshDisplayData).offset; } if (displayData != null && displayData.type == DisplayType.Mesh && ffdFlags.IndexOf((displayData as MeshDisplayData).offset) < 0) { var timeline = BaseObject.BorrowObject <SlotFFDTimelineState>(); timeline.meshOffset = (uint)meshOffset; timeline.slot = slot; timeline.Init(this._armature, this, null); this._slotTimelines.Add(timeline); } } } } } } foreach (var k in slotTimelines.Keys) { // Remove slot timelines. var timeLines = slotTimelines[k]; foreach (var timeline in timeLines) { this._slotTimelines.Remove(timeline); timeline.ReturnToPool(); } } }
/** * @private */ override protected Slot _generateSlot(BuildArmaturePackage dataPackage, SkinSlotData skinSlotData, Armature armature) { var slotData = skinSlotData.slot; var slot = BaseObject.BorrowObject <UnitySlot>(); var displayList = new List <object>(); DragonBones.ResizeList(displayList, skinSlotData.displays.Count, null); var armatureDisplay = armature.display as GameObject; var slotsRoot = armatureDisplay.GetComponent <UnityArmatureComponent>().slotsRoot.transform; var transform = slotsRoot.Find(skinSlotData.slot.name); var gameObject = transform == null ? null : transform.gameObject; if (gameObject == null) { gameObject = new GameObject(slotData.name); } slot._init( skinSlotData, gameObject, gameObject ); for (int i = 0, l = skinSlotData.displays.Count; i < l; ++i) { var displayData = skinSlotData.displays[i]; switch (displayData.type) { case DisplayType.Image: if (displayData.texture == null) { displayData.texture = _getTextureData(dataPackage.dataName, displayData.path); } if (!string.IsNullOrEmpty(dataPackage.textureAtlasName)) { slot._textureDatas[i] = _getTextureData(dataPackage.textureAtlasName, displayData.path); } displayList[i] = slot.rawDisplay; break; case DisplayType.Mesh: if (displayData.texture == null) { displayData.texture = _getTextureData(dataPackage.dataName, displayData.path); } if (!string.IsNullOrEmpty(dataPackage.textureAtlasName)) { slot._textureDatas[i] = _getTextureData(dataPackage.textureAtlasName, displayData.path); } displayList[i] = slot.meshDisplay; break; case DisplayType.Armature: var childDisplayName = slotData.name + " (" + displayData.path + ")"; // var childTransform = slotsRoot.Find(childDisplayName); var childArmature = childTransform == null? BuildArmature(displayData.path, dataPackage.dataName) : BuildArmatureComponent(displayData.path, dataPackage.dataName, null, dataPackage.textureAtlasName, childTransform.gameObject).armature; if (childArmature != null) { childArmature.inheritAnimation = displayData.inheritAnimation; if (!childArmature.inheritAnimation) { var actions = slotData.actions.Count > 0 ? slotData.actions : childArmature.armatureData.actions; if (actions.Count > 0) { foreach (var actionData in actions) { childArmature._bufferAction(actionData); } } else { childArmature.animation.Play(); } } displayData.armature = childArmature.armatureData; // // Hide var childArmatureDisplay = childArmature.display as GameObject; childArmatureDisplay.GetComponent <UnityArmatureComponent>().isUGUI = armatureDisplay.GetComponent <UnityArmatureComponent>().isUGUI; childArmatureDisplay.name = childDisplayName; childArmatureDisplay.gameObject.hideFlags = HideFlags.HideInHierarchy; childArmatureDisplay.SetActive(false); } displayList[i] = childArmature; break; default: displayList[i] = null; break; } } slot._setDisplayList(displayList); return(slot); }
/** * @private */ internal void _init(Armature armature, AnimationData animationData, AnimationConfig animationConfig) { _armature = armature; _animationData = animationData; _name = !string.IsNullOrEmpty(animationConfig.name) ? animationConfig.name : animationConfig.animationName; actionEnabled = animationConfig.actionEnabled; additiveBlending = animationConfig.additiveBlending; displayControl = animationConfig.displayControl; playTimes = (uint)animationConfig.playTimes; timeScale = animationConfig.timeScale; fadeTotalTime = animationConfig.fadeInTime; autoFadeOutTime = animationConfig.autoFadeOutTime; weight = animationConfig.weight; if (animationConfig.pauseFadeIn) { _playheadState = 2; // 10 } else { _playheadState = 3; // 11 } _fadeState = -1; _subFadeState = -1; _layer = animationConfig.layer; _time = animationConfig.position; _group = animationConfig.group; if (animationConfig.duration < 0.0f) { _position = 0.0f; _duration = _animationData.duration; } else { _position = animationConfig.position; _duration = animationConfig.duration; } if (fadeTotalTime <= 0.0f) { _fadeProgress = 0.999999f; } if (animationConfig.boneMask.Count > 0) { DragonBones.ResizeList(_boneMask, animationConfig.boneMask.Count, null); for (int i = 0, l = _boneMask.Count; i < l; ++i) { _boneMask[i] = animationConfig.boneMask[i]; } } _timeline = BaseObject.BorrowObject <AnimationTimelineState>(); _timeline._init(_armature, this, _animationData); if (_animationData.zOrderTimeline != null) { _zOrderTimeline = BaseObject.BorrowObject <ZOrderTimelineState>(); _zOrderTimeline._init(_armature, this, _animationData.zOrderTimeline); } _updateTimelineStates(); }
/** * @private */ override public TextureData GenerateTextureData() { return(BaseObject.BorrowObject <UnityTextureData>()); }
/** * @private */ internal void _updateTimelineStates() { var boneTimelineStates = new Dictionary <string, BoneTimelineState>(); var slotTimelineStates = new Dictionary <string, SlotTimelineState>(); var ffdTimelineStates = new Dictionary <string, FFDTimelineState>(); for (int i = 0, l = _boneTimelines.Count; i < l; ++i) // Creat bone timelines map. { var boneTimelineState = _boneTimelines[i]; boneTimelineStates[boneTimelineState.bone.name] = boneTimelineState; } var bones = _armature.GetBones(); for (int i = 0, l = bones.Count; i < l; ++i) { var bone = bones[i]; var boneTimelineName = bone.name; if (ContainsBoneMask(boneTimelineName)) { var boneTimelineData = _animationData.GetBoneTimeline(boneTimelineName); if (boneTimelineData != null) { if (boneTimelineStates.ContainsKey(boneTimelineName)) // Remove bone timeline from map. { boneTimelineStates.Remove(boneTimelineName); } else // Create new bone timeline. { var boneTimelineState = BaseObject.BorrowObject <BoneTimelineState>(); boneTimelineState.bone = bone; boneTimelineState._init(_armature, this, boneTimelineData); _boneTimelines.Add(boneTimelineState); } } } } foreach (var boneTimelineState in boneTimelineStates.Values) // Remove bone timelines. { boneTimelineState.bone.InvalidUpdate(); // _boneTimelines.Remove(boneTimelineState); boneTimelineState.ReturnToPool(); } for (int i = 0, l = _slotTimelines.Count; i < l; ++i) // Creat slot timelines map. { var slotTimelineState = _slotTimelines[i]; slotTimelineStates[slotTimelineState.slot.name] = slotTimelineState; } for (int i = 0, l = _ffdTimelines.Count; i < l; ++i) // Creat ffd timelines map. { var ffdTimelineState = _ffdTimelines[i]; var display = ffdTimelineState._timelineData.display; var meshName = display.inheritAnimation ? display.mesh.name : display.name; ffdTimelineStates[meshName] = ffdTimelineState; } var slots = _armature.GetSlots(); for (int i = 0, l = slots.Count; i < l; ++i) { var slot = slots[i]; var slotTimelineName = slot.name; var parentTimelineName = slot.parent.name; var resetFFDVertices = false; if (ContainsBoneMask(parentTimelineName)) { var slotTimelineData = _animationData.GetSlotTimeline(slotTimelineName); if (slotTimelineData != null) { if (slotTimelineStates.ContainsKey(slotTimelineName)) // Remove slot timeline from map. { slotTimelineStates.Remove(slotTimelineName); } else // Create new slot timeline. { var slotTimelineState = BaseObject.BorrowObject <SlotTimelineState>(); slotTimelineState.slot = slot; slotTimelineState._init(_armature, this, slotTimelineData); _slotTimelines.Add(slotTimelineState); } } var ffdTimelineDatas = _animationData.GetFFDTimeline(_armature._skinData.name, slotTimelineName); if (ffdTimelineDatas != null) { foreach (var pair in ffdTimelineDatas) { if (ffdTimelineStates.ContainsKey(pair.Key)) // Remove ffd timeline from map. { ffdTimelineStates.Remove(pair.Key); } else // Create new ffd timeline. { var ffdTimelineState = BaseObject.BorrowObject <FFDTimelineState>(); ffdTimelineState.slot = slot; ffdTimelineState._init(_armature, this, pair.Value); _ffdTimelines.Add(ffdTimelineState); } } } else { resetFFDVertices = true; } } else { resetFFDVertices = true; } if (resetFFDVertices) { for (int iA = 0, lA = slot._ffdVertices.Count; iA < lA; ++iA) { slot._ffdVertices[iA] = 0.0f; } slot._meshDirty = true; } } foreach (var slotTimelineState in slotTimelineStates.Values) // Remove slot timelines. { _slotTimelines.Remove(slotTimelineState); slotTimelineState.ReturnToPool(); } foreach (var ffdTimelineState in ffdTimelineStates.Values)// Remove ffd timelines. { _ffdTimelines.Remove(ffdTimelineState); ffdTimelineState.ReturnToPool(); } }
public override void Update(float passedTime) { var prevState = this.playState; var prevPlayTimes = this.currentPlayTimes; var prevTime = this.currentTime; if (this.playState <= 0 && this._SetCurrentTime(passedTime)) { var eventDispatcher = this._armature.proxy; if (prevState < 0) { if (this.playState != prevState) { if (this._animationState.displayControl && this._animationState.resetToPose) { // Reset zorder to pose. this._armature._SortZOrder(null, 0); } prevPlayTimes = this.currentPlayTimes; if (eventDispatcher.HasEventListener(EventObject.START)) { var eventObject = BaseObject.BorrowObject <EventObject>(); eventObject.type = EventObject.START; eventObject.armature = this._armature; eventObject.animationState = this._animationState; this._armature._dragonBones.BufferEvent(eventObject); } } else { return; } } var isReverse = this._animationState.timeScale < 0.0f; EventObject loopCompleteEvent = null; EventObject completeEvent = null; if (this.currentPlayTimes != prevPlayTimes) { if (eventDispatcher.HasEventListener(EventObject.LOOP_COMPLETE)) { loopCompleteEvent = BaseObject.BorrowObject <EventObject>(); loopCompleteEvent.type = EventObject.LOOP_COMPLETE; loopCompleteEvent.armature = this._armature; loopCompleteEvent.animationState = this._animationState; } if (this.playState > 0) { if (eventDispatcher.HasEventListener(EventObject.COMPLETE)) { completeEvent = BaseObject.BorrowObject <EventObject>(); completeEvent.type = EventObject.COMPLETE; completeEvent.armature = this._armature; completeEvent.animationState = this._animationState; } } } if (this._frameCount > 1) { var timelineData = this._timelineData as TimelineData; var timelineFrameIndex = (uint)Math.Floor(this.currentTime * this._frameRate); // uint var frameIndex = (int)this._frameIndices[timelineData.frameIndicesOffset + timelineFrameIndex]; if (this._frameIndex != frameIndex) { // Arrive at frame. var crossedFrameIndex = this._frameIndex; this._frameIndex = frameIndex; if (this._timelineArray != null) { this._frameOffset = this._animationData.frameOffset + this._timelineArray[timelineData.offset + (int)BinaryOffset.TimelineFrameOffset + this._frameIndex]; if (isReverse) { if (crossedFrameIndex < 0) { var prevFrameIndex = (int)Math.Floor(prevTime * this._frameRate); crossedFrameIndex = (int)this._frameIndices[timelineData.frameIndicesOffset + prevFrameIndex]; if (this.currentPlayTimes == prevPlayTimes) { // Start. if (crossedFrameIndex == frameIndex) { // Uncrossed. crossedFrameIndex = -1; } } } while (crossedFrameIndex >= 0) { var frameOffset = this._animationData.frameOffset + this._timelineArray[timelineData.offset + (int)BinaryOffset.TimelineFrameOffset + crossedFrameIndex]; // const framePosition = this._frameArray[frameOffset] * this._frameRateR; // Precision problem var framePosition = (float)this._frameArray[frameOffset] / (float)this._frameRate; if (this._position <= framePosition && framePosition <= this._position + this._duration) { // Support interval play. this._OnCrossFrame(crossedFrameIndex); } if (loopCompleteEvent != null && crossedFrameIndex == 0) { // Add loop complete event after first frame. this._armature._dragonBones.BufferEvent(loopCompleteEvent); loopCompleteEvent = null; } if (crossedFrameIndex > 0) { crossedFrameIndex--; } else { crossedFrameIndex = (int)this._frameCount - 1; } if (crossedFrameIndex == frameIndex) { break; } } } else { if (crossedFrameIndex < 0) { var prevFrameIndex = (int)Math.Floor(prevTime * this._frameRate); crossedFrameIndex = (int)this._frameIndices[timelineData.frameIndicesOffset + prevFrameIndex]; var frameOffset = this._animationData.frameOffset + this._timelineArray[timelineData.offset + (int)BinaryOffset.TimelineFrameOffset + crossedFrameIndex]; // const framePosition = this._frameArray[frameOffset] * this._frameRateR; // Precision problem var framePosition = (float)this._frameArray[frameOffset] / (float)this._frameRate; if (this.currentPlayTimes == prevPlayTimes) { // Start. if (prevTime <= framePosition) { // Crossed. if (crossedFrameIndex > 0) { crossedFrameIndex--; } else { crossedFrameIndex = (int)this._frameCount - 1; } } else if (crossedFrameIndex == frameIndex) { // Uncrossed. crossedFrameIndex = -1; } } } while (crossedFrameIndex >= 0) { if (crossedFrameIndex < this._frameCount - 1) { crossedFrameIndex++; } else { crossedFrameIndex = 0; } var frameOffset = this._animationData.frameOffset + this._timelineArray[timelineData.offset + (int)BinaryOffset.TimelineFrameOffset + crossedFrameIndex]; // const framePosition = this._frameArray[frameOffset] * this._frameRateR; // Precision problem var framePosition = (float)this._frameArray[frameOffset] / (float)this._frameRate; if (this._position <= framePosition && framePosition <= this._position + this._duration) { // Support interval play. this._OnCrossFrame(crossedFrameIndex); } if (loopCompleteEvent != null && crossedFrameIndex == 0) { // Add loop complete event before first frame. this._armature._dragonBones.BufferEvent(loopCompleteEvent); loopCompleteEvent = null; } if (crossedFrameIndex == frameIndex) { break; } } } } } } else if (this._frameIndex < 0) { this._frameIndex = 0; if (this._timelineData != null) { this._frameOffset = this._animationData.frameOffset + this._timelineArray[this._timelineData.offset + (int)BinaryOffset.TimelineFrameOffset]; // Arrive at frame. var framePosition = (float)this._frameArray[this._frameOffset] / (float)this._frameRate; if (this.currentPlayTimes == prevPlayTimes) { // Start. if (prevTime <= framePosition) { this._OnCrossFrame(this._frameIndex); } } else if (this._position <= framePosition) { // Loop complete. if (!isReverse && loopCompleteEvent != null) { // Add loop complete event before first frame. this._armature._dragonBones.BufferEvent(loopCompleteEvent); loopCompleteEvent = null; } this._OnCrossFrame(this._frameIndex); } } } if (loopCompleteEvent != null) { this._armature._dragonBones.BufferEvent(loopCompleteEvent); } if (completeEvent != null) { this._armature._dragonBones.BufferEvent(completeEvent); } } }
/// <private/> protected object _GetSlotDisplay(BuildArmaturePackage dataPackage, DisplayData displayData, DisplayData rawDisplayData, Slot slot) { var dataName = dataPackage != null ? dataPackage.dataName : displayData.parent.parent.parent.name; object display = null; switch (displayData.type) { case DisplayType.Image: { var imageDisplayData = displayData as ImageDisplayData; if (imageDisplayData.texture == null) { imageDisplayData.texture = this._GetTextureData(dataName, displayData.path); } else if (dataPackage != null && !string.IsNullOrEmpty(dataPackage.textureAtlasName)) { imageDisplayData.texture = this._GetTextureData(dataPackage.textureAtlasName, displayData.path); } if (rawDisplayData != null && rawDisplayData.type == DisplayType.Mesh && this._IsSupportMesh()) { display = slot.meshDisplay; } else { display = slot.rawDisplay; } } break; case DisplayType.Mesh: { var meshDisplayData = displayData as MeshDisplayData; if (meshDisplayData.texture == null) { meshDisplayData.texture = this._GetTextureData(dataName, meshDisplayData.path); } else if (dataPackage != null && !string.IsNullOrEmpty(dataPackage.textureAtlasName)) { meshDisplayData.texture = this._GetTextureData(dataPackage.textureAtlasName, meshDisplayData.path); } if (this._IsSupportMesh()) { display = slot.meshDisplay; } else { display = slot.rawDisplay; } } break; case DisplayType.Armature: { var armatureDisplayData = displayData as ArmatureDisplayData; var childArmature = this._BuildChildArmature(dataPackage, slot, displayData); if (childArmature != null) { childArmature.inheritAnimation = armatureDisplayData.inheritAnimation; if (!childArmature.inheritAnimation) { var actions = armatureDisplayData.actions.Count > 0 ? armatureDisplayData.actions : childArmature.armatureData.defaultActions; if (actions.Count > 0) { foreach (var action in actions) { var eventObject = BaseObject.BorrowObject <EventObject>(); EventObject.ActionDataToInstance(action, eventObject, slot.armature); eventObject.slot = slot; slot.armature._BufferAction(eventObject, false); } } else { childArmature.animation.Play(); } } armatureDisplayData.armature = childArmature.armatureData; // } display = childArmature; } break; case DisplayType.BoundingBox: break; } return(display); }
protected override AnimationData _ParseAnimation(Dictionary <string, object> rawData) { var animation = BaseObject.BorrowObject <AnimationData>(); animation.frameCount = (uint)Math.Max(ObjectDataParser._GetNumber(rawData, DataParser.DURATION, 1), 1); animation.playTimes = (uint)ObjectDataParser._GetNumber(rawData, DataParser.PLAY_TIMES, 1); animation.duration = (float)animation.frameCount / (float)this._armature.frameRate;//Must float animation.fadeInTime = ObjectDataParser._GetNumber(rawData, DataParser.FADE_IN_TIME, 0.0f); animation.scale = ObjectDataParser._GetNumber(rawData, DataParser.SCALE, 1.0f); animation.name = ObjectDataParser._GetString(rawData, DataParser.NAME, DataParser.DEFAULT_NAME); if (animation.name.Length == 0) { animation.name = DataParser.DEFAULT_NAME; } // Offsets. var offsets = rawData[DataParser.OFFSET] as List <object>; animation.frameIntOffset = uint.Parse(offsets[0].ToString()); animation.frameFloatOffset = uint.Parse(offsets[1].ToString()); animation.frameOffset = uint.Parse(offsets[2].ToString()); this._animation = animation; if (rawData.ContainsKey(DataParser.ACTION)) { animation.actionTimeline = this._ParseBinaryTimeline(TimelineType.Action, uint.Parse(rawData[DataParser.ACTION].ToString())); } if (rawData.ContainsKey(DataParser.Z_ORDER)) { animation.zOrderTimeline = this._ParseBinaryTimeline(TimelineType.ZOrder, uint.Parse(rawData[DataParser.Z_ORDER].ToString())); } if (rawData.ContainsKey(DataParser.BONE)) { var rawTimeliness = rawData[DataParser.BONE] as Dictionary <string, object>; foreach (var k in rawTimeliness.Keys) { var rawTimelines = rawTimeliness[k] as List <object>; var bone = this._armature.GetBone(k); if (bone == null) { continue; } for (int i = 0, l = rawTimelines.Count; i < l; i += 2) { var timelineType = int.Parse(rawTimelines[i].ToString()); var timelineOffset = int.Parse(rawTimelines[i + 1].ToString()); var timeline = this._ParseBinaryTimeline((TimelineType)timelineType, (uint)timelineOffset); this._animation.AddBoneTimeline(bone, timeline); } } } if (rawData.ContainsKey(DataParser.SLOT)) { var rawTimeliness = rawData[DataParser.SLOT] as Dictionary <string,object>; foreach (var k in rawTimeliness.Keys) { var rawTimelines = rawTimeliness[k] as List <object>; var slot = this._armature.GetSlot(k); if (slot == null) { continue; } for (int i = 0,l = rawTimelines.Count; i < l; i += 2) { var timelineType = int.Parse(rawTimelines[i].ToString()); var timelineOffset = int.Parse(rawTimelines[i + 1].ToString()); var timeline = this._ParseBinaryTimeline((TimelineType)timelineType,(uint)timelineOffset); this._animation.AddSlotTimeline(slot,timeline); } } } if (rawData.ContainsKey(DataParser.CONSTRAINT)) { var rawTimeliness = rawData[DataParser.CONSTRAINT] as Dictionary <string,object>; foreach (var k in rawTimeliness.Keys) { var rawTimelines = rawTimeliness[k] as List <object>; var constraint = this._armature.GetConstraint(k); if (constraint == null) { continue; } for (int i = 0,l = rawTimelines.Count; i < l; i += 2) { var timelineType = int.Parse(rawTimelines[i].ToString()); var timelineOffset = int.Parse(rawTimelines[i + 1].ToString()); var timeline = this._ParseBinaryTimeline((TimelineType)timelineType,(uint)timelineOffset); this._animation.AddConstraintTimeline(constraint,timeline); } } } this._animation = null; return(animation); }
override public void Update(float time) { var prevTime = _currentTime; var prevPlayTimes = _currentPlayTimes; if (!_isCompleted && _setCurrentTime(time)) { var eventDispatcher = _armature._eventDispatcher; if (!_isStarted) { _isStarted = true; if (eventDispatcher.HasEventListener(EventObject.START)) { var eventObject = BaseObject.BorrowObject <EventObject>(); eventObject.animationState = _animationState; _armature._bufferEvent(eventObject, EventObject.START); } } if (_keyFrameCount > 0) { var currentFrameIndex = _keyFrameCount > 1 ? (int)(_currentTime * _frameRate) : 0; var currentFrame = _timeline.frames[currentFrameIndex]; if (_currentFrame != currentFrame) { if (_keyFrameCount > 1) { var crossedFrame = _currentFrame; _currentFrame = currentFrame; if (crossedFrame == null) { var prevFrameIndex = (int)(prevTime * _frameRate); crossedFrame = _timeline.frames[prevFrameIndex]; if (_isReverse) { } else { if ( prevTime <= crossedFrame.position || prevPlayTimes != _currentPlayTimes ) { crossedFrame = crossedFrame.prev; } } } // TODO 1 2 3 key frame loop, first key frame after loop complete. if (_isReverse) { while (crossedFrame != currentFrame) { _onCrossFrame(crossedFrame); crossedFrame = crossedFrame.prev; } } else { while (crossedFrame != currentFrame) { crossedFrame = crossedFrame.next; _onCrossFrame(crossedFrame); } } } else { _currentFrame = currentFrame; _onCrossFrame(_currentFrame); } } } if (prevPlayTimes != _currentPlayTimes) { if (eventDispatcher.HasEventListener(EventObject.LOOP_COMPLETE)) { var eventObject = BaseObject.BorrowObject <EventObject>(); eventObject.animationState = _animationState; _armature._bufferEvent(eventObject, EventObject.LOOP_COMPLETE); } if (_isCompleted && eventDispatcher.HasEventListener(EventObject.COMPLETE)) { var eventObject = BaseObject.BorrowObject <EventObject>(); eventObject.animationState = _animationState; _armature._bufferEvent(eventObject, EventObject.COMPLETE); } _currentFrame = null; } } }
/** * @language zh_CN * 淡入播放指定名称的动画。 * @param animationName 动画数据的名称。 * @param playTimes 循环播放的次数。 [-1: 使用数据默认值, 0: 无限循环播放, [1~N]: 循环播放 N 次] * @param fadeInTime 淡入的时间。 [-1: 使用数据默认值, [0~N]: N 秒淡入完毕] (以秒为单位) * @param layer 混合的图层,图层高会优先获取混合权重。 * @param group 混合的组,用于给动画状态编组,方便混合淡出控制。 * @param fadeOutMode 淡出的模式。 * @param additiveBlending 以叠加的形式混合。 * @param displayControl 是否对显示对象属性可控。 * @param pauseFadeOut 暂停需要淡出的动画。 * @param pauseFadeIn 暂停需要淡入的动画,直到淡入结束才开始播放。 * @returns 返回控制这个动画数据的动画状态。 * @see dragonBones.AnimationFadeOutMode * @see dragonBones.AnimationState * @version DragonBones 4.5 */ public AnimationState FadeIn( string animationName, float fadeInTime = -1.0f, int playTimes = -1, int layer = 0, string group = null, AnimationFadeOutMode fadeOutMode = AnimationFadeOutMode.SameLayerAndGroup, bool additiveBlending = false, bool displayControl = true, bool pauseFadeOut = true, bool pauseFadeIn = true ) { if (!_animations.ContainsKey(animationName)) { _time = 0; DragonBones.Warn( "Non-existent animation. " + " DragonBones: " + _armature.armatureData.parent.name + " Armature: " + _armature.name + " Animation: " + animationName ); return(null); } var animationData = _animations[animationName]; if (float.IsNaN(_time)) { _time = 0.0f; } _isPlaying = true; if (fadeInTime < 0.0f || float.IsNaN(fadeInTime)) { if (_lastAnimationState != null) { fadeInTime = animationData.fadeInTime; } else { fadeInTime = 0.0f; } } if (playTimes < 0) { playTimes = (int)animationData.playTimes; } _fadeOut(fadeInTime, layer, group, fadeOutMode, pauseFadeOut); _lastAnimationState = BaseObject.BorrowObject <AnimationState>(); _lastAnimationState._layer = layer; _lastAnimationState._group = group; _lastAnimationState.additiveBlending = additiveBlending; _lastAnimationState.displayControl = displayControl; _lastAnimationState._fadeIn( _armature, animationData.animation != null ? animationData.animation : animationData, animationName, (uint)playTimes, animationData.position, animationData.duration, _time, 1 / animationData.scale, fadeInTime, pauseFadeIn ); _animationStates.Add(_lastAnimationState); _animationStateDirty = true; _time = 0.0f; _armature._cacheFrameIndex = -1; if (_animationStates.Count > 1) { _animationStates.Sort(_sortAnimationState); } foreach (var slot in _armature.GetSlots()) { if (slot.inheritAnimation) { var childArmature = slot.childArmature; if ( childArmature != null && childArmature.animation.HasAnimation(animationName) && childArmature.animation.GetState(animationName) == null ) { childArmature.animation.FadeIn(animationName); } } } if (fadeInTime <= 0.0f) { _armature.AdvanceTime(0.0f); // Blend animation state, update armature. (pass actions and events) } return(_lastAnimationState); }
override public void Update(float passedTime) { var prevState = _playState; var prevTime = _currentTime; var prevPlayTimes = _currentPlayTimes; if (_playState <= 0 && _setCurrentTime(passedTime)) { var eventDispatcher = _armature.eventDispatcher; if (prevState < 0 && _playState != prevState) { if (_animationState.displayControl) { _armature._sortZOrder(null); } if (eventDispatcher.HasEventListener(EventObject.START)) { var eventObject = BaseObject.BorrowObject <EventObject>(); eventObject.animationState = _animationState; _armature._bufferEvent(eventObject, EventObject.START); } } if (prevTime < 0.0f) { return; } bool isReverse = false; EventObject loopCompleteEvent = null; EventObject completeEvent = null; if (_currentPlayTimes != prevPlayTimes) { if (eventDispatcher.HasEventListener(EventObject.LOOP_COMPLETE)) { loopCompleteEvent = BaseObject.BorrowObject <EventObject>(); loopCompleteEvent.animationState = _animationState; } if (_playState > 0) { if (eventDispatcher.HasEventListener(EventObject.COMPLETE)) { completeEvent = BaseObject.BorrowObject <EventObject>(); completeEvent.animationState = _animationState; } isReverse = prevTime > this._currentTime; } else { isReverse = prevTime < this._currentTime; } } else { isReverse = prevTime > this._currentTime; } if (_keyFrameCount > 1) { var currentFrameIndex = (int)(_currentTime * _frameRate); // uint var currentFrame = _timelineData.frames[currentFrameIndex]; if (_currentFrame != currentFrame) { var crossedFrame = _currentFrame; _currentFrame = currentFrame; if (isReverse) { if (crossedFrame == null) { var prevFrameIndex = (int)(prevTime * _frameRate); crossedFrame = _timelineData.frames[prevFrameIndex]; if (_currentPlayTimes == prevPlayTimes) // Start. { if (crossedFrame == currentFrame) // Uncrossed. { crossedFrame = null; } } } while (crossedFrame != null) { if ( _position <= crossedFrame.position && crossedFrame.position <= _position + _duration ) // Support interval play. { _onCrossFrame(crossedFrame); } if (loopCompleteEvent != null && crossedFrame == _timelineData.frames[0]) // Add loop complete event after first frame. { _armature._bufferEvent(loopCompleteEvent, EventObject.LOOP_COMPLETE); loopCompleteEvent = null; } crossedFrame = crossedFrame.prev; if (crossedFrame == currentFrame) { break; } } } else { if (crossedFrame == null) { var prevFrameIndex = (int)(prevTime * _frameRate); crossedFrame = _timelineData.frames[prevFrameIndex]; if (_currentPlayTimes == prevPlayTimes) // Start. { if (prevTime <= crossedFrame.position) // Crossed. { crossedFrame = crossedFrame.prev; } else if (crossedFrame == currentFrame) // Uncrossed. { crossedFrame = null; } } } while (crossedFrame != null) { crossedFrame = crossedFrame.next; if (loopCompleteEvent != null && crossedFrame == _timelineData.frames[0]) // Add loop complete event before first frame. { _armature._bufferEvent(loopCompleteEvent, EventObject.LOOP_COMPLETE); loopCompleteEvent = null; } if ( _position <= crossedFrame.position && crossedFrame.position <= _position + _duration ) // Support interval play. { _onCrossFrame(crossedFrame); } if (crossedFrame == currentFrame) { break; } } } } else if (_keyFrameCount > 0 && _currentFrame == null) { _currentFrame = _timelineData.frames[0]; if (_currentPlayTimes == prevPlayTimes) // Start. { if (prevTime <= _currentFrame.position) { _onCrossFrame(_currentFrame); } } else if (_position <= _currentFrame.position) // Loop complete. { if (!isReverse && loopCompleteEvent != null) // Add loop complete event before first frame. { _armature._bufferEvent(loopCompleteEvent, EventObject.LOOP_COMPLETE); loopCompleteEvent = null; } _onCrossFrame(_currentFrame); } } } if (loopCompleteEvent != null) { _armature._bufferEvent(loopCompleteEvent, EventObject.LOOP_COMPLETE); loopCompleteEvent = null; } if (completeEvent != null) { _armature._bufferEvent(completeEvent, EventObject.COMPLETE); completeEvent = null; } } }
/** * @private */ override protected void _updateFrame() { var isMeshDisplay = _meshData != null && _display == _meshDisplay; var currentTextureData = _textureData as UnityTextureData; var renderer = _renderDisplay.GetComponent <MeshRenderer>(); var meshFilter = _renderDisplay.GetComponent <MeshFilter>(); if (_display != null && _displayIndex >= 0 && currentTextureData != null) { var currentTextureAtlasData = currentTextureData.parent as UnityTextureAtlasData; // Update replaced texture atlas. if (_armature.replacedTexture != null && _displayData != null && currentTextureAtlasData == _displayData.texture.parent) { currentTextureAtlasData = _armature._replaceTextureAtlasData as UnityTextureAtlasData; if (currentTextureAtlasData == null) { currentTextureAtlasData = BaseObject.BorrowObject <UnityTextureAtlasData>(); currentTextureAtlasData.CopyFrom(_textureData.parent); currentTextureAtlasData.texture = _armature.replacedTexture as Material; _armature._replaceTextureAtlasData = currentTextureAtlasData; } currentTextureData = currentTextureAtlasData.GetTexture(currentTextureData.name) as UnityTextureData; } var currentTextureAtlas = currentTextureAtlasData.texture; if (currentTextureAtlas != null) { var textureAtlasWidth = currentTextureAtlasData.width > 0.0f ? currentTextureAtlasData.width : currentTextureAtlas.mainTexture.width; var textureAtlasHeight = currentTextureAtlasData.height > 0.0f ? currentTextureAtlasData.height : currentTextureAtlas.mainTexture.height; if (_mesh != null) { #if UNITY_EDITOR //Object.DestroyImmediate(_mesh); #else Object.Destroy(_mesh); #endif } _mesh = new Mesh(); if (isMeshDisplay) // Mesh. { _uvs = new Vector2[_meshData.uvs.Count / 2]; _vertices = new Vector3[_meshData.vertices.Count / 2]; for (int i = 0, l = _meshData.uvs.Count; i < l; i += 2) { var iN = i / 2; var u = _meshData.uvs[i]; var v = _meshData.uvs[i + 1]; _uvs[iN] = new Vector2( (currentTextureData.region.x + u * currentTextureData.region.width) / textureAtlasWidth, 1.0f - (currentTextureData.region.y + v * currentTextureData.region.height) / textureAtlasHeight ); _vertices[iN] = new Vector3(_meshData.vertices[i], -_meshData.vertices[i + 1], 0.0f); } _mesh.vertices = _vertices; // Must set vertices before uvs. _mesh.uv = _uvs; _mesh.triangles = _meshData.vertexIndices.ToArray(); } else // Normal texture. { var pivotY = _pivotY - currentTextureData.region.height * _armature.armatureData.scale; if (_vertices == null || _vertices.Length != 4) { _vertices = new Vector3[4]; } for (int i = 0, l = 4; i < l; ++i) { var u = 0.0f; var v = 0.0f; switch (i) { case 0: break; case 1: u = 1.0f; break; case 2: u = 1.0f; v = 1.0f; break; case 3: v = 1.0f; break; default: break; } _helpVector2s[i].x = (currentTextureData.region.x + u * currentTextureData.region.width) / textureAtlasWidth; _helpVector2s[i].y = 1.0f - (currentTextureData.region.y + v * currentTextureData.region.height) / textureAtlasHeight; _vertices[i].x = (u * currentTextureData.region.width) * 0.01f - _pivotX; _vertices[i].y = (1.0f - v) * currentTextureData.region.height * 0.01f + pivotY; _vertices[i].z = 0.0f * 0.01f; } _mesh.vertices = _vertices; // Must set vertices before uvs. _mesh.uv = _helpVector2s; _mesh.triangles = TRIANGLES; } meshFilter.sharedMesh = _mesh; renderer.sharedMaterial = currentTextureAtlas; _updateVisible(); return; } } _renderDisplay.SetActive(false); meshFilter.sharedMesh = null; renderer.sharedMaterial = null; _helpVector3.x = 0.0f; _helpVector3.y = 0.0f; _helpVector3.z = _renderDisplay.transform.localPosition.z; _renderDisplay.transform.localPosition = _helpVector3; }
override public void Update(float passedTime) { var prevState = _playState; var prevTime = _currentTime; var prevPlayTimes = _currentPlayTimes; if (_playState <= 0 && _setCurrentTime(passedTime)) { var eventDispatcher = _armature.eventDispatcher; if (prevState < 0 && _playState != prevState) { if (_animationState.displayControl) { _armature._sortZOrder(null); } if (eventDispatcher.HasEventListener(EventObject.START)) { var eventObject = BaseObject.BorrowObject <EventObject>(); eventObject.animationState = _animationState; _armature._bufferEvent(eventObject, EventObject.START); } } if (prevTime < 0.0f) { return; } if (_keyFrameCount > 1) { var currentFrameIndex = (int)(_currentTime * _frameRate); // uint var currentFrame = _timelineData.frames[currentFrameIndex]; if (_currentFrame != currentFrame) { var isReverse = _currentPlayTimes == prevPlayTimes && prevTime > _currentTime; var crossedFrame = _currentFrame; _currentFrame = currentFrame; if (crossedFrame == null) { var prevFrameIndex = (int)(prevTime * _frameRate); crossedFrame = _timelineData.frames[prevFrameIndex]; if (isReverse) { } else { if ( prevTime <= crossedFrame.position // || prevPlayTimes != _currentPlayTimes ? ) { crossedFrame = crossedFrame.prev; } } } if (isReverse) { while (crossedFrame != currentFrame) { _onCrossFrame(crossedFrame); crossedFrame = crossedFrame.prev; } } else { while (crossedFrame != currentFrame) { crossedFrame = crossedFrame.next; _onCrossFrame(crossedFrame); } } } else if (_keyFrameCount > 0 && _currentFrame == null) { _currentFrame = _timelineData.frames[0]; _onCrossFrame(_currentFrame); } } if (_currentPlayTimes != prevPlayTimes) { if (eventDispatcher.HasEventListener(EventObject.LOOP_COMPLETE)) { var eventObject = BaseObject.BorrowObject <EventObject>(); eventObject.animationState = _animationState; _armature._bufferEvent(eventObject, EventObject.LOOP_COMPLETE); } if (_playState > 0 && eventDispatcher.HasEventListener(EventObject.COMPLETE)) { var eventObject = BaseObject.BorrowObject <EventObject>(); eventObject.animationState = _animationState; _armature._bufferEvent(eventObject, EventObject.COMPLETE); } } } }