简体   繁体   中英

Vector 3 Position on different Screen Size

在此处输入图片说明

so this is what happens when Im using vector 3 on my game I want my vector 3 to be on specific position for different screen sizes? is that possible? here is my codes

 public virtual void ShuffleButton()
    {
       Vector3 buttonFirst = gameButtons[0].transform.position;
       buttonFirst.x += 297f;
       gameButtons[0].transform.position = buttonFirst;

       Vector3 buttonSecond = gameButtons[1].transform.position;
       buttonSecond.x -= 74.25f;
       gameButtons[1].transform.position = buttonSecond;

       Vector3 buttonThird = gameButtons[2].transform.position;
       buttonThird.x += 74.25f;
       gameButtons[2].transform.position = buttonThird;

       Vector3 buttonFourth = gameButtons[3].transform.position;
       buttonFourth.x -= 148.5f;
       gameButtons[3].transform.position = buttonFourth;

       Vector3 buttonFifth = gameButtons[4].transform.position;
       buttonFifth.x -= 148.5f;
       gameButtons[4].transform.position = buttonFifth;
    }

You are looking for the position conversion functions like Camera.ScreenToWorldPoint() . These can be found in the Unity Camera class documentation here: https://docs.unity3d.com/ScriptReference/Camera.html

If, for example, you want to place a Sprite in the top-left corner, regardless of screen size, you would use the screen or viewport space. The position of the sprite will have to be translated from this screen/viewport space to world space. You could use Camera.ScreenToWorldPoint() for this.

However, Unity uses three viewspaces: Screen, World and Viewport. You should read up on all three as your problem stems from the fact that you are trying to use world coordinates (transform.position) to set the position of UI elements (which use either the screen or world space; this is dependent on the parent Canvas settings)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM