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;
    }

}

完成

相关推荐
警醒与鞭策1 分钟前
大模型对比
unity·性能优化·c#·游戏引擎·cursor
上海云盾-小余19 分钟前
高防CDN+AI协同:零误封精准拦截CC攻击的7层策略拆解
网络·人工智能·tcp/ip·游戏
南村群童欺我老无力.24 分钟前
Flutter 框架跨平台鸿蒙开发 - 从零开发经典推箱子游戏
flutter·游戏·华为·typescript·harmonyos
云边散步1 小时前
godot2D游戏教程系列一(2)
游戏
WarPigs1 小时前
switch和硬编码字典的等效性
unity·c#
没事写写笔记1 小时前
UniTask
unity
yi.Ist1 小时前
博弈论 Nim游戏
c++·学习·算法·游戏·博弈论
cy_cy0021 小时前
投影互动游戏定制化开发方案
大数据·科技·游戏·人机交互·软件构建
秦奈1 小时前
Unity复习学习笔记(八):动画、模型与寻路
笔记·学习·unity
呆呆敲代码的小Y2 小时前
【Unity工具篇】| Unity项目中如何使用LuBan插件,详细集成步骤
游戏·unity·游戏引擎·u3d·luban·免费游戏·unity工具