From ec0873a7483387e2a0943bc6eebeef560c5cab29 Mon Sep 17 00:00:00 2001 From: Abbysssal Date: Sat, 24 Sep 2022 20:45:54 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=EF=B8=8F=20Fix=20sprites=20using?= =?UTF-8?q?=20their=20cached=20textures'=20names?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RogueLibsCore/Sprites/RogueSprite.cs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/RogueLibsCore/Sprites/RogueSprite.cs b/RogueLibsCore/Sprites/RogueSprite.cs index de960b983..2c2c0e1ee 100644 --- a/RogueLibsCore/Sprites/RogueSprite.cs +++ b/RogueLibsCore/Sprites/RogueSprite.cs @@ -74,7 +74,6 @@ public string Name if (defined && value is null) throw new ArgumentNullException(nameof(value)); Undefine(); name = value; - if (Texture != null) Texture.name = value; if (Sprite != null) Sprite.name = value; if (defined) Define(); } @@ -192,15 +191,26 @@ internal static void DefinePrepared(tk2dSpriteCollectionData collection, SpriteS private static readonly Dictionary loadedTextures = new Dictionary(); - internal RogueSprite(string spriteName, SpriteScope spriteScope, byte[] rawData, Rect? spriteRegion, float ppu = 64f) + /// + /// Loads a texture from the specified , and if one wasn't created earlier, sets its name to .
Can be used before any sprite initializations to define the texture's name (the first sprite's name is used by default).
+ ///
+ /// The byte array containing a raw PNG- or JPEG-encoded image. + /// The texture name to set, if the texture hasn't been loaded before. + /// The loaded texture. + public static Texture2D LoadTexture(byte[] rawData, string textureName) { if (!loadedTextures.TryGetValue(rawData, out Texture2D? tex)) { - tex = new Texture2D(13, 6) { name = spriteName, filterMode = FilterMode.Point }; + tex = new Texture2D(13, 6) { name = textureName, filterMode = FilterMode.Point }; tex.LoadImage(rawData); loadedTextures.Add(rawData, tex); } - texture = tex; + return tex; + } + + internal RogueSprite(string spriteName, SpriteScope spriteScope, byte[] rawData, Rect? spriteRegion, float ppu = 64f) + { + texture = LoadTexture(rawData, spriteName); name = spriteName; scope = spriteScope; pixelsPerUnit = ppu; @@ -257,6 +267,7 @@ internal void DefineInternal(tk2dSpriteCollectionData? coll, SpriteScope targetS if (coll != null) { tk2dSpriteDefinition def = CreateDefinition(texture!, region, 64f / PixelsPerUnit / coll.invOrthoSize / coll.halfTargetHeight); + def.name = name; AddDefinition(coll, def); def.__RogueLibsCustom = this; definitions!.Add(new CustomTk2dDefinition(coll, def, targetScope)); @@ -390,7 +401,6 @@ public static tk2dSpriteDefinition CreateDefinition(Texture2D texture, Rect? uvR materialInst = mat, flipped = tk2dSpriteDefinition.FlipMode.None, extractRegion = false, - name = texture.name, colliderType = tk2dSpriteDefinition.ColliderType.Unset, positions = new Vector3[] {