【问题标题】:How to auto-scale to different screen resolutions?如何自动缩放到不同的屏幕分辨率?
【发布时间】:2014-08-06 02:50:35
【问题描述】:

我正在统一制作一个无限跑步者,我有一个瓦片生成器/生成器及其基于屏幕高度和宽度生成的游戏对象,我设法使它与宽度一起工作,但是当改变高度时相机不跟随,我无法做到这一点。

无论如何,我的代码并不好,我在过去的 6 个小时里都花在了这上面,但我不欣赏结果。

我发现你可以为相机定义一个纵横比,它会自动调整你的比例,但它会扭曲图像并且看起来不太好。

由于所有这些注释,这是自动缩放 2D 平台游戏的最佳方式(不考虑 GUI,仅考虑 GameObjects)

【问题讨论】:

    标签: c# unity3d 2d resolution


    【解决方案1】:

    我正在使用此脚本根据精灵的大小拉伸精灵,适用于大多数情况。我使用 5 作为相机的 orthographicSize。

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

    【讨论】:

      【解决方案2】:

      首先我想说,没有好的解决方案只有更差的。

      最简单的方法是只支持一种纵横比,这样您就可以在不失真的情况下放大和缩小所有内容,但这几乎不是一种选择。

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

      如果您仍然想要不同的纵横比,解决方案是增加游戏区域,但这可能会导致具有不同纵横比的人获得优势,因为他们可以看到更远或更远的地方,这可能会打乱您的关卡设计。

      我刚刚在https://stackoverflow.com/a/25160299/2885785987654321@https://stackoverflow.com/a/25160299/2885785 发布了一个关于如何让一切自动运行的答案

      【讨论】:

        猜你喜欢
        • 2016-07-27
        • 1970-01-01
        • 2016-10-21
        • 1970-01-01
        • 2016-03-01
        • 1970-01-01
        • 2013-09-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多