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

}

完成

相关推荐
新的瑞拉公主8 小时前
Unity游戏发布微信小游戏:从构建到提审
unity·webgl·微信小游戏·开放数据域
Jae den19 小时前
网络延迟为什么会影响游戏和实时业务?
网络·游戏
思盛iOS签名上架1 天前
ipa企业签名闪退是什么原因?
游戏·ios·testfight
狂人开飞机1 天前
14、游戏手感与视觉打磨
游戏·游戏引擎·godot
心前阳光2 天前
Unity发布PC软件图标不能改变
unity·游戏引擎
笨鸟先飞的橘猫2 天前
系统设计第十七天决策卡
学习·游戏
一孤程2 天前
游戏测试专题第二篇:游戏功能测试与用例设计实战
功能测试·游戏·测试·测试覆盖率
科技泡泡堂2 天前
想玩《黎明行者之血》?2026年这4款适合玩3A的游戏本值得一看
游戏
an86950012 天前
unity如何在visual studio中打断点debug
unity·游戏引擎·visual studio
梦想平凡2 天前
百游棋牌源代码开发搭建教程(二):PostgreSQL安装、业务表创建与版本化迁移
数据库·游戏·postgresql