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

}

完成

相关推荐
码流怪侠4 小时前
Sunshine 深度拆解:当 NVIDIA GameStream 闭源后,如何用 4 万颗 Star 重建自托管云游戏
游戏·github·nvidia
RoboWizard5 小时前
游戏电脑不装固态硬盘会怎么样?
游戏·电脑
不吃凉粉5 小时前
Unity与AS通信
unity·游戏引擎
驱动小百科6 小时前
英伟达GeForce Game Ready 616.92驱动发布:支持《007 First Light》等游戏
人工智能·游戏·英伟达616.92驱动·geforce game·nvidia显卡驱动
职场的momo8 小时前
用户讨论:算法开发Offer决赛:大疆纯后端Agent与网易游戏测试,对内AI跳槽难?
人工智能·游戏·跳槽
两水先木示18 小时前
【Unity】探索小地图迷雾散开Shader效果(基础版)
unity·游戏引擎
飞奔的猫1 天前
锈了儿—素材工作流简化游戏场景元素的生成(三)
人工智能·游戏·个人开发
WarrenMondeville1 天前
AI复刻街头霸王到 Unity
unity·游戏引擎
该逃避避1 天前
ChatGPT vs Fable 5:AI 对话助手与游戏创作引擎的全面对比
人工智能·游戏·chatgpt
比兔代理1 天前
游戏加速、直播拉流、视频下载场景下的代理IP技术方案
网络·http·游戏