【小工具】 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;
        }
    }
相关推荐
爱搞虚幻的阿恺11 天前
Niagara粒子系统-超炫酷的闪电特效(加餐 纸牌螺旋上升效果)
游戏·游戏引擎
_Li.11 天前
Simulink - 6DOF (Euler Angles)
人工智能·算法·机器学习·游戏引擎·cocos2d
weixin_4242946711 天前
Unity 调用Steamworks API 的 SteamUserStats.RequestCurrentStats()报错
unity·游戏引擎·steamwork
HoFunGames11 天前
Unity小地图,Easy Minimap System MT-GPS插件
unity·游戏引擎
wy32586436412 天前
Unity 新输入系统InputSystem(基本操作)
unity·c#·游戏引擎
WarPigs12 天前
着色器multi_compile笔记
unity·着色器
ECHO飞跃 01212 天前
Unity2019 本地推理 通义千问0.5-1.5B微调导入
人工智能·深度学习·unity·llama
Unity游戏资源学习屋12 天前
【Unity UI资源包】GUI Pro - Casual Game 专为休闲手游打造的专业级UI资源包
ui·unity
冰凌糕12 天前
Unity3D Shader 顶点法线外扩实现描边效果
unity
星和月12 天前
Untiy使用说明
c#·游戏引擎