Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 支持统一的多纵横比_Android_Ios_Unity3d_2d_Aspect Ratio - Fatal编程技术网

Android 支持统一的多纵横比

Android 支持统一的多纵横比,android,ios,unity3d,2d,aspect-ratio,Android,Ios,Unity3d,2d,Aspect Ratio,我一直在尝试创建一个Unity 2D游戏,它支持android和平板电脑设备的每一个纵横比。Unity提供或推荐了一种方法来实现这一点吗?在我的例子中,我通过创建一个标尺来完成工作 因此,它可以支持任何屏幕 //Find Screen resolution at the splash or loading screen float scalex = DataFactory.SCREEN_WIDTH / (float)DataFactory.OUR_FIXED_GAME_SCREEN;

我一直在尝试创建一个Unity 2D游戏,它支持android和平板电脑设备的每一个纵横比。Unity提供或推荐了一种方法来实现这一点吗?

在我的例子中,我通过创建一个标尺来完成工作

因此,它可以支持任何屏幕

//Find Screen resolution at the splash or loading screen
    float scalex = DataFactory.SCREEN_WIDTH / (float)DataFactory.OUR_FIXED_GAME_SCREEN;
    float scaley = DataFactory.SCREEN_HEIGHT / (float)DataFactory.OUR_FIXED_GAME_SCREEN;
    if (scalex >= scaley)
        DataFactory.SCALE = scalex;
    else 
        DataFactory.SCALE = scaley;

//Set all size in game at the start
private int gameWidth = (int) (1400 * DataFactory.SCALE);
private int gameHeight = (int) (800 * DataFactory.SCALE);


private int startGameX = (int) (300 * DataFactory.SCALE);
private int startGameY = (int) (280 * DataFactory.SCALE);

private int objectX = (int) (410  * DataFactory.SCALE) + DataFactory.BEGIN_X;
private int objectY = (int) (979 * DataFactory.SCALE) + DataFactory.BEGIN_Y;

private int objectGapX = (int) (400 * DataFactory.SCALE);
private int objectGapY = (int) (180 * DataFactory.SCALE);
private int objectWidth = (int) (560 * DataFactory.SCALE);
private int objectHeight = (int) (400 * DataFactory.SCALE);


private int xRing = (int) (1005 * DataFactory.SCALE) +  DataFactory.BEGIN_X;
private int yRing = (int) (1020 * DataFactory.SCALE) + DataFactory.BEGIN_Y;
private int radiusOutside = (int) (740 * DataFactory.SCALE);
private int radiusInside = (int) (480 * DataFactory.SCALE);
private int radiusObject = (int) (600 * DataFactory.SCALE);
private int yObjectRing = (int) (920 * DataFactory.SCALE) + DataFactory.BEGIN_Y;
*所有固定值都是我基于单屏幕创建的值*

这是我制作的3D游戏的一些示例,但是,我在GUI部分仍然使用相同的概念

这是我使用这个概念的2D游戏的一些示例


我知道这是一篇老文章,我想展示一个替代方案。您可以尝试定义要向哪个轴缩放游戏(例如,所有宽度应始终可见,高度应分别缩放到宽度):将所有场景对象存储在父对象中并缩放父对象。 例如(我的宽度是固定的,高度被切割成宽度)


注:我的玩家最初是规模(1,1,1)

有几件事需要考虑。第一个问题是应该允许哪些元素进行缩放?有两类,即UI和游戏元素

游戏元素部分可能意味着很多事情。如果游戏空间有限,关键点通常是,包括大量的“负空间”,或图像中不会对游戏产生显著影响的部分。例如,可以从左侧和右侧裁剪下面的图像,而不会显著影响图像。将图像的中心部分作为关键元素,或放在一边

人们也可以拉伸元素,尽管这可能会导致不良影响。对于这样的背景元素来说,拥有多余的图像并使用不同的纵横比进行测试是最好的方法。这些背景元素可以放置在背景中,画布设置为“按屏幕大小缩放”,并将“屏幕匹配模式”设置为最适合图像的效果。有关详细信息,请参阅“”

