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

}

完成

相关推荐
世洋Blog3 小时前
更好的利用ChatGPT进行项目的开发
人工智能·unity·chatgpt
徐同保10 小时前
claude code创建一个html文件,内容是打字游戏,需要打汉字,有分数统计,界面要炫酷,html文件可以直接运行看效果
游戏
evolution_language12 小时前
Unity场景(Scene)的注意事项和易错点
unity·游戏引擎·scene
U***e6312 小时前
元宇宙在虚拟现实游戏中的交互设计
游戏·vr
远程软件小帮手14 小时前
UU远程上线mac被控!如何远程控制 macOS 设备办公?
游戏·macos·智能手机·电脑
EQ-雪梨蛋花汤15 小时前
【AI工具】使用 Doubao-Seed-Code 优化 Unity 编辑器插件:从功能实现到界面美化的完整实践
人工智能·unity·编辑器
熊猫钓鱼>_>16 小时前
从零开始构建RPG游戏战斗系统:实战心得与技术要点
开发语言·人工智能·经验分享·python·游戏·ai·qoder
Dr.勿忘19 小时前
开源Unity小框架:高效单例与模块化设计
游戏·unity·开源·c#·游戏引擎·游戏程序·gamejam
zstar-_2 天前
我用AI做了一个3D六子棋游戏
人工智能·游戏
悦悦欧呐呐呐呐2 天前
休闲摸鱼小游戏扫雷游戏|下载即玩
游戏