【小工具】 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______3 小时前
unity值属性修改步骤
unity·游戏引擎
小贺儿开发5 小时前
Unity3D 四星探秘:手势互动演示
科技·unity·人机交互·科普·硬件·leap motion·互动
风酥糖6 小时前
Godot游戏练习01-第11节-显示优化,游戏背景,Shader
游戏·游戏引擎·godot
码界奇点8 小时前
基于模块化架构的Unity游戏开发框架设计与实现
java·c++·unity·架构·毕业设计·源代码管理
风酥糖10 小时前
Godot游戏练习01-第13节-粒子系统,武器攻击特效
游戏·游戏引擎·godot
张老师带你学12 小时前
unity船资源,快艇,帆船,游轮
科技·游戏·unity·游戏引擎·模型
C蔡博士13 小时前
Unity游戏物体渲染顺序
unity·游戏引擎·游戏开发
瑞瑞小安15 小时前
Unity功能篇:PCVR开发(设备:HTC Vive Pro)更新中。。。
unity·游戏引擎
风酥糖1 天前
Godot游戏练习01-第10节-组件化,玩家受伤,YSort,和一点思考
游戏·游戏引擎·godot