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); }
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); }