【小工具】 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;
        }
    }
相关推荐
陈言必行3 小时前
Unity 之 设备性能分级与游戏画质设置与设备自动适配指南
游戏·unity·游戏引擎
CreasyChan6 小时前
Unity DOTS技术栈详解
unity·c#·游戏引擎
gshh__6 小时前
SuperMap Hi-Fi 3D SDK for Unreal 如何实现横断面分析
3d·ue5·游戏引擎·supermap
在路上看风景8 小时前
1.1 Unity资源生命周期管理与内存机制
unity
CreasyChan8 小时前
Unity的ECS(Entity Component System)架构详解
unity·架构·游戏引擎
神码编程9 小时前
【Unity】 HTFramework框架(六十九)Log在编辑器日志中自定义点击事件
unity·编辑器·游戏引擎
TO_ZRG1 天前
Unity打包安卓、iOS知识点
android·unity·android studio
冰凌糕1 天前
Unity3D Shader 顶点动画 呼吸和水波
unity
呆呆敲代码的小Y1 天前
【Unity 实用工具篇】| UX Tool 工具 快速上手使用,提高日常开发效率
游戏·unity·游戏引擎·游戏程序·ux
世洋Blog1 天前
Unity开发微信小程序-避开新InputSystem有关坑
unity·微信小程序