/// <private/> protected void _RefreshTextureAtlas(UnityTextureAtlasData textureAtlasData, bool isUGUI, bool isEditor = false) { Material material = null; if (isUGUI && textureAtlasData.uiTexture == null) { if (isEditor) { #if UNITY_EDITOR if (!Application.isPlaying) { material = AssetDatabase.LoadAssetAtPath <Material>(textureAtlasData.imagePath + "_UI_Mat.mat"); } #endif } else { material = Resources.Load <Material>(textureAtlasData.imagePath + "_UI_Mat"); } if (material == null) { Texture2D textureAtlas = null; if (isEditor) { #if UNITY_EDITOR if (!Application.isPlaying) { textureAtlas = AssetDatabase.LoadAssetAtPath <Texture2D>(textureAtlasData.imagePath + ".png"); } #endif } else { textureAtlas = Resources.Load <Texture2D>(textureAtlasData.imagePath); } material = UnityFactoryHelper.GenerateMaterial(defaultUIShaderName, textureAtlas.name + "_UI_Mat", textureAtlas); if (textureAtlasData.width < 2) { textureAtlasData.width = (uint)textureAtlas.width; } if (textureAtlasData.height < 2) { textureAtlasData.height = (uint)textureAtlas.height; } textureAtlasData._disposeEnabled = true; #if UNITY_EDITOR if (!Application.isPlaying) { string path = AssetDatabase.GetAssetPath(textureAtlas); path = path.Substring(0, path.Length - 4); AssetDatabase.CreateAsset(material, path + "_UI_Mat.mat"); AssetDatabase.SaveAssets(); } #endif } // textureAtlasData.uiTexture = material; } else if (!isUGUI && textureAtlasData.texture == null) { if (isEditor) { #if UNITY_EDITOR if (!Application.isPlaying) { material = AssetDatabase.LoadAssetAtPath <Material>(textureAtlasData.imagePath + "_Mat.mat"); } #endif } else { material = Resources.Load <Material>(textureAtlasData.imagePath + "_Mat"); } if (material == null) { Texture2D textureAtlas = null; if (isEditor) { #if UNITY_EDITOR if (!Application.isPlaying) { textureAtlas = AssetDatabase.LoadAssetAtPath <Texture2D>(textureAtlasData.imagePath + ".png"); } #endif } else { textureAtlas = Resources.Load <Texture2D>(textureAtlasData.imagePath); } material = UnityFactoryHelper.GenerateMaterial(defaultShaderName, textureAtlas.name + "_Mat", textureAtlas); if (textureAtlasData.width < 2) { textureAtlasData.width = (uint)textureAtlas.width; } if (textureAtlasData.height < 2) { textureAtlasData.height = (uint)textureAtlas.height; } textureAtlasData._disposeEnabled = true; #if UNITY_EDITOR if (!Application.isPlaying) { string path = AssetDatabase.GetAssetPath(textureAtlas); path = path.Substring(0, path.Length - 4); AssetDatabase.CreateAsset(material, path + "_Mat.mat"); AssetDatabase.SaveAssets(); } #endif } textureAtlasData.texture = material; } }
/// <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); }