/** * @language zh_CN * 用外部贴图替换display贴图。 * @param dragonBonesName 指定的龙骨数据名称。 * @param armatureName 指定的骨架名称。 * @param slotName 指定的插槽名称。 * @param displayName 指定的显示对象名称。 * @param slot 指定的插槽实例。 * @param texture 新的贴图。 * @param material 新的材质。 * @param isUGUI 是否为ugui。 * @param displayIndex 要替换的显示对象的索引,如果未设置,则替换当前正在显示的显示对象。 * @version DragonBones 4.5 */ public void ReplaceSlotDisplay(string dragonBonesName, string armatureName, string slotName, string displayName, Slot slot, Texture2D texture, Material material, bool isUGUI = false, int displayIndex = -1) { var dataPackage = new BuildArmaturePackage(); if (_fillBuildArmaturePackage(dataPackage, dragonBonesName, armatureName, null, null)) { var slotDisplayDataSet = dataPackage.skin.GetSlot(slotName); if (slotDisplayDataSet != null) { DisplayData prevDispalyData = null; foreach (var displayData in slotDisplayDataSet.displays) { if (displayData.name == displayName) { prevDispalyData = displayData; break; } } if (prevDispalyData == null) { return; } TextureData prevTextureData = prevDispalyData.texture; UnityTextureData newTextureData = new UnityTextureData(); newTextureData.CopyFrom(prevTextureData); newTextureData.rotated = false; newTextureData.region.x = 0f; newTextureData.region.y = 0f; newTextureData.region.width = texture.width; newTextureData.region.height = texture.height; newTextureData.frame = newTextureData.region; newTextureData.name = prevTextureData.name; newTextureData.parent = new UnityTextureAtlasData(); newTextureData.parent.width = texture.width; newTextureData.parent.height = texture.height; if (isUGUI) { (newTextureData.parent as UnityTextureAtlasData).uiTexture = material; } else { (newTextureData.parent as UnityTextureAtlasData).texture = material; } material.mainTexture = texture; DisplayData newDisplayData = new DisplayData(); newDisplayData.armature = prevDispalyData.armature; newDisplayData.boundingBox = prevDispalyData.boundingBox; newDisplayData.inheritAnimation = prevDispalyData.inheritAnimation; newDisplayData.isRelativePivot = prevDispalyData.isRelativePivot; newDisplayData.name = prevDispalyData.name; newDisplayData.pivot.CopyFrom(prevDispalyData.pivot); newDisplayData.texture = newTextureData; newDisplayData.type = prevDispalyData.type; newDisplayData.transform.CopyFrom(prevDispalyData.transform); newDisplayData.mesh = prevDispalyData.mesh; if (newDisplayData.mesh != null && newDisplayData.mesh.uvs != null) { List <float> uvs = new List <float>(); for (int i = 0; i < newDisplayData.mesh.uvs.Count; i += 2) { Vector2 uv = new Vector2(newDisplayData.mesh.uvs[i], newDisplayData.mesh.uvs[i + 1]); Vector2 uvPos = new Vector2(newTextureData.frame.x, -newTextureData.frame.y) + new Vector2(newTextureData.frame.width * uv.x, newTextureData.frame.height * uv.y); uv.x = uvPos.x / newTextureData.frame.width; uv.y = uvPos.y / newTextureData.frame.height; uvs.Add(uv.x); uvs.Add(uv.y); } newDisplayData.mesh.uvs.Clear(); newDisplayData.mesh.uvs.AddRange(uvs); } _replaceSlotDisplay(dataPackage, newDisplayData, slot, displayIndex); } } }
/** * @language zh_CN * 用外部贴图替换display贴图。 * @param dragonBonesName 指定的龙骨数据名称。 * @param armatureName 指定的骨架名称。 * @param slotName 指定的插槽名称。 * @param displayName 指定的显示对象名称。 * @param slot 指定的插槽实例。 * @param texture 新的贴图。 * @param material 新的材质。 * @param isUGUI 是否为ugui。 * @param displayIndex 要替换的显示对象的索引,如果未设置,则替换当前正在显示的显示对象。 * @version DragonBones 4.5 */ public void ReplaceSlotDisplay(string dragonBonesName, string armatureName, string slotName, string displayName, Slot slot, Texture2D texture, Material material, bool isUGUI = false, int displayIndex = -1) { var dataPackage = new BuildArmaturePackage(); if (_FillBuildArmaturePackage(dataPackage, dragonBonesName, armatureName, null, null)) { var displays = dataPackage.skin.GetDisplays(slotName); DisplayData prevDispalyData = null; foreach (var displayData in displays) { if (displayData.name == displayName) { prevDispalyData = displayData; break; } } //QQQ if (prevDispalyData == null || !(prevDispalyData is ImageDisplayData)) { return; } TextureData prevTextureData = (prevDispalyData as ImageDisplayData).texture; UnityTextureData newTextureData = new UnityTextureData(); newTextureData.CopyFrom(prevTextureData); newTextureData.rotated = false; newTextureData.region.x = 0.0f; newTextureData.region.y = 0.0f; newTextureData.region.width = texture.width; newTextureData.region.height = texture.height; newTextureData.frame = newTextureData.region; newTextureData.name = prevTextureData.name; newTextureData.parent = new UnityTextureAtlasData(); newTextureData.parent.width = (uint)texture.width; newTextureData.parent.height = (uint)texture.height; if (isUGUI) { (newTextureData.parent as UnityTextureAtlasData).uiTexture = material; } else { (newTextureData.parent as UnityTextureAtlasData).texture = material; } material.mainTexture = texture; ImageDisplayData newDisplayData = prevDispalyData is MeshDisplayData ? new MeshDisplayData() : new ImageDisplayData(); newDisplayData.type = prevDispalyData.type; newDisplayData.name = prevDispalyData.name; newDisplayData.path = prevDispalyData.path; newDisplayData.transform.CopyFrom(prevDispalyData.transform); newDisplayData.parent = prevDispalyData.parent; newDisplayData.pivot.CopyFrom((prevDispalyData as ImageDisplayData).pivot); newDisplayData.texture = newTextureData; if (newDisplayData is MeshDisplayData) { (newDisplayData as MeshDisplayData).inheritAnimation = (prevDispalyData as MeshDisplayData).inheritAnimation; (newDisplayData as MeshDisplayData).offset = (prevDispalyData as MeshDisplayData).offset; (newDisplayData as MeshDisplayData).weight = (prevDispalyData as MeshDisplayData).weight; } _ReplaceSlotDisplay(dataPackage, newDisplayData, slot, displayIndex); } }
/** * @language zh_CN * 将指定骨架的动画替换成其他骨架的动画。 (通常这些骨架应该具有相同的骨架结构) * @param toArmature 指定的骨架。 * @param fromArmatreName 其他骨架的名称。 * @param fromSkinName 其他骨架的皮肤名称,如果未设置,则使用默认皮肤。 * @param fromDragonBonesDataName 其他骨架属于的龙骨数据名称,如果未设置,则检索所有的龙骨数据。 * @param ifRemoveOriginalAnimationList 是否移除原有的动画。 [true: 移除, false: 不移除] * @returns 是否替换成功。 [true: 成功, false: 不成功] * @see DragonBones.Armature * @version DragonBones 4.5 */ public bool CopyAnimationsToArmature( Armature toArmature, string fromArmatreName, string fromSkinName = null, string fromDragonBonesDataName = null, bool ifRemoveOriginalAnimationList = true ) { var dataPackage = new BuildArmaturePackage(); if (_fillBuildArmaturePackage(dataPackage, fromDragonBonesDataName, fromArmatreName, fromSkinName, null)) { var fromArmatureData = dataPackage.armature; if (ifRemoveOriginalAnimationList) { toArmature.animation.animations = fromArmatureData.animations; } else { var animations = new Dictionary <string, AnimationData>(); foreach (var pair in toArmature.animation.animations) { animations[pair.Key] = toArmature.animation.animations[pair.Key]; } foreach (var pair in fromArmatureData.animations) { animations[pair.Key] = fromArmatureData.animations[pair.Key]; } toArmature.animation.animations = animations; } if (dataPackage.skin != null) { var slots = toArmature.GetSlots(); for (int i = 0, l = slots.Count; i < l; ++i) { var toSlot = slots[i]; var toSlotDisplayList = toSlot.displayList; for (int iA = 0, lA = toSlotDisplayList.Count; iA < lA; ++iA) { var toDisplayObject = toSlotDisplayList[iA]; if (toDisplayObject is Armature) { var displays = dataPackage.skin.GetSlot(toSlot.name).displays; if (iA < displays.Count) { var fromDisplayData = displays[iA]; if (fromDisplayData.type == DisplayType.Armature) { CopyAnimationsToArmature((Armature)toDisplayObject, fromDisplayData.path, fromSkinName, fromDragonBonesDataName, ifRemoveOriginalAnimationList); } } } } } return(true); } } return(false); }
/** * @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 */ override protected Slot _generateSlot(BuildArmaturePackage dataPackage, SlotDisplayDataSet slotDisplayDataSet, Armature armature) { var slot = BaseObject.BorrowObject <UnitySlot>(); var slotData = slotDisplayDataSet.slot; var displayList = new List <object>(); var armatureDisplay = armature.display as GameObject; var transform = armatureDisplay.transform.Find(slotData.name); var gameObject = transform == null ? null : transform.gameObject; if (gameObject == null) { gameObject = new GameObject(slotData.name); gameObject.AddComponent <MeshRenderer>(); gameObject.AddComponent <MeshFilter>(); } slot.name = slotData.name; slot._rawDisplay = gameObject; slot._meshDisplay = slot._rawDisplay; 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 childDisplayName = slotData.name + " (" + displayData.name + ")"; var childTransform = armatureDisplay.transform.Find(childDisplayName); var childArmature = childTransform == null? this.BuildArmature(displayData.name, dataPackage.dataName) : BuildArmatureComponent(displayData.name, dataPackage.dataName, null, childTransform.gameObject).armature; if (childArmature != null) { if (childArmature._clock != null) { childArmature._clock.Remove(childArmature); } 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(); } } 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 abstract Slot _generateSlot(BuildArmaturePackage dataPackage, SkinSlotData skinSlotData, Armature armature);
/** * 将骨架的动画替换成其他骨架的动画。 (通常这些骨架应该具有相同的骨架结构) * @param toArmature 指定的骨架。 * @param fromArmatreName 其他骨架的名称。 * @param fromSkinName 其他骨架的皮肤名称,如果未设置,则使用默认皮肤。 * @param fromDragonBonesDataName 其他骨架属于的龙骨数据名称,如果未设置,则检索所有的龙骨数据。 * @param replaceOriginalAnimation 是否替换原有的同名动画。 * @returns 是否替换成功。 * @see dragonBones.Armature * @see dragonBones.ArmatureData * @version DragonBones 4.5 * @language zh_CN */ public bool CopyAnimationsToArmature(Armature toArmature, string fromArmatreName, string fromSkinName = null, string fromDragonBonesDataName = null, bool replaceOriginalAnimation = true) { var dataPackage = new BuildArmaturePackage(); if (this._FillBuildArmaturePackage(dataPackage, fromDragonBonesDataName, fromArmatreName, fromSkinName, "")) { var fromArmatureData = dataPackage.armature; if (replaceOriginalAnimation) { toArmature.animation.animations = fromArmatureData.animations; } else { Dictionary <string, AnimationData> animations = new Dictionary <string, AnimationData>(); foreach (var animationName in toArmature.animation.animations.Keys) { animations[animationName] = toArmature.animation.animations[animationName]; } foreach (var animationName in fromArmatureData.animations.Keys) { animations[animationName] = fromArmatureData.animations[animationName]; } toArmature.animation.animations = animations; } if (dataPackage.skin != null) { var slots = toArmature.GetSlots(); for (int i = 0, l = slots.Count; i < l; ++i) { var toSlot = slots[i]; var toSlotDisplayList = toSlot.displayList; for (int j = 0, lJ = toSlotDisplayList.Count; j < lJ; ++j) { var toDisplayObject = toSlotDisplayList[j]; if (toDisplayObject is Armature) { var displays = dataPackage.skin.GetDisplays(toSlot.name); if (displays != null && j < displays.Count) { var fromDisplayData = displays[j]; if (fromDisplayData != null && fromDisplayData.type == DisplayType.Armature) { this.CopyAnimationsToArmature(toDisplayObject as Armature, fromDisplayData.path, fromSkinName, fromDragonBonesDataName, replaceOriginalAnimation); } } } } } return(true); } } return(false); }
/// <private/> protected virtual Armature _BuildChildArmature(BuildArmaturePackage dataPackage, Slot slot, DisplayData displayData) { return(this.BuildArmature(displayData.path, dataPackage != null ? dataPackage.dataName : "", "", dataPackage != null ? dataPackage.textureAtlasName : "")); }
/** * @private */ protected abstract Armature _BuildArmature(BuildArmaturePackage dataPackage);
/** * @private */ protected abstract Slot _BuildSlot(BuildArmaturePackage dataPackage, SlotData slotData, List <DisplayData> displays, Armature armature);
/** * @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._BuildChildArmatrue(dataPackage, slot, displayData); //var childArmature = this.BuildArmature(armatureDisplayData.path, dataName, null, dataPackage != null ? dataPackage.textureAtlasName : null); 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) { childArmature._BufferAction(action, true); } } else { childArmature.animation.Play(); } } armatureDisplayData.armature = childArmature.armatureData; // } display = childArmature; break; case DisplayType.BoundingBox: break; } return(display); }
/** * @private */ protected bool _FillBuildArmaturePackage(BuildArmaturePackage dataPackage, string dragonBonesName, string armatureName, string skinName, string textureAtlasName) { DragonBonesData dragonBonesData = null; ArmatureData armatureData = null; var isAvailableName = !string.IsNullOrEmpty(dragonBonesName); if (isAvailableName) { if (this._dragonBonesDataMap.ContainsKey(dragonBonesName)) { dragonBonesData = this._dragonBonesDataMap[dragonBonesName]; armatureData = dragonBonesData.GetArmature(armatureName); } } if (armatureData == null && (!isAvailableName || this.autoSearch)) { // Will be search all data, if do not give a data name or the autoSearch is true. foreach (var key in this._dragonBonesDataMap.Keys) { dragonBonesData = this._dragonBonesDataMap[key]; if (!isAvailableName || dragonBonesData.autoSearch) { armatureData = dragonBonesData.GetArmature(armatureName); if (armatureData != null) { dragonBonesName = key; break; } } } } if (armatureData != null) { dataPackage.dataName = dragonBonesName; dataPackage.textureAtlasName = textureAtlasName; dataPackage.data = dragonBonesData; dataPackage.armature = armatureData; dataPackage.skin = null; if (!string.IsNullOrEmpty(skinName)) { dataPackage.skin = armatureData.GetSkin(skinName); if (dataPackage.skin == null && this.autoSearch) { foreach (var k in this._dragonBonesDataMap.Keys) { var skinDragonBonesData = this._dragonBonesDataMap[k]; var skinArmatureData = skinDragonBonesData.GetArmature(skinName); if (skinArmatureData != null) { dataPackage.skin = skinArmatureData.defaultSkin; break; } } } } if (dataPackage.skin == null) { dataPackage.skin = armatureData.defaultSkin; } return(true); } return(false); }
/// <private/> protected abstract Slot _BuildSlot(BuildArmaturePackage dataPackage, SlotData slotData, Armature armature);
/** * @private */ protected bool _fillBuildArmaturePackage(string dragonBonesName, string armatureName, string skinName, BuildArmaturePackage dataPackage) { var isAvailableName = !string.IsNullOrEmpty(dragonBonesName); if (isAvailableName) { if (_dragonBonesDataMap.ContainsKey(dragonBonesName)) { var dragonBonesData = _dragonBonesDataMap[dragonBonesName]; var armatureData = dragonBonesData.GetArmature(armatureName); if (armatureData != null) { dataPackage.dataName = dragonBonesName; dataPackage.data = dragonBonesData; dataPackage.armature = armatureData; dataPackage.skin = armatureData.GetSkin(skinName); if (dataPackage.skin == null) { dataPackage.skin = armatureData.defaultSkin; } return(true); } } } if (!isAvailableName || this.autoSearch) // Will be search all data, if do not give a data name or the autoSearch is true. { foreach (var pair in _dragonBonesDataMap) { if (!isAvailableName || pair.Value.autoSearch) { var armatureData = pair.Value.GetArmature(armatureName); if (armatureData != null) { dataPackage.dataName = pair.Key; dataPackage.data = pair.Value; dataPackage.armature = armatureData; dataPackage.skin = armatureData.GetSkin(skinName); if (dataPackage.skin == null) { dataPackage.skin = armatureData.defaultSkin; } return(true); } } } } return(false); }
/** * @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); }
/** * @private */ protected abstract Armature _generateArmature(BuildArmaturePackage dataPackage);
/** * @private */ protected abstract Slot _generateSlot(BuildArmaturePackage dataPackage, SlotDisplayDataSet slotDisplayDataSet, Armature armature);