2D Toolkit Forum
2D Toolkit => Support => Topic started by: Stars 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.
-
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.
-
Thank you; it works great!
-
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:
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);
}