操纵杆相对于屏幕尺寸的定位
我希望操纵杆固定在屏幕的右下角,无论分辨率/窗口大小如何。虽然我的解决方案有效,但必须有一个更清晰的代码来解决这个问题或一些我不知道的画布选项。
我尝试并测试了适用于设定分辨率的位置,然后将新的分辨率考虑在内。测试分辨率:434x494 和 1920x1080
对于 434 像素宽屏幕,操纵杆位置 x 为 434 - 115 = 319。 这个系数太大了,所以我不得不将其平方根以获得宽度。
对于 120 高度适用于 494 高度屏幕的情况,我只是做了一些技巧,使其适用于 1920x1080。
public Joystick joystick;
public Vector3 m_myPosition;
public RectTransform m_NewTransform;
void Start()
{
double width = Screen.width - (Mathf.Sqrt(Screen.width / 434)) * 115;
double height = 120 + (4* Mathf.Exp(Screen.width / 494));
m_myPosition.x = (float)width;
m_myPosition.y = (float)height;
m_NewTransform.position = m_myPosition;
}
我不应该依赖这样的不确定代码,有什么解决方案吗?
I want the joystick fixed on the bottom-right the screen, no matter the resolution/window size. Although my solution works, there must be a cleaner code for this or some canvas option I don't know about.
I tried and tested a position that works on a set resolution and then factor the new resolution to that. Tested on resolutions: 434x494 and 1920x1080
For 434 pixel wide screen a joystick position.x of 434 - 115 = 319.
The factor was too big so I had to square root it for width.
For the height 120 worked for 494 height screen, I just did some hocus pocus to make it work for 1920x1080.
public Joystick joystick;
public Vector3 m_myPosition;
public RectTransform m_NewTransform;
void Start()
{
double width = Screen.width - (Mathf.Sqrt(Screen.width / 434)) * 115;
double height = 120 + (4* Mathf.Exp(Screen.width / 494));
m_myPosition.x = (float)width;
m_myPosition.y = (float)height;
m_NewTransform.position = m_myPosition;
}
I shouldn't rely on iffy code like this, any solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
YouTube 上到处都是解决方案
...
您只需将画布 UI 比例更改为“缩放到屏幕大小”,然后将 UI 操纵杆对象锚定到某个方向。
对于我的操纵杆,我只需将其固定在右下角即可完成!
如果您想了解如何进行的深入信息;我遵循了本教程:https://www.youtube.com/watch?v=w3sMD -3OJro
G'day folks
Solution is everywhere on youtube...
You just change the canvas UI scale to 'Scale to screen size' and then Anchor the UI joystick object to a direction.
For my joystick I just anchored it to bottom-right and done!
In case you want in depth info on how to; I followed this tutorial: https://www.youtube.com/watch?v=w3sMD-3OJro