【小工具】 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 小时前
【Laya】LocalStorage 本地存储
typescript·游戏引擎·游戏程序·laya
妙为9 小时前
unreal engine5角色把敌人 “挤飞”
游戏引擎·虚幻·ue·unrealengine5
4Forsee9 小时前
【增强现实】快速上手 Vuforia Unity Android AR 应用开发
android·unity·ar
两水先木示9 小时前
【Unity】对指定物体进行描边——模板测试法
unity·游戏引擎·shader·外描边
Miss_SQ10 小时前
实现Unity录音、百度云语音转文字
unity·语音识别
CreasyChan10 小时前
unity 对象池实测可用
unity·c#
weixin_4242946710 小时前
Unity项目的Artifacts文件夹过大怎么解决?
unity·游戏引擎
没事写写笔记18 小时前
Unity HDRP14.0.12 Volume 配置参数
unity
红黑色的圣西罗21 小时前
手游手动异形屏适配方案,类“明日方舟”
unity
syker1 天前
3D游戏引擎Bluely Engine 开发手册
开发语言·3d·游戏引擎