Unity:2D游戏设置相机orthographicSize

目录

[根据设备分辨率动态设置相机 orthographicSize](#根据设备分辨率动态设置相机 orthographicSize)


根据设备分辨率动态设置相机 orthographicSize

  • 2d游戏里面相机的Orthan.size确定的是高度,宽度是按照屏幕的宽高比计算出来的
  • cameraWidthSize = camera.Orthographic.size*(Screen.Width/Screen.height)
  • 我在游戏里设置的 开发分辨率是1080*1920
  • 所以我在原先Y=1920情况下 Camera设置的orthographicSize=10,可以恰好把图片横向全部展示
  • 如下

但是导出apk安装到手机上的时候 我的手机分辨率是1080*2440

  • 这个时候用unity用10来计算
  • 10*(Screen.Width/Screen.height)
  • 就会把宽度变小了
  • 但是到了手机里就变成了这样

所以需要按照开发的尺寸(原先的宽)度来重新计算size ,挂在摄像机上

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraAdaper : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        SetCamera(GetComponent<Camera>());
    }

    float width = 1080;//开发分辨率x
    float height = 1920;//开发分辨率y

    //需要适配宽度
    public void SetCamera(Camera camera)
    {
        var realWidthSize = (camera.orthographicSize * (width / height));
        Debug.Log($"width_{Screen.width}-height_{Screen.height}");
        float val  = realWidthSize / ((float)Screen.width / (float)Screen.height);
        Debug.Log($"cameraSzie{val}");
        camera.orthographicSize = val;
    }

}

完成

相关推荐
Maynor99612 分钟前
「原子弹爆炸」级别:Astra 复刻游戏合集(含实机截图)
java·linux·运维·数据库·gpt·游戏
wind100322 天前
DLSS5 开启保姆级教程:RTX20/30/40 显卡通用 鸣潮 / 异环 / 绝区零全适配
游戏·电脑·英伟达·dlss·鸣潮
谢斯2 天前
[unity]如何在unity中添加newtonsoft-json
unity·json·游戏引擎
全速向光2 天前
JoiPlay 模拟器:在手机上畅玩你所热爱的独立游戏
游戏·智能手机
天人合一peng2 天前
Unity标记固定颜色及投影标记
unity
云雨巫山2 天前
Unity 游戏开发性能优化全景手册
unity·智能手机·性能优化·游戏引擎
maybeyaluokenai2 天前
unity build in渲染管线概述
unity·图形渲染
云雨巫山2 天前
Unity 性能优化 · 图解全景手册
unity·性能优化·游戏引擎
yangmu32033 天前
《黎明行者之血》MOD整合包保姆级安装与使用指南
游戏·游戏程序
Madokaly3 天前
DOTween的Vector3Plugin
unity