简体   繁体   中英

Position sprite at the edge of a camera (Unity)

I am new to Unity and for the past day Im having troubles positioning a simple sprite game object to the edge of a camera that will look good on iPhone and iPad (so basically two aspect ratios).

For example this is how it looks on iPad (and it should look the same on the iPhone)

在此处输入图片说明

And this is how it looks on iPhone devices (or basically any other phones).

在此处输入图片说明

So what do I need to do, that the ball on the second image will look the same like on the first image?

Thank you in advance!

Based on a camera Orthographic , Size 5

Size screen base 1536 x 2048 (Ipad Mini Retina in your image).

Creat new script ResizeCamera and attach this scrip at MainCamera :

using UnityEngine;

    public class ResizeCamera : MonoBehaviour {

        // Use this for initialization
        void Start () {
            float TARGET_WIDTH = 1536.0f;
            float TARGET_HEIGHT = 2048.0f;
            float PIXELS_TO_UNITS = 102.4f; // 1:1 ratio of pixels to units

            float desiredRatio = TARGET_WIDTH / TARGET_HEIGHT;
            float currentRatio = (float)Screen.width/(float)Screen.height;

            if(currentRatio >= desiredRatio)
            {
                // Our resolution has plenty of width, so we just need to use the height to determine the camera size
                Camera.main.orthographicSize = TARGET_HEIGHT / 4 / PIXELS_TO_UNITS;
            }
            else
            {
                // Our camera needs to zoom out further than just fitting in the height of the image.
                // Determine how much bigger it needs to be, then apply that to our original algorithm.
                float differenceInSize = desiredRatio / currentRatio;
                Camera.main.orthographicSize = TARGET_HEIGHT / 4 / PIXELS_TO_UNITS * differenceInSize;
            }
        }
    }

That is the result HTC screen (1080 x 1920 as your image)

在此处输入图片说明

That is the result Ipad Mini Retina screen (15636 x 2048 as your image)

在此处输入图片说明

...Vary the PIXEL_TO_UNITS variable value to get the desired screen size based on your sprites PixelToUnits value. Make sure you stop the scene and then change the resolution for the code to update the Camera size as our code is in the Start method...

Check more here!

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