• 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 MigunovAlex · Feb 09, 2015 at 01:13 AM · prefabsscalingpositioning

Wrong positions of new objects. Problem with scaling

Hi guys, I need an advice: I create some objects from prefab dynamically and add it to GameObject, which already in UI Canvas. Canvas has parameter "Ui Scale Mode" which has been set as "Scale with screen size". The problem is that objects which I have added have coords which are not the same as Vector3 value which I pass to Translate method of these objects. For example if I add my Object to coords(54, -108, 0) it adds it to position(71.7608, -143.5216, 0). If I chenge screen dimentions, the values will be different. How can I manage it? Thanks. I add new objects with script:

 public class GridGamePanel : MonoBehaviour {
 
     public Transform linePrefab;
     private static int LINE_WIDTH = 108;
 
     public void drawGridFromScratch ()
     {
         for (var y=1; y<3; y++) {
             for (var x=0; x<3; x++) {
 
                 Transform lineObject = (Transform) GameObject.Instantiate(linePrefab, new Vector3 (0, 0, 0), Quaternion.identity);
                 lineObject.SetParent(this.transform, false);
                 Vector3 newPosition = new Vector3 (x * LINE_WIDTH+LINE_WIDTH/2, y * -LINE_WIDTH, 0);
                 print("new position for horizontal - "+newPosition);
                 lineObject.Translate(newPosition);
             }
         }
         for (var x=1; x<3; x++) {
             for(var y=0; y<3; y++){
                 Transform line = (Transform)Instantiate (linePrefab, new Vector3 (0, 0, 0), Quaternion.identity);
                 line.SetParent (this.transform, false);
                 line.Translate(new Vector3 (x * LINE_WIDTH, y * -LINE_WIDTH - LINE_WIDTH/2, 0)); 
                 line.Rotate(0,0,90);
             }
         }
     }
 }
 
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
2

Answer by Joyrider · Feb 09, 2015 at 09:54 AM

A little background first. Basically, with the ScaleMode "Scale with screen size", what is happening is the positions given to your canvas are interpreted as being positions in the reference resolution and thus not in your current resolution. These are automatically scaled by the canvasScaler to scale with your screen; hence the difference in position between the position you set and the actual position your object is displayed at.

This is not a solution on its own, but an explanation of what is happening by choosing "Scale with screen size".

However, you can get the scaling of your canvas from the RectTransform of your canvas. So with your desired position and the scale applied to you canvas you can compute the position in your reference resolution.

In practice, from what I gather, you RectTransform scale is 1.3289 so you should set the position to (54/1.3289, -108/1.3289, 0) and your final display position should be about (54, -108, 0).

I didn't test this, but it should work imo.

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 MigunovAlex · Feb 09, 2015 at 09:37 PM 0
Share

Thank You! It is that I was trying to find.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Dynamic scaling when instantiated or different prefabs? 1 Answer

How to control position of GUITexture when its inside a prefab 0 Answers

How to get spawners and objects come out of the spawner to scale with resolution size? 0 Answers

How can I layout prefabs in external program? [Blender, Modo etc] 0 Answers

changing the scale of sprite changes its position center?,changing scale of 2d sprite 1 Answer


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