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

}

完成

相关推荐
腾讯WeTest8 小时前
范式转移:LLM如何重塑游戏自动化测试的底层逻辑
功能测试·游戏·ai·腾讯wetest
云水木石12 小时前
Android 的下一个战场:Windows 应用与游戏?
android·windows·游戏
老朱佩琪!15 小时前
Unity备忘录模式
java·unity·备忘录模式
一帘多啦A梦16 小时前
解决unity2022.3.x版本项目使用vs无法生成解决方案的问题
unity·vs
weixin_4242946718 小时前
Unity LocalPosition 和 Position 的区别?还有其他的Position 没?
unity·游戏引擎
UWA18 小时前
GPM 2.0全新功能发布|GPU精准监测 + 精细化运营,重构游戏性能管控新范式
人工智能·游戏·性能优化·重构·游戏开发·uwa
UX201718 小时前
Git LFS 管理 Unity 大文件
git·unity
nnsix19 小时前
Unity WebGL端调用Windows窗口选择文件
unity·游戏引擎·webgl
向宇it19 小时前
【unity游戏开发——网络】使用Unity+PurrNet+Heathens+Steam,在 Unity 中通过 Steam与你的朋友建立联系
网络·游戏·unity·c#·游戏引擎·steam
nnsix19 小时前
Unity 反编译dll(Windows平台)
unity