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

}

完成

相关推荐
bmcyzs16 小时前
【展厅多媒体】展厅小知识:VR体感游戏推动展厅数字化转型
经验分享·科技·游戏·人机交互·软件构建·vr·设计规范
yi碗汤园1 天前
【超详细】C#自定义工具类-StringHelper
开发语言·前端·unity·c#·游戏引擎
野奔在山外的猫1 天前
【案例】Unity 平台访问文件浏览器(汇总)
unity
代码改变世界100861 天前
像素策略游戏:资源战争
css·游戏·css3
半夏知半秋1 天前
游戏登录方案中常见的设计模式整理
服务器·开发语言·笔记·学习·游戏·设计模式·lua
佩京科技VR2 天前
垃圾分类抠像拍照系统-垃圾分类AR互动游戏-体感漫画拍照一体机
游戏·ar·抠像拍照·体感抠像
WaWaJie_Ngen2 天前
【OpenGL】模板测试(StencilTest)
c++·算法·游戏·游戏引擎·游戏程序·图形渲染
风亦辰7392 天前
从 Hello World 到游戏世界——pygame 快速入门
python·游戏·pygame
2501_918126912 天前
用c语言写一个nes游戏模拟器
游戏
D155540880582 天前
电竞护航小程序+公众号成品搭建游戏派单小程序开发游戏装备交易系统定制
游戏