- Home /
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
It is impractical to answer with a full explanation of every line in one post without knowing, first, what in this code does not make sense, so let's do that in a conversation.
Are you familiar with what SpriteRenderer is, and what sr it will be as a result?
Is GetComponent a familiar call to you?
is transform.localScale unclear?
I'll pause here because if you got that, I don't need to explain. If not, we can't go further until I do.
it looks like this code is just trying to more accurately set your sprite's size to an even percentage of your screen size
Answer by tormentoarmagedoom · Sep 07, 2018 at 10:22 PM
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220612174229im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
tessalating background? 2 Answers
Scale camera to fit screen size 1 Answer
multiple apks to match certain devices 0 Answers
How to set screen resolution 1 Answer
Aspect ratio and resolution problem,,About resolution and aspect ratio 0 Answers