Hello Guest

Author Topic: Scroll repeating, sprite width?  (Read 9114 times)

bigtunacan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
    • View Profile
Scroll repeating, sprite width?
« 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Scroll repeating, sprite width?
« Reply #1 on: April 06, 2012, 05:37:47 pm »
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.

bigtunacan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Scroll repeating, sprite width?
« Reply #2 on: April 07, 2012, 03:52:25 pm »
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.
Code: [Select]
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);
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Scroll repeating, sprite width?
« Reply #3 on: April 07, 2012, 05:56:33 pm »
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.