【小工具】 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;
        }
    }
相关推荐
鼎艺创新科技17 小时前
三维电子沙盘中OSGB倾斜摄影数据的加载与渲染
游戏引擎·cocos2d
玉夏18 小时前
【Shader基础】UV 与纹理采样 Part1
unity·着色器·uv
kyle~18 小时前
Godot开源游戏引擎
开源·游戏引擎·godot
zdr尽职尽责19 小时前
Unity录像功能
学习·ui·unity·游戏引擎
真鬼12319 小时前
【Unity Cursor】AI规矩
unity·游戏引擎
mxwin19 小时前
Unity Shader 深入理解 LinearEyeDepth 与 DepthTexture
unity·游戏引擎
小贺儿开发19 小时前
Unity3D VR 全景图游览
unity·渲染·vr·虚拟现实·全景图·漫游·互动
开维游戏引擎1 天前
AI自动生成游戏时,deepseek和mimo对比
android·游戏·语言模型·游戏引擎·ai编程
avi91111 天前
Unity 商业插件之(四)粒子系统,古法射击子弹轨迹 ,附加:HDRP Built-in Particle Shaders 最新的高级管线粒子Shader
unity·游戏引擎·粒子系统·particle·拖尾效果
晓13132 天前
【Cocos Creator 3.x】篇——第四章 子系统
前端·javascript·游戏引擎