Please help me understand this code

I am new to Unity and coding but since I am learning and highly interested, I am trying to understand things as much as possible. I am following a tutorial I have purchased which used this code but I am not able to understand what happened. I request the awesome community here to please comment the lines so that I can understand what happened in this code.

	SpriteRenderer sr = GetComponent <SpriteRenderer> ();
	Vector3 tempScale = transform.localScale;

	float width = sr.sprite.bounds.size.x;
	float worldHeight = Camera.main.orthographicSize * 2f;
	float worldWidth = worldHeight / Screen.height * Screen.width;

	tempScale.x = worldWidth / width;
	transform.localScale = tempScale;

PS: this code is used to resize the background image to fit the screen size

Good day.

I will explain what every line does, but here there are some elementents that you may not know, so you will need to find them at Unity Manual Pages (look for it in google as Unity + Word).

This script is attached, to an object, so when i say “the object”, i mean the object where this script is attached to.

 SpriteRenderer sr = GetComponent <SpriteRenderer> ();

Get the component Sprite Renderer from the object, and store it in a variable called “sr”

 Vector3 tempScale = transform.localScale;

Create a vector3 variable with value of the current localScale of the object

 float width = sr.sprite.bounds.size.x;

Create a float variable called “width” with the value of the Xaxis lenght of the sprite image of the sr (the sprite renderer)

 float worldHeight = Camera.main.orthographicSize * 2f;

Createa float variable called “worldHeight” with the value of 2* the camera orthografic size (property for an orthografic camera to set the size of the view)

 float worldWidth = worldHeight / Screen.height * Screen.width;

Create a float variable with a value = worldHeight (created before) / (Screen height * Screen width) So you have a proportional value that “knows” the type of your screen (4:3, 16:9 , …)

 tempScale.x = worldWidth / width;

The component X of the vector tempScale, is modified to worldwith / width

 transform.localScale = tempScale;

Change the localScale of the object to this new tempScale, that has a “modified” X compoennt.

This can be usefull to make panels, or UI that you want to be “proportional” for all screen sizes and porportions.

Bye!