private void SetSprite(SlotAndAttachment slot, Sprite sprite) { _attachmentNames.Clear(); _clonedSkin.FindNamesForSlot(slot.SlotIndex, _attachmentNames); var attachmentName = _attachmentNames.Count > 0 ? _attachmentNames[0] : string.Empty; if (string.IsNullOrEmpty(attachmentName)) { _clonedSkin.AddAttachment( slot.SlotIndex, slot.Name, slot.Attachment); } else if (sprite is null) { _clonedSkin.SetAttachment( slot.SlotIndex, attachmentName, slot.Attachment); } else { var attachment = RemapAttachment(slot.Slot, sprite); _clonedSkin.SetAttachment(slot.SlotIndex, attachmentName, attachment); } }
public static Attachment AddUnitySprite(this SkeletonData skeletonData, string slotName, Sprite sprite, string skinName = "", string shaderName = "Spine/Skeleton") { RegionAttachment regionAttachment = sprite.ToRegionAttachment(shaderName); int slotIndex = skeletonData.FindSlotIndex(slotName); Skin skin = skeletonData.defaultSkin; if (skinName != string.Empty) { skin = skeletonData.FindSkin(skinName); } skin.AddAttachment(slotIndex, regionAttachment.Name, regionAttachment); return(regionAttachment); }
public static Attachment AddUnitySprite(this SkeletonData skeletonData, string slotName, UnityEngine.Sprite sprite, string skinName, Shader shader, bool applyPMA) { RegionAttachment regionAttachment = sprite.ToRegionAttachment(shader, applyPMA); int slotIndex = skeletonData.FindSlotIndex(slotName); Skin skin = skeletonData.defaultSkin; if (skinName != string.Empty) { skin = skeletonData.FindSkin(skinName); } skin.AddAttachment(slotIndex, regionAttachment.Name, regionAttachment); return(regionAttachment); }
private void Start() { var slotIndex = _skeletonAnimation.skeleton.FindSlotIndex("sword"); var templateSkin = _skeletonAnimation.SkeletonDataAsset.GetSkeletonData(false).FindSkin("default"); var templateAttachment = templateSkin.GetAttachment(slotIndex, "sword"); var attachment = templateAttachment.GetRemappedClone(_attachment, _skeletonAnimation.skeletonDataAsset.atlasAssets[0].materials[0], true); var skin = new Skin("EquipSkin"); skin.Append(_skeletonAnimation.SkeletonDataAsset.GetSkeletonData(false).FindSkin("knight")); skin.AddAttachment(slotIndex, "sword", attachment); _skeletonAnimation.Skeleton.SetSkin(skin); _skeletonAnimation.Skeleton.SetSlotsToSetupPose(); }
public static RegionAttachment AddUnitySprite(this SkeletonData skeletonData, string slotName, Sprite sprite, string skinName, Shader shader, bool applyPMA, float rotation = 0f) { RegionAttachment attachment = !applyPMA?sprite.ToRegionAttachment(new Material(shader), rotation) : sprite.ToRegionAttachmentPMAClone(shader, TextureFormat.RGBA32, false, null, rotation); int slotIndex = skeletonData.FindSlotIndex(slotName); Skin defaultSkin = skeletonData.DefaultSkin; if (skinName != string.Empty) { defaultSkin = skeletonData.FindSkin(skinName); } defaultSkin.AddAttachment(slotIndex, attachment.Name, attachment); return(attachment); }
public static void SetAttachment(this Skin skin, string slotName, string keyName, Attachment attachment, Skeleton skeleton) { int slotIndex = skeleton.FindSlotIndex(slotName); if (skeleton == null) { throw new ArgumentNullException("skeleton", "skeleton cannot be null."); } if (slotIndex == -1) { throw new ArgumentException($"Slot '{slotName}' does not exist in skeleton.", "slotName"); } skin.AddAttachment(slotIndex, keyName, attachment); }
void Start() { skeletonRenderer = GetComponent <SkeletonRenderer>(); Skeleton skeleton = skeletonRenderer.skeleton; customSkin = new Skin("CustomSkin"); foreach (var pair in skinning) { var attachment = SpineAttachment.GetAttachment(pair.sourceAttachment, skinSource); customSkin.AddAttachment(skeleton.FindSlotIndex(pair.targetSlot), pair.targetAttachment, attachment); } skeleton.SetSkin(customSkin); }
public static Attachment AddUnitySprite(this SkeletonData skeletonData, string slotName, Sprite sprite, string skinName = "", string shaderName = "Spine/Skeleton") { var att = sprite.ToRegionAttachment(shaderName); var slotIndex = skeletonData.FindSlotIndex(slotName); Skin skin = skeletonData.defaultSkin; if (skinName != "") { skin = skeletonData.FindSkin(skinName); } skin.AddAttachment(slotIndex, att.Name, att); return(att); }
public static RegionAttachment AddUnitySprite(this SkeletonData skeletonData, string slotName, Sprite sprite, string skinName, Shader shader, bool applyPMA) { RegionAttachment att = applyPMA ? sprite.ToRegionAttachmentPMAClone(shader) : sprite.ToRegionAttachment(new Material(shader)); var slotIndex = skeletonData.FindSlotIndex(slotName); Skin skin = skeletonData.DefaultSkin; if (skinName != "") { skin = skeletonData.FindSkin(skinName); } skin.AddAttachment(slotIndex, att.Name, att); return(att); }
void Start() { skeletonRenderer = GetComponent <SkeletonRenderer>(); Skeleton skeleton = skeletonRenderer.skeleton; customSkin = new Skin("CustomSkin"); foreach (var pair in skinItems) { var attachment = SpineAttachment.GetAttachment(pair.sourceAttachmentPath, skinSource); customSkin.AddAttachment(skeleton.FindSlotIndex(pair.targetSlot), pair.targetAttachment, attachment); } // The custom skin does not need to be added to the skeleton data for it to work. // But it's useful for your script to keep a reference to it. skeleton.SetSkin(customSkin); }
/// <summary> /// Creates and populates a duplicate skin with cloned attachments that are backed by a new packed texture atlas comprised of all the regions from the original skin.</summary> /// <remarks>No Spine.Atlas object is created so there is no way to find AtlasRegions except through the Attachments using them.</remarks> public static Skin GetRepackedSkin(this Skin o, string newName, Shader shader, out Material m, out Texture2D t, int maxAtlasSize = 1024, int padding = 2) { var skinAttachments = o.Attachments; var newSkin = new Skin(newName); var repackedAttachments = new List <Attachment>(); var texturesToPack = new List <Texture2D>(); foreach (var kvp in skinAttachments) { var newAttachment = kvp.Value.GetClone(true); if (IsRenderable(newAttachment)) { texturesToPack.Add(newAttachment.GetAtlasRegion().ToTexture()); repackedAttachments.Add(newAttachment); } var key = kvp.Key; newSkin.AddAttachment(key.slotIndex, key.name, newAttachment); } var newTexture = new Texture2D(maxAtlasSize, maxAtlasSize); newTexture.name = newName; var rects = newTexture.PackTextures(texturesToPack.ToArray(), padding, maxAtlasSize); var newMaterial = new Material(shader); newMaterial.name = newName; newMaterial.mainTexture = newTexture; var page = newMaterial.ToSpineAtlasPage(); page.name = newName; for (int i = 0, n = repackedAttachments.Count; i < n; i++) { var a = repackedAttachments[i]; var r = rects[i]; var oldRegion = a.GetAtlasRegion(); var newRegion = UVRectToAtlasRegion(r, oldRegion.name, page, oldRegion.offsetX, oldRegion.offsetY, oldRegion.rotate); a.SetRegion(newRegion); } t = newTexture; m = newMaterial; return(newSkin); }
public static void AddToSkin(Skeleton skeleton, Skin skin, Skin otherSkin) { ExposedList <Slot> slots = skeleton.Slots; for (int i = 0, n = slots.Count; i < n; i++) { Slot slot = slots.Items[i]; string name = slot.Data.AttachmentName; if (name != null) { Attachment attachment = otherSkin.GetAttachment(i, name); if (attachment != null) { skin.AddAttachment(i, name, attachment); } } } }
Skin readSkin(string skinName, bool nonessential) { int slotCount = ReadInt(); if (slotCount == 0) { return(null); } Skin skin = new Skin(skinName); for (int i = 0; i < slotCount; i++) { int slotIndex = ReadInt(); int jCount = ReadInt(); for (int j = 0; j < jCount; j++) { string name = ReadString(); skin.AddAttachment(slotIndex, name, readAttachment(skin, name, nonessential)); } } return(skin); }
// // Use this for initialization void Start() { SkeletonAnimation _ani = this.GetComponent <SkeletonAnimation>(); Skeleton _skeleton = _ani.skeleton; Skin _newSkin = _skeleton.UnshareSkin(true, false, _ani.AnimationState); //HeadFront Setting RegionAttachment _newHeadFront = headFront.ToRegionAttachmentPMAClone(Shader.Find("Spine/Skeleton")); _newHeadFront.SetScale(1f, 1f); _newHeadFront.UpdateOffset(); int headFrontSlotIndex = _skeleton.FindSlotIndex(headFrontSlot); _newSkin.AddAttachment(headFrontSlotIndex, headFront.name, _newHeadFront); //repack _newSkin = _newSkin.GetRepackedSkin("repacked", repackedShader, out runtimeMaterial, out runtimeAtlas); _skeleton.SetSkin(_newSkin); _skeleton.SetToSetupPose(); //_skeleton.SetToSetupPose(); _skeleton.SetAttachment(headFrontSlot, headFront.name); }
/// <summary> /// Creates and populates a duplicate skin with cloned attachments that are backed by a new packed texture atlas comprised of all the regions from the original skin.</summary> /// <remarks>No Spine.Atlas object is created so there is no way to find AtlasRegions except through the Attachments using them.</remarks> public static Skin GetRepackedSkin(this Skin o, string newName, Shader shader, out Material m, out Texture2D t, int maxAtlasSize = 1024, int padding = 2, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps) { var skinAttachments = o.Attachments; var newSkin = new Skin(newName); var existingRegions = new Dictionary <AtlasRegion, int>(); var regionIndexes = new List <int>(); var repackedAttachments = new List <Attachment>(); var texturesToPack = new List <Texture2D>(); var originalRegions = new List <AtlasRegion>(); int newRegionIndex = 0; foreach (var kvp in skinAttachments) { var newAttachment = kvp.Value.GetClone(true); if (IsRenderable(newAttachment)) { var region = newAttachment.GetAtlasRegion(); int existingIndex; if (existingRegions.TryGetValue(region, out existingIndex)) { regionIndexes.Add(existingIndex); // Store the region index for the eventual new attachment. } else { originalRegions.Add(region); texturesToPack.Add(region.ToTexture()); // Add the texture to the PackTextures argument existingRegions.Add(region, newRegionIndex); // Add the region to the dictionary of known regions regionIndexes.Add(newRegionIndex); // Store the region index for the eventual new attachment. newRegionIndex++; } repackedAttachments.Add(newAttachment); } var key = kvp.Key; newSkin.AddAttachment(key.slotIndex, key.name, newAttachment); } var newTexture = new Texture2D(maxAtlasSize, maxAtlasSize, textureFormat, mipmaps); newTexture.anisoLevel = texturesToPack[0].anisoLevel; newTexture.name = newName; var rects = newTexture.PackTextures(texturesToPack.ToArray(), padding, maxAtlasSize); var newMaterial = new Material(shader); newMaterial.name = newName; newMaterial.mainTexture = newTexture; var page = newMaterial.ToSpineAtlasPage(); page.name = newName; var repackedRegions = new List <AtlasRegion>(); for (int i = 0, n = originalRegions.Count; i < n; i++) { var oldRegion = originalRegions[i]; var newRegion = UVRectToAtlasRegion(rects[i], oldRegion.name, page, oldRegion.offsetX, oldRegion.offsetY, oldRegion.rotate); repackedRegions.Add(newRegion); } for (int i = 0, n = repackedAttachments.Count; i < n; i++) { var a = repackedAttachments[i]; a.SetRegion(repackedRegions[regionIndexes[i]]); } t = newTexture; m = newMaterial; return(newSkin); }
public static Skin GetRepackedSkin(this Skin o, string newName, Shader shader, out Material outputMaterial, out Texture2D outputTexture, int maxAtlasSize = 0x400, int padding = 2, TextureFormat textureFormat = 4, bool mipmaps = false, Material materialPropertySource = null, bool clearCache = false, bool useOriginalNonrenderables = true) { if (o == null) { throw new NullReferenceException("Skin was null"); } Dictionary <Skin.AttachmentKeyTuple, Attachment> attachments = o.Attachments; Skin skin = new Skin(newName); Dictionary <AtlasRegion, int> dictionary2 = new Dictionary <AtlasRegion, int>(); List <int> list = new List <int>(); List <Attachment> list2 = new List <Attachment>(); List <Texture2D> list3 = new List <Texture2D>(); List <AtlasRegion> list4 = new List <AtlasRegion>(); int num = 0; foreach (KeyValuePair <Skin.AttachmentKeyTuple, Attachment> pair in attachments) { Skin.AttachmentKeyTuple key = pair.Key; Attachment a = pair.Value; if (IsRenderable(a)) { Attachment clone = a.GetClone(true); AtlasRegion region = clone.GetRegion(); if (dictionary2.TryGetValue(region, out int num2)) { list.Add(num2); } else { list4.Add(region); list3.Add(region.ToTexture(true, TextureFormat.RGBA32, false)); dictionary2.Add(region, num); list.Add(num); num++; } list2.Add(clone); skin.AddAttachment(key.slotIndex, key.name, clone); } else { skin.AddAttachment(key.slotIndex, key.name, !useOriginalNonrenderables ? a.GetClone(true) : a); } } Texture2D textured = new Texture2D(maxAtlasSize, maxAtlasSize, textureFormat, mipmaps) { mipMapBias = -0.5f, anisoLevel = list3[0].anisoLevel, name = newName }; Rect[] rectArray = textured.PackTextures(list3.ToArray(), padding, maxAtlasSize); Material m = new Material(shader); if (materialPropertySource != null) { m.CopyPropertiesFromMaterial(materialPropertySource); m.shaderKeywords = materialPropertySource.shaderKeywords; } m.name = newName; m.mainTexture = textured; AtlasPage page = m.ToSpineAtlasPage(); page.name = newName; List <AtlasRegion> list5 = new List <AtlasRegion>(); int index = 0; int count = list4.Count; while (index < count) { AtlasRegion region2 = list4[index]; AtlasRegion item = UVRectToAtlasRegion(rectArray[index], region2.name, page, region2.offsetX, region2.offsetY, region2.rotate); list5.Add(item); index++; } int num5 = 0; int num6 = list2.Count; while (num5 < num6) { Attachment a = list2[num5]; if (IsRenderable(a)) { a.SetRegion(list5[list[num5]], true); } num5++; } if (clearCache) { ClearCache(); } outputTexture = textured; outputMaterial = m; return(skin); }
private void ReadSkeletonSkins(SkeletonData skeletonData, JObject data, float scale) { foreach (JProperty skin in data["skins"]) { var skeletonSkin = new Skin(skin.Name); foreach (JProperty slot in skin.Value) { int slotIndex = skeletonData.FindSlotIndex(slot.Name); foreach (JProperty attachment in slot.Value) { Attachment skeletonAttachment = ReadSkeletonAttachment(attachment.Name, attachment.Value, scale); if (attachment != null) { skeletonSkin.AddAttachment(slotIndex, attachment.Name, skeletonAttachment); } } } skeletonData.AddSkin(skeletonSkin); if (skeletonSkin.Name.Equals("default")) { skeletonData.DefaultSkin = skeletonSkin; } } }
public static Skin GetRepackedSkin(this Skin o, string newName, Shader shader, out Material m, out Texture2D t, int maxAtlasSize = 1024, int padding = 2, TextureFormat textureFormat = TextureFormat.RGBA32, bool mipmaps = false, Material materialPropertySource = null) { Dictionary <Skin.AttachmentKeyTuple, Attachment> attachments = o.Attachments; Skin skin = new Skin(newName); Dictionary <AtlasRegion, int> dictionary = new Dictionary <AtlasRegion, int>(); List <int> list = new List <int>(); List <Attachment> list2 = new List <Attachment>(); List <Texture2D> list3 = new List <Texture2D>(); List <AtlasRegion> list4 = new List <AtlasRegion>(); int num = 0; foreach (KeyValuePair <Skin.AttachmentKeyTuple, Attachment> item2 in attachments) { Attachment clone = item2.Value.GetClone(cloneMeshesAsLinked: true); if (IsRenderable(clone)) { AtlasRegion atlasRegion = clone.GetAtlasRegion(); if (dictionary.TryGetValue(atlasRegion, out int value)) { list.Add(value); } else { list4.Add(atlasRegion); list3.Add(atlasRegion.ToTexture()); dictionary.Add(atlasRegion, num); list.Add(num); num++; } list2.Add(clone); } Skin.AttachmentKeyTuple key = item2.Key; skin.AddAttachment(key.slotIndex, key.name, clone); } Texture2D texture2D = new Texture2D(maxAtlasSize, maxAtlasSize, textureFormat, mipmaps); texture2D.mipMapBias = -0.5f; texture2D.anisoLevel = list3[0].anisoLevel; texture2D.name = newName; Rect[] array = texture2D.PackTextures(list3.ToArray(), padding, maxAtlasSize); Material material = new Material(shader); if (materialPropertySource != null) { material.CopyPropertiesFromMaterial(materialPropertySource); material.shaderKeywords = materialPropertySource.shaderKeywords; } material.name = newName; material.mainTexture = texture2D; AtlasPage atlasPage = material.ToSpineAtlasPage(); atlasPage.name = newName; List <AtlasRegion> list5 = new List <AtlasRegion>(); int i = 0; for (int count = list4.Count; i < count; i++) { AtlasRegion atlasRegion2 = list4[i]; AtlasRegion item = UVRectToAtlasRegion(array[i], atlasRegion2.name, atlasPage, atlasRegion2.offsetX, atlasRegion2.offsetY, atlasRegion2.rotate); list5.Add(item); } int j = 0; for (int count2 = list2.Count; j < count2; j++) { Attachment attachment = list2[j]; attachment.SetRegion(list5[list[j]]); } t = texture2D; m = material; return(skin); }
public static void SetAttachment(this Skin skin, int slotIndex, string keyName, Attachment attachment) { skin.AddAttachment(slotIndex, keyName, attachment); }
/// <summary> /// Creates and populates a duplicate skin with cloned attachments that are backed by a new packed texture atlas comprised of all the regions from the original skin.</summary> /// <remarks>No Spine.Atlas object is created so there is no way to find AtlasRegions except through the Attachments using them.</remarks> public static Skin GetRepackedSkin (this Skin o, string newName, Shader shader, out Material m, out Texture2D t, int maxAtlasSize = 1024, int padding = 2) { var skinAttachments = o.Attachments; var newSkin = new Skin(newName); var repackedAttachments = new List<Attachment>(); var texturesToPack = new List<Texture2D>(); foreach (var kvp in skinAttachments) { var newAttachment = kvp.Value.GetClone(true); if (IsRenderable(newAttachment)) { texturesToPack.Add(newAttachment.GetAtlasRegion().ToTexture()); repackedAttachments.Add(newAttachment); } var key = kvp.Key; newSkin.AddAttachment(key.slotIndex, key.name, newAttachment); } var newTexture = new Texture2D(maxAtlasSize, maxAtlasSize); newTexture.name = newName; var rects = newTexture.PackTextures(texturesToPack.ToArray(), padding, maxAtlasSize); var newMaterial = new Material(shader); newMaterial.name = newName; newMaterial.mainTexture = newTexture; var page = newMaterial.ToSpineAtlasPage(); page.name = newName; for (int i = 0, n = repackedAttachments.Count; i < n; i++) { var a = repackedAttachments[i]; var r = rects[i]; var oldRegion = a.GetAtlasRegion(); var newRegion = UVRectToAtlasRegion(r, oldRegion.name, page, oldRegion.offsetX, oldRegion.offsetY, oldRegion.rotate); a.SetRegion(newRegion); } t = newTexture; m = newMaterial; return newSkin; }
public SkeletonData ReadSkeletonData (TextReader reader) { if (reader == null) throw new ArgumentNullException("reader cannot be null."); SkeletonData skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary<String, Object>; if (root == null) throw new Exception("Invalid JSON."); // Bones. foreach (Dictionary<String, Object> boneMap in (List<Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) throw new Exception("Parent bone not found: " + boneMap["parent"]); } BoneData boneData = new BoneData((String)boneMap["name"], parent); boneData.length = GetFloat(boneMap, "length", 0) * Scale; boneData.x = GetFloat(boneMap, "x", 0) * Scale; boneData.y = GetFloat(boneMap, "y", 0) * Scale; boneData.rotation = GetFloat(boneMap, "rotation", 0); boneData.scaleX = GetFloat(boneMap, "scaleX", 1); boneData.scaleY = GetFloat(boneMap, "scaleY", 1); boneData.inheritScale = GetBoolean(boneMap, "inheritScale", true); boneData.inheritRotation = GetBoolean(boneMap, "inheritRotation", true); skeletonData.AddBone(boneData); } // Slots. if (root.ContainsKey("slots")) { foreach (Dictionary<String, Object> slotMap in (List<Object>)root["slots"]) { String slotName = (String)slotMap["name"]; String boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) throw new Exception("Slot bone not found: " + boneName); SlotData slotData = new SlotData(slotName, boneData); if (slotMap.ContainsKey("color")) { String color = (String)slotMap["color"]; slotData.r = ToColor(color, 0); slotData.g = ToColor(color, 1); slotData.b = ToColor(color, 2); slotData.a = ToColor(color, 3); } if (slotMap.ContainsKey("attachment")) slotData.attachmentName = (String)slotMap["attachment"]; if (slotMap.ContainsKey("additive")) slotData.additiveBlending = (bool)slotMap["additive"]; skeletonData.AddSlot(slotData); } } // Skins. if (root.ContainsKey("skins")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["skins"]) { Skin skin = new Skin(entry.Key); foreach (KeyValuePair<String, Object> slotEntry in (Dictionary<String, Object>)entry.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair<String, Object> attachmentEntry in ((Dictionary<String, Object>)slotEntry.Value)) { Attachment attachment = ReadAttachment(skin, attachmentEntry.Key, (Dictionary<String, Object>)attachmentEntry.Value); if (attachment != null) skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment); } } skeletonData.AddSkin(skin); if (skin.name == "default") skeletonData.defaultSkin = skin; } } // Events. if (root.ContainsKey("events")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["events"]) { var entryMap = (Dictionary<String, Object>)entry.Value; EventData eventData = new EventData(entry.Key); eventData.Int = GetInt(entryMap, "int", 0); eventData.Float = GetFloat(entryMap, "float", 0); eventData.String = GetString(entryMap, "string", null); skeletonData.AddEvent(eventData); } } // Animations. if (root.ContainsKey("animations")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["animations"]) ReadAnimation(entry.Key, (Dictionary<String, Object>)entry.Value, skeletonData); } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.animations.TrimExcess(); return skeletonData; }
/// <summary> /// Creates and populates a duplicate skin with cloned attachments that are backed by a new packed texture atlas comprised of all the regions from the original skin.</summary> /// <remarks>No Spine.Atlas object is created so there is no way to find AtlasRegions except through the Attachments using them.</remarks> public static Skin GetRepackedSkin(this Skin o, string newName, Shader shader, out Material m, out Texture2D t, int maxAtlasSize = 1024, int padding = 2, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, Material materialPropertySource = null) { var skinAttachments = o.Attachments; var newSkin = new Skin(newName); // Use these to detect and use shared regions. var existingRegions = new Dictionary <AtlasRegion, int>(); var regionIndexes = new List <int>(); // Collect all textures from the attachments of the original skin. var repackedAttachments = new List <Attachment>(); var texturesToPack = new List <Texture2D>(); var originalRegions = new List <AtlasRegion>(); int newRegionIndex = 0; foreach (var kvp in skinAttachments) { var newAttachment = kvp.Value.GetClone(true); if (IsRenderable(newAttachment)) { var region = newAttachment.GetAtlasRegion(); int existingIndex; if (existingRegions.TryGetValue(region, out existingIndex)) { regionIndexes.Add(existingIndex); // Store the region index for the eventual new attachment. } else { originalRegions.Add(region); texturesToPack.Add(region.ToTexture()); // Add the texture to the PackTextures argument existingRegions.Add(region, newRegionIndex); // Add the region to the dictionary of known regions regionIndexes.Add(newRegionIndex); // Store the region index for the eventual new attachment. newRegionIndex++; } repackedAttachments.Add(newAttachment); } var key = kvp.Key; newSkin.AddAttachment(key.slotIndex, key.name, newAttachment); } // Fill a new texture with the collected attachment textures. var newTexture = new Texture2D(maxAtlasSize, maxAtlasSize, textureFormat, mipmaps); newTexture.anisoLevel = texturesToPack[0].anisoLevel; newTexture.name = newName; var rects = newTexture.PackTextures(texturesToPack.ToArray(), padding, maxAtlasSize); // Rehydrate the repacked textures as a Material, Spine atlas and Spine.AtlasAttachments var newMaterial = new Material(shader); if (materialPropertySource != null) { newMaterial.CopyPropertiesFromMaterial(materialPropertySource); newMaterial.shaderKeywords = materialPropertySource.shaderKeywords; } newMaterial.name = newName; newMaterial.mainTexture = newTexture; var page = newMaterial.ToSpineAtlasPage(); page.name = newName; var repackedRegions = new List <AtlasRegion>(); for (int i = 0, n = originalRegions.Count; i < n; i++) { var oldRegion = originalRegions[i]; var newRegion = UVRectToAtlasRegion(rects[i], oldRegion.name, page, oldRegion.offsetX, oldRegion.offsetY, oldRegion.rotate); repackedRegions.Add(newRegion); } // Map the cloned attachments to the repacked atlas. for (int i = 0, n = repackedAttachments.Count; i < n; i++) { var a = repackedAttachments[i]; a.SetRegion(repackedRegions[regionIndexes[i]]); } // Clean up foreach (var ttp in texturesToPack) { UnityEngine.Object.Destroy(ttp); } t = newTexture; m = newMaterial; return(newSkin); }
public static void GetSpineRepackedSkin(CombineTextureData combineTextureData, Skin skin, string newName, Shader shader, int maxAtlasSize, TextureFormat textureFormat, bool mipMaps, Material materialPropertySource, bool clearCache) { if (skin == null) { throw new System.NullReferenceException("Skin was null"); } var skinAttachments = skin.Attachments; var newSkin = new Skin(newName); var existingRegions = new Dictionary <AtlasRegion, int> (); var regionIndexes = new List <int> (); var repackedAttachments = new List <Attachment> (); var texturesToPack = new List <Texture2D> (); var originalRegions = new List <AtlasRegion> (); int newRegionIndex = 0; foreach (var kvp in skinAttachments) { var newAttachment = kvp.Value.GetClone(true); if (IsRenderable(newAttachment)) { var region = newAttachment.GetAtlasRegion(); int existingIndex; if (existingRegions.TryGetValue(region, out existingIndex)) { regionIndexes.Add(existingIndex); // Store the region index for the eventual new attachment. } else { originalRegions.Add(region); //texturesToPack.Add (region.ToTexture ()); // Add the texture to the PackTextures argument existingRegions.Add(region, newRegionIndex); // Add the region to the dictionary of known regions regionIndexes.Add(newRegionIndex); // Store the region index for the eventual new attachment. newRegionIndex++; } repackedAttachments.Add(newAttachment); } var key = kvp.Key; newSkin.AddAttachment(key.slotIndex, key.name, newAttachment); } //合并图集 if (null != combineTextureData.combineCoroutineSmall) { //region.ToTexture ()??? GameManager.Instance.StopCoroutine(combineTextureData.combineCoroutineSmall); } combineTextureData.combineCoroutineSmall = GameManager.Instance.StartCoroutine(CombineTexturesTool.Instance.GetRenderTextureByTexturesOptimize(combineTextureData, originalRegions, maxAtlasSize, (PackagedTextureResult result) => { RenderTexture renderTexture = result.renderTexture; TextureClass[] outTextureClassList = result.textureInfo; var newMaterial = new Material(shader); if (materialPropertySource != null) { newMaterial.CopyPropertiesFromMaterial(materialPropertySource); newMaterial.shaderKeywords = materialPropertySource.shaderKeywords; } newMaterial.name = newName; newMaterial.mainTexture = renderTexture; var page = newMaterial.ToSpineAtlasPage(); page.name = newName; var repackedRegions = new List <AtlasRegion> (); for (int i = 0, n = originalRegions.Count; i < n; i++) { var oldRegion = originalRegions[i]; var newRegion = UVRectToAtlasRegion(GetTargetRectByIndex(outTextureClassList, i), oldRegion.name, page, oldRegion.offsetX, oldRegion.offsetY, oldRegion.rotate); repackedRegions.Add(newRegion); } for (int i = 0, n = repackedAttachments.Count; i < n; i++) { var a = repackedAttachments[i]; a.SetRegion(repackedRegions[regionIndexes[i]]); } if (clearCache) { AtlasUtilities.ClearCache(); } if (null != combineTextureData.resultData) { combineTextureData.resultData.skin = null; combineTextureData.resultData.material = null; if (null != combineTextureData.resultData.currentRenderTex) { combineTextureData.resultData.lastRenderTex = combineTextureData.resultData.currentRenderTex; } } combineTextureData.resultData.skin = newSkin; combineTextureData.resultData.material = newMaterial; combineTextureData.resultData.currentRenderTex = renderTexture; combineTextureData.isFinished = true; })); }