【小工具】 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;
        }
    }
相关推荐
DowneyJoy36 分钟前
【多媒体交互】透明无边框窗口
unity·c#·.net·交互
CreasyChan1 小时前
Unity GC实战优化总结
unity·c#
Howrun7775 小时前
虚幻引擎_AController_APlayerController_AAIController
开发语言·c++·游戏引擎·虚幻
习惯就好zz5 小时前
Godot Player CharacterBody2D 移动和停止配置
游戏引擎·godot·characterbody2d·animationplayer·animationtree
ysn111117 小时前
Unity合批实战
unity·游戏引擎
Howrun7777 小时前
虚幻引擎_玩家控制器APlayerController(进阶)
游戏引擎·虚幻
IMPYLH8 小时前
Lua 的 Package 模块
java·开发语言·笔记·后端·junit·游戏引擎·lua
警醒与鞭策8 小时前
大模型对比
unity·性能优化·c#·游戏引擎·cursor
Howrun7778 小时前
虚幻引擎_玩家控制器APlayerController(初阶)
游戏引擎·虚幻
WarPigs9 小时前
switch和硬编码字典的等效性
unity·c#