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

}

完成

相关推荐
leeshuqing5 小时前
剪刀石头布-Python练习
python·游戏·优化·列表·字典·控制语句
Lethehong8 小时前
从轻点到落块:Rokid AIUI 游戏《叠叠乐》开发实践
java·开发语言·游戏
张不无-Unity8 小时前
Unity的四种灯光Directional、Point、Spot 和 Area
unity·游戏引擎
开开心心就好1 天前
游戏安装报错缺环境?运行库整合包补上
java·mysql·游戏·pdf·ocr·音视频·lua
鹿野素材屋1 天前
游戏开发(三):AI时代,如何快速还原一款竞品游戏
游戏
淡海水1 天前
02-02-原理篇-Scriban模板引擎原理深度解析
unity·excel·游戏开发·luban
一只大鸽子2 天前
游戏开发笔记19-Godot RPG游戏基础-文本样式
笔记·游戏·godot
禁默2 天前
AI搓童年游戏-用华为云码道 CodeArts(Kimi-K3.1-Pro)从零写一个零依赖俄罗斯方块
人工智能·游戏·华为云
2601_949695592 天前
显卡驱动版本过低玩不了游戏怎么办?如何检查最新驱动?
游戏·电脑·显卡·显卡驱动·驱动程序
ttod_qzstudio3 天前
系统架构设计师论文逐句拆解(三):ATAM 架构评估,怎样把“方案比较”写成质量属性权衡
unity·架构·系统架构·软考