简体   繁体   English

将精灵放置在相机的边缘(Unity)

[英]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). 我是Unity的新手,在过去的一天中,我无法将简单的Sprite游戏对象放置在相机的边缘,在iPhone和iPad上看起来不错(因此基本上是两个长宽比)。

For example this is how it looks on iPad (and it should look the same on the iPhone) 例如,这就是在iPad上的外观(在iPhone上看起来应该相同)

在此处输入图片说明

And this is how it looks on iPhone devices (or basically any other phones). 这就是它在iPhone设备(或基本上任何其他电话)上的外观。

在此处输入图片说明

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 基于Orthographic摄影机, Size 5

Size screen base 1536 x 2048 (Ipad Mini Retina in your image). 屏幕尺寸为1536 x 2048(图像中的Ipad Mini Retina)。

Creat new script ResizeCamera and attach this scrip at MainCamera : 创建新脚本ResizeCamera并将此脚本添加到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) 这就是HTC屏幕的结果(1080 x 1920作为图像)

在此处输入图片说明

That is the result Ipad Mini Retina screen (15636 x 2048 as your image) 那就是Ipad Mini Retina屏幕的结果(15636 x 2048作为您的图像)

在此处输入图片说明

...Vary the PIXEL_TO_UNITS variable value to get the desired screen size based on your sprites PixelToUnits value. ...根据您的精灵PixelToUnits值改变PIXEL_TO_UNITS变量值以获得所需的屏幕尺寸。 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... 确保停止场景,然后更改代码的分辨率以更新相机尺寸,因为我们的代码位于Start方法中。

Check more here! 在这里查看更多!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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