【小工具】 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;
        }
    }
相关推荐
ellis19702 小时前
Unity中ScriptableObject用法整理
unity
玉梅小洋6 小时前
Unity Muse 完整使用文档:Sprite+Texture专项
unity·ai·游戏引擎
能源革命7 小时前
Three.js、Unity、Cesium对比分析
开发语言·javascript·unity
timathy331 天前
Unity Addressable 实现Build时自定义剔除资源组
unity·游戏引擎
一种时光2 天前
Unity 获取当前播放的动画,判断是否是某个动画
unity·游戏引擎
速冻鱼Kiel2 天前
Lyra的相机系统
笔记·ue5·游戏引擎·虚幻
不绝1912 天前
Unity入门 :场景叠加/预制体资源包/脚本资源/生命周期函数/Inspector页面
unity·游戏引擎
在路上看风景2 天前
20. 资源和脚本的绑定关系
unity
yj爆裂鼓手2 天前
unity对象池
unity·c#
在路上看风景2 天前
3.7 SRP Batcher
unity