2D Toolkit Forum
2D Toolkit => Support => Topic started by: bigtunacan on April 06, 2012, 04:37:26 pm
-
I have scrolling backgrounds that need to loop. In order to do this I need to be able to detect my sprite width so I can add in a new prefab instances to the end of each other. How can I access this?
-
sprite.GetBounds / GetUntrimmedBounds returns the bounds of the sprite. You will need to use the center & bounds to get the number you're after, as the sprite anchor could be anywhere.
-
I have tried this; but it seems like my extents are slightly off; like the image is being padded out or something.
Here is the code I use to move one prefab to the end of the other. This results in about a 2 or 3 pixel wide gap between the 2.
if(scr4.transform.position.x < (-2 * scr4.GetBounds().extents.x)){
scr4.transform.position = new Vector3((scr1.GetBounds().center.x + scr1.GetBounds().extents.x), currpos4.y, currpos4.z);
}
-
src1.GetBounds().center returns the local space center of the object.
Lets say the 2 objects have the pivot in the center -
j = src.transform.position => center of sprite #1
k = src.transform.position + Vector3(src.GetBounds().extents.x, 0, 0) => right edge of sprite #1
l = k + Vector3(second.GetBounds().extents.x, 0, 0) => positions second sprite at the right of sprite #1
To offset this correctly for any arbitrary anchor position for sprite #1 and #2, use this:
b.transform.position = a.transform.position + new Vector3(a.GetBounds().extents.x + a.GetBounds().center.x + b.GetBounds().extents.x - b.GetBounds().center.x, 0, 0);
This positions sprite b tiled to the right of sprite a.