【小工具】 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;
        }
    }
相关推荐
老朱佩琪!1 天前
Unity备忘录模式
java·unity·备忘录模式
一帘多啦A梦1 天前
解决unity2022.3.x版本项目使用vs无法生成解决方案的问题
unity·vs
weixin_424294671 天前
Unity LocalPosition 和 Position 的区别?还有其他的Position 没?
unity·游戏引擎
UX20171 天前
Git LFS 管理 Unity 大文件
git·unity
nnsix1 天前
Unity WebGL端调用Windows窗口选择文件
unity·游戏引擎·webgl
向宇it1 天前
【unity游戏开发——网络】使用Unity+PurrNet+Heathens+Steam,在 Unity 中通过 Steam与你的朋友建立联系
网络·游戏·unity·c#·游戏引擎·steam
nnsix1 天前
Unity 反编译dll(Windows平台)
unity
XR技术研习社1 天前
四种安装特定版本Package的方法
unity·ar·xr·vr
dzj20211 天前
Unity行为脚本与编辑器脚本的解耦例子之一
unity·解耦·编辑器脚本·行为脚本
技术小甜甜1 天前
【Godot】【入门】编辑器界面速通:场景/节点/Inspector/信号(30 分钟上手不迷路)
编辑器·游戏引擎·godot