Hello Guest

Author Topic: Stretch to fit for one sprite  (Read 5667 times)

Stars

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Stretch to fit for one sprite
« on: September 08, 2013, 01:11:42 am »
Hi,

I am using the Fit Height setting for the camera and it works really well, except that I would like my background sprite to Stretch to Fit.  I want to attach a script to the sprite for that purpose, but I am looking for some help and guidance because I am not sure how to do this.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Stretch to fit for one sprite
« Reply #1 on: September 08, 2013, 03:39:22 pm »
The easiest way to do this is to manually rescale your sprite to fit
tk2dCamera.ScreenExtents.width and height.

You can do this by:
scale.x = tk2dCamera.ScreenExtents.width / sprite.CurrentSprite.GetUntrimmedBounds.size.x;
and likewise for y.

That will rescale your sprite to be the same size as the camera regardless of resolution & override. You can then do what you like with it.

Stars

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Stretch to fit for one sprite
« Reply #2 on: September 08, 2013, 08:54:23 pm »
Thank you; it works great!

habitoti

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
    • Rombos Homepage
Re: Stretch to fit for one sprite
« Reply #3 on: October 18, 2013, 07:20:41 am »
For those few that make their first babysteps and struggle a bit with the above high level direction of accomplishing this: here is the exact code you need to put into the Start function of a script attached to the sprite:

Code: [Select]
void Start () {

// make sprite fullscreen sized
tk2dSprite sprite = GetComponent<tk2dSprite>();

sprite.scale = new Vector3(tk2dCamera.Instance.ScreenExtents.width/sprite.CurrentSprite.GetUntrimmedBounds().size.x,
tk2dCamera.Instance.ScreenExtents.height/sprite.CurrentSprite.GetUntrimmedBounds().size.y,1);
}
Checkout our homepage or visit the Rombos blog.