Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by therisingsun39 · Sep 07, 2018 at 08:10 PM · screen sizebackgrounds

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JVene · Sep 07, 2018 at 08:50 PM 1
Share

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.

avatar image toddisarockstar · Sep 07, 2018 at 08:55 PM 0
Share

it looks like this code is just trying to more accurately set your sprite's size to an even percentage of your screen size

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

90 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges