Unity 2D Background Implementation

2019-08-27 09:07发布

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.

1条回答
贼婆χ
2楼-- · 2019-08-27 09:36

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.

step 1 - Create a Plane object in your scene.

step 2 - Add this script to the object created.

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);
        }
    }
}
查看更多
登录 后发表回答