【小工具】 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;
        }
    }
相关推荐
游乐码2 小时前
Unity基础(十二)资源异步加载
unity·游戏引擎
weixin_424294673 小时前
程序不知道写在了什么位置???
unity
weixin_441940014 小时前
vuforia ar unity实验教程
unity·游戏引擎·ar
妙为6 小时前
unreal engine5(UE5)中使用Rider
ue5·游戏引擎·虚幻·rider
WarPigs8 小时前
Unity AB包资源加载管理器
unity
程序员正茂8 小时前
EasyAR使用OpenCV下USB摄像头作为自定义相机
opencv·unity·easyar
诙_9 小时前
unity——C#
unity·c#·游戏引擎
晓131310 小时前
【Cocos Creator 3.x】篇——第一章 简介
前端·javascript·游戏引擎
winlife_10 小时前
全程用 AI 做一款商业级手游 · EP9 收尾与复盘:做到了哪,没做到哪,边界在哪
java·开发语言·人工智能·unity·ai编程·游戏开发·mcp
晓131310 小时前
【Cocos Creator 2.x】篇——第五章 游戏常用关键技术
前端·javascript·vue.js·游戏引擎