/** * @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; int index = 0; foreach (var displayData in slotDisplayDataSet.displays) { if (displayData.name == displayName) { prevDispalyData = displayData; break; } ++index; } 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); } } }
/// <summary> /// 用特定的显示对象数据替换特定插槽当前的显示对象数据。 /// </summary> /// <param name="dragonBonesName">The DragonBonesData instance cache name</param> /// <param name="armatureName">The armature data name</param> /// <param name="slotName">The slot data name</param> /// <param name="displayName">The display data name</param> /// <param name="slot">The slot</param> /// <param name="texture">The new texture</param> /// <param name="material">The new material</param> /// <param name="isUGUI">is ugui。</param> /// <param name="displayIndex">The index of the display data that is replaced. (If it is not set, replaces the current display data)</param> /// <version>DragonBones 4.5</version> /// <language>zh_CN</language> /// <summary> /// 用特定的显示对象数据替换特定插槽当前的显示对象数据。 /// </summary> /// <param name="dragonBonesName">指定的龙骨数据名称。</param> /// <param name="armatureName">指定的骨架名称。</param> /// <param name="slotName">指定的插槽名称。</param> /// <param name="displayName">指定的显示对象名称。</param> /// <param name="slot">指定的插槽实例。</param> /// <param name="texture">新的贴图。</param> /// <param name="material">新的材质。</param> /// <param name="isUGUI">是否为ugui。</param> /// <param name="displayIndex">被替换的显示对象数据的索引。 (如果未设置,则替换当前的显示对象数据)。</param> /// <version>DragonBones 4.5</version> /// <language>zh_CN</language> public void ReplaceSlotDisplay( string dragonBonesName, string armatureName, string slotName, string displayName, Slot slot, Texture2D texture, Material material, bool isUGUI = false, int displayIndex = -1) { var armatureData = this.GetArmatureData(armatureName, dragonBonesName); if (armatureData == null || armatureData.defaultSkin == null) { return; } var displays = armatureData.defaultSkin.GetDisplays(slotName); if (displays == null) { return; } DisplayData prevDispalyData = null; foreach (var displayData in displays) { if (displayData.name == displayName) { prevDispalyData = displayData; break; } } 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; } ReplaceDisplay(slot, newDisplayData, displayIndex); }
/// <summary> /// 用特定的显示对象数据替换特定插槽当前的显示对象数据。 /// </summary> /// <param name="dragonBonesName">The DragonBonesData instance cache name</param> /// <param name="armatureName">The armature data name</param> /// <param name="slotName">The slot data name</param> /// <param name="displayName">The display data name</param> /// <param name="slot">The slot</param> /// <param name="texture">The new texture</param> /// <param name="material">The new material</param> /// <param name="isUGUI">is ugui。</param> /// <param name="displayIndex">The index of the display data that is replaced. (If it is not set, replaces the current display data)</param> /// <version>DragonBones 4.5</version> /// <language>zh_CN</language> /// <summary> /// 用特定的显示对象数据替换特定插槽当前的显示对象数据。 /// </summary> /// <param name="dragonBonesName">指定的龙骨数据名称。</param> /// <param name="armatureName">指定的骨架名称。</param> /// <param name="slotName">指定的插槽名称。</param> /// <param name="displayName">指定的显示对象名称。</param> /// <param name="slot">指定的插槽实例。</param> /// <param name="texture">新的贴图。</param> /// <param name="material">新的材质。</param> /// <param name="isUGUI">是否为ugui。</param> /// <param name="displayIndex">被替换的显示对象数据的索引。 (如果未设置,则替换当前的显示对象数据)。</param> /// <version>DragonBones 4.5</version> /// <language>zh_CN</language> public void ReplaceSlotDisplay( string dragonBonesName, string armatureName, string slotName, string displayName, Slot slot, Texture2D texture, Material material = null, bool isUGUI = false, int displayIndex = -1) { var armatureData = this.GetArmatureData(armatureName, dragonBonesName); if (armatureData == null || armatureData.defaultSkin == null) { return; } var displays = armatureData.defaultSkin.GetDisplays(slotName); if (displays == null) { return; } DisplayData prevDispalyData = null; foreach (var displayData in displays) { if (displayData.name == displayName) { prevDispalyData = displayData; break; } } if (prevDispalyData == null || !((prevDispalyData is ImageDisplayData) || (prevDispalyData is MeshDisplayData))) { return; } TextureData prevTextureData = null; if (prevDispalyData is ImageDisplayData) { prevTextureData = (prevDispalyData as ImageDisplayData).texture; } else { prevTextureData = (prevDispalyData as MeshDisplayData).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; newTextureData.parent.scale = prevTextureData.parent.scale; // if (material == null) { if (isUGUI) { material = UnityFactoryHelper.GenerateMaterial(defaultUIShaderName, texture.name + "_UI_Mat", texture); } else { material = UnityFactoryHelper.GenerateMaterial(defaultShaderName, texture.name + "_Mat", texture); } } if (isUGUI) { (newTextureData.parent as UnityTextureAtlasData).uiTexture = material; } else { (newTextureData.parent as UnityTextureAtlasData).texture = material; } material.mainTexture = texture; DisplayData newDisplayData = null; if (prevDispalyData is ImageDisplayData) { newDisplayData = new ImageDisplayData(); newDisplayData.type = prevDispalyData.type; newDisplayData.name = prevDispalyData.name; newDisplayData.path = prevDispalyData.path; newDisplayData.transform.CopyFrom(prevDispalyData.transform); newDisplayData.parent = prevDispalyData.parent; (newDisplayData as ImageDisplayData).pivot.CopyFrom((prevDispalyData as ImageDisplayData).pivot); (newDisplayData as ImageDisplayData).texture = newTextureData; } else if (prevDispalyData is MeshDisplayData) { newDisplayData = new MeshDisplayData(); newDisplayData.type = prevDispalyData.type; newDisplayData.name = prevDispalyData.name; newDisplayData.path = prevDispalyData.path; newDisplayData.transform.CopyFrom(prevDispalyData.transform); newDisplayData.parent = prevDispalyData.parent; (newDisplayData as MeshDisplayData).texture = newTextureData; (newDisplayData as MeshDisplayData).vertices.inheritDeform = (prevDispalyData as MeshDisplayData).vertices.inheritDeform; (newDisplayData as MeshDisplayData).vertices.offset = (prevDispalyData as MeshDisplayData).vertices.offset; (newDisplayData as MeshDisplayData).vertices.data = (prevDispalyData as MeshDisplayData).vertices.data; (newDisplayData as MeshDisplayData).vertices.weight = (prevDispalyData as MeshDisplayData).vertices.weight; } ReplaceDisplay(slot, newDisplayData, displayIndex); }