unity3d面片全屏

本文介绍了一种在Unity中实现面片全屏显示的方法,通过调整面片的宽高比来保持其原始比例,同时确保它能正确填充整个屏幕。文章详细解释了如何根据摄像机和面片的宽高比进行缩放,并根据不同的宽高比情况调整面片的位置。

在使用中,为了是面片保持宽高比和原始位置,全屏化选择

//  获取摄像机的宽高比
                float camera_aspect = virtureCamera.aspect;
                //  面片的宽高比
                float texture_aspect = (float)m_width / m_height;
                //  根据图像的比率对图片进行缩放操作
                backgroundPlane.localScale = new Vector3(m_width / 10f, 1, m_height / 10f);
                //  获取背景图片的像素宽高
                Vector2 point1 = virtureCamera.WorldToScreenPoint(backgroundPlane.GetComponent<Renderer>().bounds.min);
                Vector3 point2 = virtureCamera.WorldToScreenPoint(backgroundPlane.GetComponent<Renderer>().bounds.max);

                if (camera_aspect > texture_aspect)
                {
                    float plane_X= point2.x - point1.x;
                    float screen_plane = Screen.width / plane_X;
                    if (screen_plane < 0)
                        screen_plane *= -1;
                    backgroundPlane.localScale *= screen_plane;
                }
                else
                {
                    float plane_Y = point2.y - point1.y;
                    float screen_plane = Screen.height / plane_Y;
                    if (screen_plane < 0)
                        screen_plane *= -1;
                    backgroundPlane.localScale *= screen_plane;
                }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值