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

}

完成

相关推荐
liangdabiao2 小时前
[分享好东西]类似vibe coding一样开发游戏 -harness-设计架构-生成美术资源-编码-测试-运行-交付
游戏
一只蝉nahc3 小时前
vue使用iframe内嵌unity模型,并且向模型传递信息,接受信息
前端·vue.js·unity
聊点儿技术4 小时前
【游戏风控】如何用IP数据接口从“IP即判罚”升级为“IP参与评分”
tcp/ip·游戏·游戏安全·ip数据库·ip地理定位api·ip数据接口·ip风险画像
小拉达不是臭老鼠8 小时前
Unity05_3D数学
学习·unity·游戏引擎
TMT星球9 小时前
阿里云连续5年稳居游戏云市场份额第一
游戏·阿里云·云计算
沙振宇12 小时前
【Web】使用Vue3+PlayCanvas开发3D游戏(十)让人物动起来
前端·游戏·3d·人物·
黑客说13 小时前
深耕AI,终破局:无限流游戏的核心创新之路
人工智能·游戏
风酥糖13 小时前
Godot游戏练习01-第28节-显示效果与音效
游戏·游戏引擎·godot
2301_7807896615 小时前
什么是端口?端口攻击如何检测和防御
服务器·人工智能·游戏·架构·零信任
老纪的技术唠嗑局15 小时前
4.15 bubseek —— 让 Agent 的足迹,变成团队的洞察
大数据·数据库·sql·游戏·ai·oceanbase·sql优化