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!