【小工具】 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;
        }
    }
相关推荐
派葛穆15 小时前
Unity-UI 按钮点击弹窗功能
unity·游戏引擎
toponad19 小时前
Unity Ads Bidding 现已正式加入TopOn 聚合平台
unity·topon
unityのkiven1 天前
Unity UGUI 实战:动态生成任务面板
unity·游戏引擎
郝学胜-神的一滴1 天前
中级OpenGL教程 026:Assimp库从编译到实战全攻略
c++·unity·游戏引擎·图形渲染·unreal engine·opengl
凯尔3152 天前
Unity简单涂料实现
unity·游戏引擎·图形渲染
刘步权2 天前
KY-RTI分布仿真技术:第十三章 基于Unity的多语言三维态势仿真——以 Island Assault(夺岛突击)想定为例
unity·游戏引擎·仿真·态势·ky-rti·hla·rti
ellis19702 天前
C#/Unity清理非托管资源
unity·c#
玖玥拾3 天前
Unity 3D 笔记(十四)Unity/C# Socket 网络笔记3
服务器·网络·unity·c#
Python私教3 天前
Godot图片怎么导入?PNG、压缩、Mipmap与Reimport一次讲清
游戏引擎·godot
玖玥拾3 天前
Unity 3D 笔记(十七)Unity/C# Socket 网络笔记6
服务器·网络·unity·c#