至于其他UI元素,关键是使用定位点。当您放置UI元素时,您可以告诉它以一定数量的像素或填充屏幕的一部分。查看每个这样的UI对象所包含的“Rect Transform”组件。您也可以在屏幕上调整这些参数


最后,您可以通过编程来完成。存在
Screen.height
Screen.width
。您可以在运行时根据需要调整对象以使其正常工作。我建议你不要对所有东西都这样做,但在某些情况下可能会有所帮助。

你应该将所有东西都放在一个主游戏对象中,并使用一个简单的脚本(摄像头“某物”可以帮助你检测屏幕比例)按不同比例缩放它。 那是我的主意。
很抱歉我的英语不好。

尝试新的带有锚定功能的unity3d ui。

对于所有ui元素,必须使用unit ui系统,这是支持多平台和纵横比的最佳方式

以下内容基于第条: 这篇文章基本上和我说的一样: 1) 关于高分辨率的设计,文章说:

“另一种方法是使用更高分辨率的图形(实际上是 具有您要瞄准的设备的最高分辨率)和 在所有的设备上都缩小它的规模。但是,这不是一个好主意,因为 实际上,您需要更多的内存,并且会丢失上的性能 低端设备。”

因此,以高分辨率进行设计,然后按比例缩小,这不是一个好方法

因此,正如文章所说,最好的办法是以不同的分辨率(SD、HD UD)使用不同的图像,并在加载游戏时加载正确的图像:文章说: “最好的方法是使用分辨率更高的不同图像,在iPhone4上使用此图像版本,在iPhone3GS上使用低分辨率版本,这实际上就是苹果使用文件名后缀为@2x的图像所做的

以类似的方式,你可以以iPad 3所需的超高分辨率创建所有图形,并附加另一个后缀,然后根据设备的屏幕分辨率加载正确的图像。这称为内容缩放,因为该游戏只为一个“逻辑”屏幕编写场景大小,所有图像和字体都按设备分辨率缩放

因此,通过使用这种方法,我们解决了具有不同分辨率的目标设备的问题。 不,正如articole所说,还有一个问题是针对具有不同纵横比的设备: 摘自文章: “但是,当您想要以具有不同纵横比的设备为目标时,这种方法是不够的”

