Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Tarious · Apr 28, 2011 at 11:23 AM · guibuildbuild-error

Build is changing GUI positions

I'm working on a tower defense game, where you can see a tower's radius as a guiTexture around the tower, while it's about to be placed on the map and while OnMouseOver the tower. This works perfectly in playmode with the offsets I've plotted in to place the radius correctly. However, once I build the project, the radius' position is completly off as if I didn't offset it both when the tower is about to be placed, and when OnMouseOver the tower.

Here is the code for the radius positioned on the tower:

//Set position for radius and instantiate it. The variable "radius" holds the radius guiTexture.</p> <pre><code>position = new Vector3(transform.position.x + xOffSet, transform.position.y, transform.position.z + zOffSet); worldPos = Camera.main.WorldToViewportPoint(position); radiusClone = (GUITexture)Instantiate(radius, worldPos, transform.rotation);

Here is the code for the radius positioned on the mouse before placing the tower on the map:

// place radius gui texture on mouse position by translating mouse position coordinates to normalized screen coordinates. The "TrooperRadius" guiTexture is found in the scene and moved to the mouse position.

trooperRadius = GameObject.Find("TrooperRadius");

trooperRadius.transform.position = new Vector3((Input.mousePosition.x + xOffSet) / Screen.width, (Input.mousePosition.y + zOffSet) / Screen.height, 0);

I also have an upgraded version of the radius for when the player upgrades the tower, which holds the same code as seen above just without the offsets, since the upgraded radius is a scaled version of the normal radius, and by coincidence the upgraded radius is positioned perfectly without offsets. However, there are no problems with the upgraded radius in the build, as it is positioned perfectly. There are only problems with the normal radius with offsets.

Your help is highly appreciated

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Wolfram · Apr 28, 2011 at 12:09 PM

GUITextures behave somewhat unintuitive, concerning their transform settings.

Generally, there are two possibilities:

1) Have the GUITexture always keep the same pixel size, independent of screen resolution. For this, you need to set transform.localScale to Vector3.zero. The actual size of the GUITexture is then given via pixelInset: pixelInset.width and pixelInset.height determine the size in pixels. Note these values scale relative to the lower left corner of the GUITexture, so to center that offset, you usually want to set pixelInset.x=-pixelInset.width/2 and pixelInset.y=-pixelInset.height/2 (or written differently, pixelInset=Rect(-mytexture.width/2,-mytexture.height/2,mytexture.width,mytexture.height)

2) Have the GUITexture always keep the same relative screen space, i.e. it scales with screen resolution. For this, you need to set x and y of transform.localScale to your desired size. However, you need to reset pixelInset to Rect(0,0,0,0) in this case, otherwise your scaling and positinoing will be skewed. Note, x and y are given in normalized viewport space, so x=1 and y=1 will have the GUITexture always exactly fill the whole screen, independend of screen size and aspect ratio. So to display the GUITexture undistorted, you need to compensate both for the texture aspect ratio, and the (inverse) screen aspect ratio. For example, for a 250x100 (=aspect ratio 2.5) texture that you want to cover 50% of a screen height of 1024x768 (=aspect ratio 1.33333), you would need to set the localScale to x=0.5/1.333333*2.5, and y=0.5

If that doesn't help solving your problem, we would need some additional info, for example: Is the positioning in the editor always correct, even when you change the size/resolution of your Game window, or change its aspect ratio (via the pulldown menu in the upper left corner of the Game window? Is the positioning in the standalone build always wrong "the same way", even if you choose different screen resolutions or aspect ratios in the Screen Resolution Dialog?

Comment
Add comment · Show 1 · 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
avatar image Joshua · Apr 28, 2011 at 12:20 PM 0
Share

Well explained.

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

No one has followed this question yet.

Related Questions

Distribute terrain in zones 3 Answers

GUITexture disappears in build 0 Answers

GUI not working in Build? 1 Answer

Blue Screen On Windows Build 0 Answers

It says jdk is not installed 3 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