C# 如何自动缩放到不同的屏幕分辨率?

C# 如何自动缩放到不同的屏幕分辨率?,c#,unity3d,2d,resolution,C#,Unity3d,2d,Resolution,我在unity中制作了一个无限长跑者,我有一个瓦片产卵器/生成器,它根据屏幕高度和宽度生成游戏对象,我设法使其与宽度一起工作,但当改变高度时,相机无法跟随,我无法使其工作 不管怎么说,我的代码不好,我花了6个小时才完成,我不欣赏结果 正如我发现的,你可以定义一个相机的纵横比,它会自动缩放你的比例,但它会扭曲图像,看起来不太好 由于所有这些注释是自动缩放2D平台游戏(不考虑GUI,只考虑游戏对象)的最佳方式,因此我使用此脚本根据精灵的大小拉伸精灵,在大多数情况下都适用。我用5表示相机的正交尺寸 u

我在unity中制作了一个无限长跑者,我有一个瓦片产卵器/生成器,它根据屏幕高度和宽度生成游戏对象,我设法使其与宽度一起工作,但当改变高度时,相机无法跟随,我无法使其工作

不管怎么说,我的代码不好,我花了6个小时才完成,我不欣赏结果

正如我发现的,你可以定义一个相机的纵横比,它会自动缩放你的比例,但它会扭曲图像,看起来不太好


由于所有这些注释是自动缩放2D平台游戏(不考虑GUI,只考虑游戏对象)的最佳方式,因此我使用此脚本根据精灵的大小拉伸精灵,在大多数情况下都适用。我用5表示相机的正交尺寸

using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
[ExecuteInEditMode]
#endif
public class SpriteStretch : MonoBehaviour {
    public enum Stretch{Horizontal, Vertical, Both};
    public Stretch stretchDirection = Stretch.Horizontal;
    public Vector2 offset = new Vector2(0f,0f);

    SpriteRenderer sprite;
    Transform _thisTransform;


    void Start () 
    {
        _thisTransform = transform;
        sprite = GetComponent<SpriteRenderer>();
        StartCoroutine("stretch");
    }
    #if UNITY_EDITOR
    void Update()
    {
        scale();
    }
    #endif
    IEnumerator stretch()
    {
        yield return new WaitForEndOfFrame();
        scale();
    }
    void scale()
    {
        float worldScreenHeight = Camera.main.orthographicSize *2f;
        float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
        float ratioScale = worldScreenWidth / sprite.sprite.bounds.size.x;
        ratioScale += offset.x;
        float h = worldScreenHeight /  sprite.sprite.bounds.size.y;
        h += offset.y;
        switch(stretchDirection)
        {
        case Stretch.Horizontal:
            _thisTransform.localScale = new Vector3(ratioScale,_thisTransform.localScale.y,_thisTransform.localScale.z);
            break;
        case Stretch.Vertical:
            _thisTransform.localScale = new Vector3(_thisTransform.localScale.x, h,_thisTransform.localScale.z);
            break;
        case Stretch.Both:
            _thisTransform.localScale = new Vector3(ratioScale, h,_thisTransform.localScale.z);
            break;
        default:break;
        }
    }
}
使用UnityEngine;
使用系统集合;
#如果统一编辑器
[执行编辑模式]
#恩迪夫
公共类SpriteStretch:单行为{
公共枚举拉伸{水平、垂直、双向};
公共拉伸拉伸方向=拉伸。水平;
公共向量2偏移=新向量2(0f,0f);
精灵精灵;
转变(即转变);;
无效开始()
{
_这个变换=变换;
sprite=GetComponent();
开始例行程序(“拉伸”);
}
#如果统一编辑器
无效更新()
{
比例();
}
#恩迪夫
IEnumerator stretch()
{
返回新的WaitForEndOfFrame();
比例();
}
空隙率()
{
float worldScreenHeight=Camera.main.orthographicSize*2f;
浮动worldScreenWidth=worldScreenHeight/Screen.height*Screen.width;
float ratioScale=worldScreenWidth/sprite.sprite.bounds.size.x;
比率比例+=偏移量.x;
float h=世界屏幕高度/sprite.sprite.bounds.size.y;
h+=偏移量y;
开关(拉伸方向)
{
案例拉伸。水平:
_thistTransform.localScale=新向量3(ratioScale,_thistTransform.localScale.y,_thistTransform.localScale.z);
打破
案例拉伸。垂直:
_thistTransform.localScale=新向量3(_thistTransform.localScale.x,h,_thistTransform.localScale.z);
打破
案例延伸。两个:
_thistTransform.localScale=新向量3(ratioScale,h,_thistTransform.localScale.z);
打破
默认:中断;
}
}
}

首先,我想说的是,没有好的解决方案,只有不那么坏的

最简单的方法是只支持一个纵横比,这样你就可以在不失真的情况下上下缩放所有内容,但这几乎不是一个选项

第二个最简单的方法是使游戏具有一个纵横比,并在边缘添加黑条(或其他东西),使实际游戏区域具有相同的纵横比

如果你仍然想要不同的纵横比,解决方案是增加游戏面积,但这可能会导致不同纵横比的人获得优势,因为他们可以看到更远或更高的东西,这可能会打乱你的关卡设计

我刚刚发布了一个答案,只要你在这里有恒定的纵横比,就可以让一切自动工作