为此,我通常选择适合游戏设计的纵横比,并使用以下脚本在不同设备上保持相同的纵横比:

    /* The MIT License (MIT)

Copyright (c) 2014, Marcel Căşvan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

using System;
using System.Collections;
using UnityEngine;

[ExecuteInEditMode]
[RequireComponent (typeof (Camera))]
public class CameraFit : MonoBehaviour
{
    #region FIELDS
    public float UnitsForWidth = 1; // width of your scene in unity units
    public static CameraFit Instance;

    private float _width;
    private float _height;
    //*** bottom screen
    private Vector3 _bl;
    private Vector3 _bc;
    private Vector3 _br;
    //*** middle screen
    private Vector3 _ml;
    private Vector3 _mc;
    private Vector3 _mr;
    //*** top screen
    private Vector3 _tl;
    private Vector3 _tc;
    private Vector3 _tr;
    #endregion

    #region PROPERTIES
    public float Width {
        get {
            return _width;
        }
    }
    public float Height {
        get {
            return _height;
        }
    }

    // helper points:
    public Vector3 BottomLeft {
        get {
            return _bl;
        }
    }
    public Vector3 BottomCenter {
        get {
            return _bc;
        }
    }
    public Vector3 BottomRight {
        get {
            return _br;
        }
    }
    public Vector3 MiddleLeft {
        get {
            return _ml;
        }
    }
    public Vector3 MiddleCenter {
        get {
            return _mc;
        }
    }
    public Vector3 MiddleRight {
        get {
            return _mr;
        }
    }
    public Vector3 TopLeft {
        get {
            return _tl;
        }
    }
    public Vector3 TopCenter {
        get {
            return _tc;
        }
    }
    public Vector3 TopRight {
        get {
            return _tr;
        }
    }
    #endregion

    #region METHODS
    private void Awake()
    {
        try{
            if((bool)GetComponent<Camera>()){
                if (GetComponent<Camera>().orthographic) {
                    ComputeResolution();
                }
            }
        }catch (Exception e){
            Debug.LogException(e, this);
        }
    }

    private void ComputeResolution()
    {
        float deviceWidth;
        float deviceHeight;
        float leftX, rightX, topY, bottomY;
#if UNITY_EDITOR
        deviceWidth = GetGameView().x;
        deviceHeight = GetGameView().y;
#else
        deviceWidth = Screen.width;
        deviceHeight = Screen.height;
#endif
        //Debug.Log("Aspect Ratio " + GetComponent<Camera>().aspect);
        if (GetComponent<Camera>().aspect >= 0.7f)
        {
            UnitsForWidth = 2.2f;
        } 
        else
        {
            UnitsForWidth = 2f;
        }
        /* Set the ortograpish size (shich is half of the vertical size) when we change the ortosize of the camera the item will be scaled 
         * autoamtically to fit the size frame of the camera
         */
        GetComponent<Camera>().orthographicSize = 1f / GetComponent<Camera>().aspect * UnitsForWidth / 2f;

        //Get the new height and Widht based on the new orthographicSize
        _height = 2f * GetComponent<Camera>().orthographicSize;
        _width = _height * GetComponent<Camera>().aspect;

        float cameraX, cameraY;
        cameraX = GetComponent<Camera>().transform.position.x;
        cameraY = GetComponent<Camera>().transform.position.y;

        leftX = cameraX - _width / 2;
        rightX = cameraX + _width / 2;
        topY = cameraY + _height / 2;
        bottomY = cameraY - _height / 2;

        //*** bottom
        _bl = new Vector3(leftX, bottomY, 0);
        _bc = new Vector3(cameraX, bottomY, 0);
        _br = new Vector3(rightX, bottomY, 0);
        //*** middle
        _ml = new Vector3(leftX, cameraY, 0);
        _mc = new Vector3(cameraX, cameraY, 0);
        _mr = new Vector3(rightX, cameraY, 0);
        //*** top
        _tl = new Vector3(leftX, topY, 0);
        _tc = new Vector3(cameraX, topY , 0);
        _tr = new Vector3(rightX, topY, 0);
        Instance = this;
    }

    private void Update()
    {
        #if UNITY_EDITOR
        ComputeResolution();
        #endif
    }

    private void OnDrawGizmos()
    {
        if (GetComponent<Camera>().orthographic) {
            DrawGizmos();
        }
    }

    private void DrawGizmos()
    {
        //*** bottom
        Gizmos.DrawIcon(_bl, "point.png", false);
        Gizmos.DrawIcon(_bc, "point.png", false);
        Gizmos.DrawIcon(_br, "point.png", false);
        //*** middle
        Gizmos.DrawIcon(_ml, "point.png", false);
        Gizmos.DrawIcon(_mc, "point.png", false);
        Gizmos.DrawIcon(_mr, "point.png", false);
        //*** top
        Gizmos.DrawIcon(_tl, "point.png", false);
        Gizmos.DrawIcon(_tc, "point.png", false);
        Gizmos.DrawIcon(_tr, "point.png", false);

        Gizmos.color = Color.green;
        Gizmos.DrawLine(_bl, _br);
        Gizmos.DrawLine(_br, _tr);
        Gizmos.DrawLine(_tr, _tl);
        Gizmos.DrawLine(_tl, _bl);
    }

    private Vector2 GetGameView()
    {
        System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
        System.Reflection.MethodInfo getSizeOfMainGameView =
            T.GetMethod("GetSizeOfMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
        System.Object resolution = getSizeOfMainGameView.Invoke(null, null);
        return (Vector2)resolution;
    }
    #endregion
}


  [1]: http://v-play.net/doc/vplay-different-screen-sizes/

希望这能有所帮助,有关更多信息,请阅读我上面链接的文章。

