How to show the same part of game on every aspect ratio on Android devices with Unity?

半腔热情 提交于 2019-12-06 03:04:33

问题


I'm coming from Libgdx engine to Unity and I'm a beginner programmer. I try to make a game in Unity for Android and iOS devices, but I have a problem with the aspect ratio/resolution scaling. In Libgdx if you have develop something you give a "default" resolution, and everything will be scaled to the device's screen, not matter if it has bigger or smaller resolution, and nothing will be cut off from the screen. On the other side, in Unity if I make a game it will crop the screen to the chosen aspect ratio, which is not the best if you want the same experience across all the devices available. I have searched on the internet for a long time, but I haven't find anything that would work for me.

Bottom line: I would like to know if there's some code for Unity (C#) which will make my game looks like the same across all devices with different aspect ratios? I'm developing with Unity 2D. If anybody could code it and would like to share it, I would be very happy, because it is very bad if it doesn't look like same on all devices. I don't have the problem with onGUI yet, as I haven't did any menu or something for it, just the game, but it is not good that the background is not the same on devices, and that the character is not on the same portion of the screen.


回答1:


During init, use Camera.pixelRect to determine the Height and Width of the Device assuming the camera is fullscreen.

Store the Height and Width and then do some math with your default settings. Eg. Default settings is 480x854, pixelRect is 800x1280. So difference in width is 800f/480f. Do the same for height.

Use the difference and multiply it to the object at the top of the hierarchy.

Hope it helps.



来源:https://stackoverflow.com/questions/22269453/how-to-show-the-same-part-of-game-on-every-aspect-ratio-on-android-devices-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!