Unity2023.1.19贴段代码看看屏幕的适配问题

文章详细描述了如何在Unity2023.1.19版本中使用代码检测并适应屏幕分辨率,确保摄像机的正交大小在不同屏幕尺寸下保持合理。作者提到摄像机宽度采用特定算法计算,当宽度小于设计值时,调整orthographicSize。
摘要由CSDN通过智能技术生成

Unity2023.1.19贴段代码看看屏幕的适配问题

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestCamara : MonoBehaviour
{
    const float devHeight = 9.6f;  // 开发设计高度
    const float devWidth = 6.4f;   // 开发设计宽度

    // Start is called before the first frame update
    void Start()
    {
        float screenHeight = Screen.height;    // 屏幕高
        Debug.Log("screenHeight = " + screenHeight);

        //float screenWidth = Screen.width;    // 屏幕宽
        //Debug.Log("screenWidth = " + screenWidth);

        float orthographicSize = this.GetComponent<Camera>().orthographicSize;  //相机正交属性设置摄像机大小

        float aspectRadio = Screen.width * 1.0f / screenHeight;     // 屏幕宽高比 <1
        Debug.Log("aspectRadio = " + aspectRadio);

        float cameraWidth = orthographicSize * 2 * aspectRadio;     // 相机宽   注:orthographicSize * 2刚好是摄像机正交大小的高即是相机高
        Debug.Log("cameraWidth = " + cameraWidth);
        

        if (cameraWidth < devHeight)     // 相机宽度小于开发宽度时,调整摄像机正交属性大小
        {
            Debug.Log("cameraWidth < devHeight ");
            orthographicSize = devWidth / (2 * aspectRadio);
            Debug.Log("new orthographicSize = " + orthographicSize);            // 摄像机正交属性的摄像机大小
            this.GetComponent<Camera>().orthographicSize = orthographicSize;
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

经测试会发现,摄像机的正交大小高度刚好是的orthographicSize两倍,宽度并不使用此算法。

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jennifer33K

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值