- Home /
Sprite.Create() only works the first time and eventually causes "Invalid AABB" errors.
So, I'm trying to have some images on a UI change based on data about levels in my game, and this is my code for one of those things:
contentPanel.transform.GetChild(0).gameObject.GetComponent() .sprite = Sprite.Create(lvl.ImageID, contentPanel.transform .GetChild(0).gameObject.GetComponent<Image>().sprite.textureRect, contentPanel.transform.GetChild(0).gameObject.GetComponent<Image>() .sprite.pivot);
"contentPanel.transform.GetChild(0).gameObject" refers to an image that is the first child of a panel on the canvas. lvl.ImageID is the thumbnail for a level.
The code runs whenever the player hits a button to open the canvas (it's actually always open, it's just hidden behind everything else when it's "closed"). The first time, it runs well, but the second time, the image I'm trying to load is drawn off-center even though the bounding box and pivot are in the right spot and the third time and afterward, the image doesn't draw at all (even though the bounding box and pivot are still right where they should be) and I get an error message that says, "Invalid AABB in AABB." The call stack never mentions my code. What is going on?
UPDATE: After more testing, I found that reloading the scene "resets" the bug.
ANOTHER UPDATE: I changed the pivot parameter in one of the Sprite.Create() functions to new Vector2(0.5f, 0.5f) and the problem got fixed for the thing that that function was helping to draw. (For anyone reading this in the future, I think that was only part of the fix, read the answers and comments.)
Answer by sheepmccree · May 21, 2020 at 05:40 AM
Invalid AABB in AABB usually means that the texture you want to create a sprite of is not set to readable. I'm the inspector, in the texture import settings, make sure that "Read/Write enabled" is set to true. Also for the textureRect, you don't want to use the previous one, but the size of the texture. So, instead of contentPanel.transform .GetChild(0).gameObject.GetComponent<Image>().sprite.textureRec
use new Rect(0, 0, lvl.ImageID.width, lvl.ImageID.height
, if I understand correctly that lvl.ImageID is your texture What I suggested only works with single textures, not packed textures
Answer by testmaster217 · May 21, 2020 at 12:57 PM
@sheepmccree My texture is part of a multiple sprite image that isn't using an atlas. Does that matter?
Read/Write should be enabled in that image as well. If you have multiple textures in one place, then looking at the Sprite.Create reference, it says "The second argument rect defines the sub-texture used. The rect argument is defined in pixels of the texture. A Rect(50.0f, 10.0f, 200.0f, 140.0f) would create a left to right range from 50.0f to 50.0f + 200.0f = 250.0f. The bottom to top range would be 10.0f to 10.0f + 140.0f = 150.0f."
So, that means you have to somehow get that rect for each of your textures. I don't completely understand your use case, but I'll be happy to help if I misunderstood something.
@sheepmccree I made the texture read/write enabled and changed the code to use a new Rect with the texture’s width and height and it didn’t change anything.