简体   繁体   中英

How to obtain physical screen size in iOS simulator with Unity?

I want to display a different number of elements in a game dependent on the actual size of the device. Not just scale the output and work with the same logical pixels in width and height. 20 elements in a row look huge on an iPad and 30 elements in a row are too small on an iPhone.

I'm using Screen.dpi together with Screen.width and Screen.height to calculate the physical Screen size. Then I decide on a small, medium, or large scenario.

But the iOS simulator gives me a 0 for Screen.dpi . Unfortunately I don't have all iPads and iPhones available. It worked alright on a physical iPhone 6S+ and iPad Air 2.

Is this a bug in Unity or normal behavior of the simulator?

Is there a better way? Or do I really need to maintain a list of device DPI for every existing model of iPhone and iPad?

Unity 2017.2.0p3; XCode 9.1

If you read the Screen.dpi docs :

This is the actual DPI of the screen attached or physical device running the application. May return 0 if unable to determine the current DPI.

As simulators are not physical devices, it would return 0 and it is normal behaviour as simulators lack many other physical properties.

You can use Device.generation to detect the target device type. Look at possible values of DeviceGeneration and set up your elements accordingly by checking against dpi for each device .

You can create your own extension to get dpi which work for simulator as well.

Hope this helps :)

You can use Cinemachine to obtain that. to do that you should:

  1. install cinemachine from the unity package manager.

  2. create an empty object and place it on the left corner of your game.

  3. crate another empty object and place it on your right corner.

  4. create a "Target Group Camera" Object from the cinemachine category. now you create 2 game object.

  5. add 2 item on the "Target" inside the CinemachineTargetGroup compnent of the TargetGroup1 game object.

  6. drag your left and right empty objects to the refrence insidce the "CinemachineTargetGroup" component.

now your camera size will change evrey time you choose a differnt screen size.

it's should look like: 在此处输入图像描述

在此处输入图像描述

Good luck

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