【小工具】 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;
        }
    }
相关推荐
zyh______6 小时前
关于unity的序列化
unity·游戏引擎
weixin_409383126 小时前
godot碰撞测试的学习
学习·游戏引擎·godot
电子云与长程纠缠6 小时前
Godot学习06 - AnimationPlayer内置动画
学习·游戏引擎·godot
星夜泊客8 小时前
C# : 引用类型都存在堆上吗
unity·c#
点量云实时渲染-小芹8 小时前
Unity模型数字孪生虚拟仿真webgl推流卡实时云渲染推流
unity·webgl·数字孪生·实时云渲染·虚拟仿真·云推流
mxwin14 小时前
Unity Shader 齐次坐标与透视除法理解 SV_POSITION 的 w 分量
unity·游戏引擎·shader
NPUQS17 小时前
【Unity 3D学习】Unity 与 Python 互通入门:点击按钮调用 Python(超简单示例)
学习·3d·unity
电子云与长程纠缠1 天前
Godot学习05 - 播放与分离FBX动画
学习·游戏引擎·godot
小贺儿开发1 天前
【Arduino与Unity交互探究】03 超声波测距模块
unity·arduino·串口通信·传感器·videoplayer·硬件交互
weixin_409383121 天前
godot等轴视角tilemaplayer的学习 isocheric的素材xy大小怎么调
学习·游戏引擎·godot