这是3D游戏。我的问题与缩放部分无关,它与纵横比有关,仅缩放游戏对象无法解决这一问题。我也在我制作的2D游戏中使用此概念……也使用此缩放概念,无论是什么scre如果尺寸合适,它将保持所有比例为合适的比例
    /* The MIT License (MIT)

Copyright (c) 2014, Marcel Căşvan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

using System;
using System.Collections;
using UnityEngine;

[ExecuteInEditMode]
[RequireComponent (typeof (Camera))]
public class CameraFit : MonoBehaviour
{
    #region FIELDS
    public float UnitsForWidth = 1; // width of your scene in unity units
    public static CameraFit Instance;

    private float _width;
    private float _height;
    //*** bottom screen
    private Vector3 _bl;
    private Vector3 _bc;
    private Vector3 _br;
    //*** middle screen
    private Vector3 _ml;
    private Vector3 _mc;
    private Vector3 _mr;
    //*** top screen
    private Vector3 _tl;
    private Vector3 _tc;
    private Vector3 _tr;
    #endregion

    #region PROPERTIES
    public float Width {
        get {
            return _width;
        }
    }
    public float Height {
        get {
            return _height;
        }
    }

    // helper points:
    public Vector3 BottomLeft {
        get {
            return _bl;
        }
    }
    public Vector3 BottomCenter {
        get {
            return _bc;
        }
    }
    public Vector3 BottomRight {
        get {
            return _br;
        }
    }
    public Vector3 MiddleLeft {
        get {
            return _ml;
        }
    }
    public Vector3 MiddleCenter {
        get {
            return _mc;
        }
    }
    public Vector3 MiddleRight {
        get {
            return _mr;
        }
    }
    public Vector3 TopLeft {
        get {
            return _tl;
        }
    }
    public Vector3 TopCenter {
        get {
            return _tc;
        }
    }
    public Vector3 TopRight {
        get {
            return _tr;
        }
    }
    #endregion

    #region METHODS
    private void Awake()
    {
        try{
            if((bool)GetComponent<Camera>()){
                if (GetComponent<Camera>().orthographic) {
                    ComputeResolution();
                }
            }
        }catch (Exception e){
            Debug.LogException(e, this);
        }
    }

    private void ComputeResolution()
    {
        float deviceWidth;
        float deviceHeight;
        float leftX, rightX, topY, bottomY;
#if UNITY_EDITOR
        deviceWidth = GetGameView().x;
        deviceHeight = GetGameView().y;
#else
        deviceWidth = Screen.width;
        deviceHeight = Screen.height;
#endif
        //Debug.Log("Aspect Ratio " + GetComponent<Camera>().aspect);
        if (GetComponent<Camera>().aspect >= 0.7f)
        {
            UnitsForWidth = 2.2f;
        } 
        else
        {
            UnitsForWidth = 2f;
        }
        /* Set the ortograpish size (shich is half of the vertical size) when we change the ortosize of the camera the item will be scaled 
         * autoamtically to fit the size frame of the camera
         */
        GetComponent<Camera>().orthographicSize = 1f / GetComponent<Camera>().aspect * UnitsForWidth / 2f;

        //Get the new height and Widht based on the new orthographicSize
        _height = 2f * GetComponent<Camera>().orthographicSize;
        _width = _height * GetComponent<Camera>().aspect;

        float cameraX, cameraY;
        cameraX = GetComponent<Camera>().transform.position.x;
        cameraY = GetComponent<Camera>().transform.position.y;

        leftX = cameraX - _width / 2;
        rightX = cameraX + _width / 2;
        topY = cameraY + _height / 2;
        bottomY = cameraY - _height / 2;

        //*** bottom
        _bl = new Vector3(leftX, bottomY, 0);
        _bc = new Vector3(cameraX, bottomY, 0);
        _br = new Vector3(rightX, bottomY, 0);
        //*** middle
        _ml = new Vector3(leftX, cameraY, 0);
        _mc = new Vector3(cameraX, cameraY, 0);
        _mr = new Vector3(rightX, cameraY, 0);
        //*** top
        _tl = new Vector3(leftX, topY, 0);
        _tc = new Vector3(cameraX, topY , 0);
        _tr = new Vector3(rightX, topY, 0);
        Instance = this;
    }

    private void Update()
    {
        #if UNITY_EDITOR
        ComputeResolution();
        #endif
    }

    private void OnDrawGizmos()
    {
        if (GetComponent<Camera>().orthographic) {
            DrawGizmos();
        }
    }

    private void DrawGizmos()
    {
        //*** bottom
        Gizmos.DrawIcon(_bl, "point.png", false);
        Gizmos.DrawIcon(_bc, "point.png", false);
        Gizmos.DrawIcon(_br, "point.png", false);
        //*** middle
        Gizmos.DrawIcon(_ml, "point.png", false);
        Gizmos.DrawIcon(_mc, "point.png", false);
        Gizmos.DrawIcon(_mr, "point.png", false);
        //*** top
        Gizmos.DrawIcon(_tl, "point.png", false);
        Gizmos.DrawIcon(_tc, "point.png", false);
        Gizmos.DrawIcon(_tr, "point.png", false);

        Gizmos.color = Color.green;
        Gizmos.DrawLine(_bl, _br);
        Gizmos.DrawLine(_br, _tr);
        Gizmos.DrawLine(_tr, _tl);
        Gizmos.DrawLine(_tl, _bl);
    }

    private Vector2 GetGameView()
    {
        System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
        System.Reflection.MethodInfo getSizeOfMainGameView =
            T.GetMethod("GetSizeOfMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
        System.Object resolution = getSizeOfMainGameView.Invoke(null, null);
        return (Vector2)resolution;
    }
    #endregion
}


  [1]: http://v-play.net/doc/vplay-different-screen-sizes/
