团结2D背景的实现 [英] Unity 2D Background Implementation

查看:209
本文介绍了团结2D背景的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个固定的背景。我添加图像作为摄像头的孩子,它的大小来填充视摄像头。这似乎团结球员好但是当我在我的Andr​​oid设备上运行它,似乎背景短。我看到在屏幕的左侧和右侧部分的空白空间。
正投影尺寸为6。

I'm trying to use a fixed background. I added the image as a child of camera and sized it to fill view of camera. It seems good in unity player however when I run it on my android devices, background seems short. I see some blank space in left and right sides of the screen. Orthographic size is 6.

推荐答案

这将使飞机或四和扩大它,所以它会填满屏幕。它无论是在正交和透视的作品。 Orthegrphic大小并不重要。

This will position a plane or quad and scale it so it will fill the screen. It works both in orthographic and perspective. Orthegrphic size doesnt matter.

第1步 - 在场景中创建一个平面物体

step 1 - Create a Plane object in your scene.

第2步 - 添加这个脚本来创建对象

step 2 - Add this script to the object created.

第3步 - 脚本将自动调整和位置在近剪裁

step 3 - Script will auto resize and position at the near clip

using UnityEngine;

public class FillScreenPlane:MonoBehaviour
{
    public float lUnityPlaneSize = 10.0f; // 10 for a Unity3d plane
    void Update()
    {
        Camera lCamera = Camera.main;

        if(lCamera.isOrthoGraphic)
        {
            float lSizeY = lCamera.orthographicSize * 2.0f;
            float lSizeX = lSizeY *lCamera.aspect;
            transform.localScale = new Vector3(lSizeX/lUnityPlaneSize, 1,lSizeY/lUnityPlaneSize);
        }
        else
        {
            float lPosition = (lCamera.nearClipPlane + 0.01f);
            transform.position = lCamera.transform.position + lCamera.transform.forward * lPosition;
            transform.LookAt (lCamera.transform);
            transform.Rotate (90.0f, 0.0f, 0.0f);

            float h = (Mathf.Tan(lCamera.fov*Mathf.Deg2Rad*0.5f)*lPosition*2f) /lUnityPlaneSize;
            transform.localScale = new Vector3(h*lCamera.aspect,1.0f, h);
        }
    }
}

这篇关于团结2D背景的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