public static tk2dSpriteCollectionData HandleAnimations2(tk2dBaseSprite sprite, List <Texture2D> playerSheet) { tk2dSpriteCollectionData orig = sprite.Collection; var copyCollection = GameObject.Instantiate(orig); GameObject.DontDestroyOnLoad(copyCollection); tk2dSpriteDefinition[] copyDefinitions = new tk2dSpriteDefinition[orig.spriteDefinitions.Length]; for (int i = 0; i < copyCollection.spriteDefinitions.Length; i++) { copyDefinitions[i] = Copy(orig.spriteDefinitions[i]); } copyCollection.spriteDefinitions = copyDefinitions; { RuntimeAtlasPage page = new RuntimeAtlasPage(); for (int i = 0; i < playerSheet.Count; i++) { var tex = playerSheet[i]; float nw = (tex.width) / 16f; float nh = (tex.height) / 16f; var def = copyCollection.GetSpriteDefinition(tex.name); if (def != null) { if (def.boundsDataCenter != Vector3.zero) { var ras = page.Pack(tex); def.materialInst.mainTexture = ras.texture; def.uvs = ras.uvs; def.extractRegion = true; def.position0 = new Vector3(0, 0, 0); def.position1 = new Vector3(nw, 0, 0); def.position2 = new Vector3(0, nh, 0); def.position3 = new Vector3(nw, nh, 0); def.boundsDataCenter = new Vector2(nw / 2, nh / 2); def.untrimmedBoundsDataCenter = def.boundsDataCenter; def.boundsDataExtents = new Vector2(nw, nh); def.untrimmedBoundsDataExtents = def.boundsDataExtents; } else { def.ReplaceTexture(tex); } } } page.Apply(); } return(copyCollection); }
private static void BuildLibrary(PlayerController player) { List <string> spriteNames = spriteNamesToReplace; List <Texture2D> sprites = new List <Texture2D>(); foreach (string name in spriteNames) { sprites.Add(ResourceExtractor.GetTextureFromResource("LostItems/sprites/PlayerSprites" + name + ".png")); } tk2dSpriteAnimation library = UnityEngine.Object.Instantiate((PickupObjectDatabase.GetById(163) as BulletArmorItem).knightLibrary); UnityEngine.Object.DontDestroyOnLoad(library); var orig = library.clips[0].frames[0].spriteCollection; var copyCollection = UnityEngine.Object.Instantiate(orig); UnityEngine.Object.DontDestroyOnLoad(copyCollection); RuntimeAtlasPage page = new RuntimeAtlasPage(); for (int i = 0; i < sprites.Count; i++) { var tex = sprites[i]; var def = copyCollection.GetSpriteDefinition(tex.name); if (def != null) { def.ReplaceTexture(tex); } } page.Apply(); foreach (var clip in library.clips) { for (int i = 0; i < clip.frames.Length; i++) { clip.frames[i].spriteCollection = copyCollection; } } //replacementLibrary = library; player.OverrideAnimationLibrary = library; }
public static void HandleAnimations(PlayerController player, CustomCharacterData data) { var orig = player.sprite.Collection; var copyCollection = GameObject.Instantiate(orig); GameObject.DontDestroyOnLoad(copyCollection); tk2dSpriteDefinition[] copyDefinitions = new tk2dSpriteDefinition[orig.spriteDefinitions.Length]; for (int i = 0; i < copyCollection.spriteDefinitions.Length; i++) { copyDefinitions[i] = orig.spriteDefinitions[i].Copy(); } copyCollection.spriteDefinitions = copyDefinitions; if (data.playerSheet != null) { Tools.Print(" Using sprite sheet replacement.", "FFBB00"); var materialsToCopy = orig.materials; copyCollection.materials = new Material[orig.materials.Length]; for (int i = 0; i < copyCollection.materials.Length; i++) { if (materialsToCopy[i] == null) { continue; } var mat = new Material(materialsToCopy[i]); GameObject.DontDestroyOnLoad(mat); mat.mainTexture = data.playerSheet; mat.name = materialsToCopy[i].name; copyCollection.materials[i] = mat; } for (int i = 0; i < copyCollection.spriteDefinitions.Length; i++) { foreach (var mat in copyCollection.materials) { if (mat != null && copyDefinitions[i].material.name.Equals(mat.name)) { copyDefinitions[i].material = mat; copyDefinitions[i].materialInst = new Material(mat); } } } } else if (data.sprites != null) { Tools.Print(" Using individual sprite replacement.", "FFBB00"); bool notSlinger = data.baseCharacter != PlayableCharacters.Gunslinger; RuntimeAtlasPage page = new RuntimeAtlasPage(); for (int i = 0; i < data.sprites.Count; i++) { var tex = data.sprites[i]; float nw = (tex.width) / 16f; float nh = (tex.height) / 16f; var def = copyCollection.GetSpriteDefinition(tex.name); if (def != null) { if (notSlinger && def.boundsDataCenter != Vector3.zero) { var ras = page.Pack(tex); def.materialInst.mainTexture = ras.texture; def.uvs = ras.uvs; def.extractRegion = true; def.position0 = new Vector3(0, 0, 0); def.position1 = new Vector3(nw, 0, 0); def.position2 = new Vector3(0, nh, 0); def.position3 = new Vector3(nw, nh, 0); def.boundsDataCenter = new Vector2(nw / 2, nh / 2); def.untrimmedBoundsDataCenter = def.boundsDataCenter; def.boundsDataExtents = new Vector2(nw, nh); def.untrimmedBoundsDataExtents = def.boundsDataExtents; } else { def.ReplaceTexture(tex); } } } page.Apply(); } else { Tools.Print(" Not replacing sprites.", "FFFF00"); } player.spriteAnimator.Library = GameObject.Instantiate(player.spriteAnimator.Library); GameObject.DontDestroyOnLoad(player.spriteAnimator.Library); foreach (var clip in player.spriteAnimator.Library.clips) { for (int i = 0; i < clip.frames.Length; i++) { clip.frames[i].spriteCollection = copyCollection; } } copyCollection.name = player.OverrideDisplayName; player.primaryHand.sprite.Collection = copyCollection; player.secondaryHand.sprite.Collection = copyCollection; player.sprite.Collection = copyCollection; }
public static tk2dSpriteCollectionData ReplaceDungeonCollection(tk2dSpriteCollectionData sourceCollection, Texture2D spriteSheet = null, List <string> spriteList = null) { if (sourceCollection == null) { return(null); } tk2dSpriteCollectionData collectionData = UnityEngine.Object.Instantiate(sourceCollection); tk2dSpriteDefinition[] spriteDefinietions = new tk2dSpriteDefinition[collectionData.spriteDefinitions.Length]; for (int i = 0; i < collectionData.spriteDefinitions.Length; i++) { spriteDefinietions[i] = collectionData.spriteDefinitions[i].Copy(); } collectionData.spriteDefinitions = spriteDefinietions; if (spriteSheet != null) { Material[] materials = sourceCollection.materials; Material[] newMaterials = new Material[materials.Length]; if (materials != null) { for (int i = 0; i < materials.Length; i++) { newMaterials[i] = materials[i].Copy(spriteSheet); } collectionData.materials = newMaterials; foreach (Material material2 in collectionData.materials) { foreach (tk2dSpriteDefinition spriteDefinition in collectionData.spriteDefinitions) { bool flag3 = material2 != null && spriteDefinition.material.name.Equals(material2.name); if (flag3) { spriteDefinition.material = material2; spriteDefinition.materialInst = new Material(material2); } } } } } else if (spriteList != null) { RuntimeAtlasPage runtimeAtlasPage = new RuntimeAtlasPage(0, 0, TextureFormat.RGBA32, 2); for (int i = 0; i < spriteList.Count; i++) { Texture2D texture2D = ResourceExtractor.GetTextureFromResource(spriteList[i]); if (!texture2D) { Debug.Log("[BuildDungeonCollection] Null Texture found at index: " + i); goto IL_EXIT; } float X = (texture2D.width / 16f); float Y = (texture2D.height / 16f); // tk2dSpriteDefinition spriteData = collectionData.GetSpriteDefinition(i.ToString()); tk2dSpriteDefinition spriteData = collectionData.spriteDefinitions[i]; if (spriteData != null) { if (spriteData.boundsDataCenter != Vector3.zero) { try { // Debug.Log("[BuildDungeonCollection] Pack Existing Atlas Element at index: " + i); RuntimeAtlasSegment runtimeAtlasSegment = runtimeAtlasPage.Pack(texture2D, false); spriteData.materialInst.mainTexture = runtimeAtlasSegment.texture; spriteData.uvs = runtimeAtlasSegment.uvs; spriteData.extractRegion = true; spriteData.position0 = Vector3.zero; spriteData.position1 = new Vector3(X, 0, 0); spriteData.position2 = new Vector3(0, Y, 0); spriteData.position3 = new Vector3(X, Y, 0); spriteData.boundsDataCenter = new Vector2((X / 2), (Y / 2)); spriteData.untrimmedBoundsDataCenter = spriteData.boundsDataCenter; spriteData.boundsDataExtents = new Vector2(X, Y); spriteData.untrimmedBoundsDataExtents = spriteData.boundsDataExtents; } catch (Exception) { Debug.Log("[BuildDungeonCollection] Exception caught at index: " + i); } } else { // Debug.Log("Test 3. Replace Existing Atlas Element at index: " + i); try { ETGMod.ReplaceTexture(spriteData, texture2D, true); } catch (Exception) { Debug.Log("[BuildDungeonCollection] Exception caught at index: " + i); } } } else { Debug.Log("[BuildDungeonCollection] SpriteData is null at index: " + i); } IL_EXIT :; } runtimeAtlasPage.Apply(); } else { Debug.Log("[BuildDungeonCollection] SpriteList is null!"); } return(collectionData); }
private static void BuildLibrary(LichsBookItem targetBook) { List <string> spriteNames = new List <string>(); string[] resources = ResourceExtractor.GetResourceNames(); string spriteDirectory = "LichItems/Resources/InfinilichTransformation"; for (int i = 0; i < resources.Length; i++) { if (resources[i].StartsWith(spriteDirectory.Replace('/', '.'), StringComparison.OrdinalIgnoreCase)) { spriteNames.Add(resources[i]); } } List <Texture2D> sprites = new List <Texture2D>(); foreach (string name in spriteNames) { sprites.Add(ResourceExtractor.GetTextureFromResource(name)); } tk2dSpriteAnimation library = Instantiate((PickupObjectDatabase.GetById(163) as BulletArmorItem).knightLibrary); DontDestroyOnLoad(library); var orig = library.clips[0].frames[0].spriteCollection; var copyCollection = Instantiate(orig); tk2dSpriteDefinition copydef = CopyDefinitionFrom((PickupObjectDatabase.GetById(607) as BankMaskItem).OverrideHandSprite.GetCurrentSpriteDef()); copydef.name = "knight_hand_001"; int handId = SpriteBuilder.AddSpriteToCollection(copydef, copyCollection); DontDestroyOnLoad(copyCollection); RuntimeAtlasPage page = new RuntimeAtlasPage(); for (int i = 0; i < sprites.Count; i++) { var tex = sprites[i]; var def = copyCollection.GetSpriteDefinition(tex.name); if (def != null) { def.ReplaceTexture(tex); def.name = def.name.Replace("knight", "inflich"); MakeOffset(def, new Vector2(-0.0625f, 0f), false); } } page.Apply(); foreach (var clip in library.clips) { for (int i = 0; i < clip.frames.Length; i++) { clip.frames[i].spriteCollection = copyCollection; } } foreach (tk2dSpriteAnimationClip clip in library.clips) { foreach (tk2dSpriteAnimationFrame frame in clip.frames) { if (!string.IsNullOrEmpty(frame.eventAudio) && (frame.eventAudio == "Play_FS" || frame.eventAudio == "Play_CHR_boot_stairs_01")) { frame.eventAudio = ""; } } } GameObject spriteObj = new GameObject("OverrideHandSprite"); spriteObj.SetActive(false); DontDestroyOnLoad(spriteObj); tk2dSprite sprite = spriteObj.AddComponent <tk2dSprite>(); sprite.SetSprite(copyCollection, handId); targetBook.replacementHandSprite = sprite; targetBook.replacementLibrary = library; }