/***
 * This script will anchor a GameObject to a relative screen position.
 * This script is intended to be used with CameraFit.cs by Marcel Căşvan, available here: http://gamedev.stackexchange.com/a/89973/50623
 * 
 * Note: For performance reasons it's currently assumed that the game resolution will not change after the game starts.
 * You could not make this assumption by periodically calling UpdateAnchor() in the Update() function or a coroutine, but is left as an exercise to the reader.
 */
/* The MIT License (MIT)

Copyright (c) 2015, Eliot Lash

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class CameraAnchor : MonoBehaviour {
    public enum AnchorType {
        BottomLeft,
        BottomCenter,
        BottomRight,
        MiddleLeft,
        MiddleCenter,
        MiddleRight,
        TopLeft,
        TopCenter,
        TopRight,
    };
    public AnchorType anchorType;
    public Vector3 anchorOffset;

    // Use this for initialization
    void Start () {
        UpdateAnchor();
    }

    void UpdateAnchor() {
        switch(anchorType) {
        case AnchorType.BottomLeft:
            SetAnchor(CameraFit.Instance.BottomLeft);
            break;
        case AnchorType.BottomCenter:
            SetAnchor(CameraFit.Instance.BottomCenter);
            break;
        case AnchorType.BottomRight:
            SetAnchor(CameraFit.Instance.BottomRight);
            break;
        case AnchorType.MiddleLeft:
            SetAnchor(CameraFit.Instance.MiddleLeft);
            break;
        case AnchorType.MiddleCenter:
            SetAnchor(CameraFit.Instance.MiddleCenter);
            break;
        case AnchorType.MiddleRight:
            SetAnchor(CameraFit.Instance.MiddleRight);
            break;
        case AnchorType.TopLeft:
            SetAnchor(CameraFit.Instance.TopLeft);
            break;
        case AnchorType.TopCenter:
            SetAnchor(CameraFit.Instance.TopCenter);
            break;
        case AnchorType.TopRight:
            SetAnchor(CameraFit.Instance.TopRight);
            break;
        }
    }

    void SetAnchor(Vector3 anchor) {
        Vector3 newPos = anchor + anchorOffset;
        if (!transform.position.Equals(newPos)) {
            transform.position = newPos;
        }
    }


    // Update is called once per frame
#if UNITY_EDITOR
    void Update () {
        UpdateAnchor();
    }
#endif
}