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 小时前
游戏开发笔记17-Godot RPG游戏基础-菜单
笔记·游戏·godot
小贺儿开发10 小时前
Unity 轻量级外部素材加载器(热更新)
科技·学习·unity·程序员·工具·热更新·ugui
赤生云电竞1 天前
云游戏边缘节点部署:机房选址与网络拓扑优化实践 —— 基于GPU虚拟化与多级QoS的端到端时延控制
游戏
芝麻粒儿2 天前
Scratch少儿编程案例-赛车与载具(1)
游戏·青少年编程·scratch·少儿编程·编程猫
jasmine02442 天前
GitHub 开源背单词游戏:四六级考研词库,拼写正确就能攻击
游戏·github·工具类
淡海水3 天前
01-03-认知篇-基础-Excel配置工作流痛点深度分析
unity·架构·excel·游戏开发·luban
liulilittle3 天前
认识麻将 —— 流水及对账
算法·游戏·lua·对账·流水·资金·玩法
赤生云电竞3 天前
云游戏边缘节点部署选型:机房选址与网络拓扑优化实践
游戏
manduic5 天前
游戏手柄摇杆检测用 TMR 传感器替代电位器
游戏
钱栈up5 天前
微软商店把《我的世界》国区从 89 涨到 110.99:一个「小调价」里的平台定价权博弈
游戏·microsoft·raft