【小工具】 Unity相机宽度适配

相机默认是根据高度适配的,但是在部分游戏中需要根据宽度进行适配

实现步骤

  • 定义标准屏幕宽、高
  • 判断标准屏幕宽高比与当前的是否相等
  • 通过**(标准宽度/当前宽度)= (标准高度 / 当前高度)**计算缩放
  • 调整相机fieldOfView即可

实现代码

csharp 复制代码
public class CameraAdjust : MonoBehaviour
    {
        [SerializeField] private int ManualWidth = 1080;
        [SerializeField] private int ManualHeight = 1920;

        private void Start()
        {
            float heigtht;
            if (Screen.height * 1f / Screen.width > ManualHeight * 1f / ManualWidth)
            {
                heigtht = ManualWidth * 1f / Screen.width * Screen.height;
            }
            else
            {
                heigtht = ManualHeight;
            }

            GetComponent<Camera>().fieldOfView *= heigtht / ManualHeight;
        }
    }
相关推荐
沉默金鱼6 小时前
Unity实用技能-GM命令
unity·游戏引擎
chillxiaohan8 小时前
unity粗糙、高光、光泽度调节shader记录
unity·游戏引擎
星夜泊客9 小时前
Unity UI 渲染与 Rebuild 机制简易解析
unity·游戏引擎
一线灵12 小时前
跨平台游戏引擎 Axmol-2.11.1 发布
游戏引擎
qiminixi21 小时前
Unity 6000下载
unity·unity 6000·unity 6000下载
CreasyChan1 天前
Unity Shader 入门指南
unity·c#·游戏引擎·shader
漂视数字孪生世界1 天前
Unity团结引擎的前世今生
unity·游戏引擎·数字孪生
心前阳光1 天前
Unity通过ScriptableObject学习访问者模式
学习·unity·访问者模式
fcm191 天前
unity之重新导入TMP
unity
心疼你的一切1 天前
【技术创作的璀璨盛宴——2025年CSDN博客之星总评选深度总结】
microsoft·unity·游戏引擎·游戏程序·csdn·博客之星