Soporta múltiples proporciones en Unity

He estado tratando de crear un juego Unity 2D que admita todas las relaciones de aspecto de dispositivos para Android y tabletas. ¿Hay alguna manera de hacerlo que haya sido proporcionada o recomendada por Unity?

Hay algunas cosas que deben ser consideradas. La primera es qué elementos deben ser permitidos a escala? Hay dos categorías, a saber, la interfaz de usuario y elementos de juego.

La parte Elementos del juego puede significar un montón de cosas. Si el espacio de juego es limitado, la clave es típicamente, incluyendo una generosa porción de "espacio negativo", o partes de la imagen que no afectan significativamente al juego. Por ejemplo, la imagen de abajo podría recortarse desde la izquierda y la derecha sin afectar la imagen significativamente. Coloque la parte central de la imagen como el elemento clave, o un lado.

Introduzca aquí la descripción de la imagen

Uno podría también estirar los elementos, aunque eso podría conducir a efectos indeseables. Tener un excedente de imagen y pruebas con diferentes raciones de aspecto es la mejor manera típica para tales elementos de fondo. Estos elementos de fondo pueden colocarse en segundo plano, con el lienzo ajustado en "Escala con tamaño de pantalla" y establecer el "Modo de coincidencia de pantalla" con el efecto que mejor funcione para la imagen. Consulte " Escala de lienzo " para obtener más información.

En cuanto a los otros elementos de la interfaz de usuario, la clave es utilizar puntos de anclaje. Puede decirle a un elemento de interfaz de usuario que tome un número de píxeles o llene una parte de la pantalla cuando lo coloque. Observe el componente "Transformar Rect" incluido con cada objeto de interfaz de usuario. También puede ajustar estos en la pantalla.

Por último, podría hacerlo programáticamente. Existe Screen.height y Screen.width . Puede ajustar los objetos como desee en tiempo de ejecución para que funcione. Le sugiero que no haga esto por todo, pero podría ayudar en algunos casos.

En mi caso, yo trabajo por crear todo ello como una escala

Por lo tanto, podría soportar no importa la pantalla son

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

* TODO VALOR FIJO ES EL VALOR QUE CREO POR BASE EN UNA SOLA PANTALLA *

Esta es una muestra de juego 3D que hice, sin embargo, todavía uso el mismo concepto en la parte GUI

Introduzca aquí la descripción de la imagen

Esta es una muestra de 2D Game que he usado este concepto

Introduzca aquí la descripción de la imagen

Introduzca aquí la descripción de la imagen

Sé que es un viejo post, quería mostrar una alternativa para esto. Puede intentar definir hacia qué eje desea escalar su juego (por ejemplo, todo el ancho debe ser siempre visible, la altura debe escalar, respectivamente, al ancho): almacenar todos los objetos de escena en un padre y escalar al padre. Ex. (Mi ancho estaba fijo y la altura se cortó para el ancho)

 bottomRightPosition = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, - Camera.main.transform.position.z)); topLeftPosition = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, -Camera.main.transform.position.z)); float Width = topLeftPosition.x -bottomRightPosition.x float scale = width / optimizedWorldDistance gameHolder.transform.localScale = new Vector3(scale,scale,1); // for 2D 

Nota: mi gameHolder es inicialmente de escala (1,1,1);

Debe poner todo en un objeto de juego principal y escalarlo con relación de diferencia utilizando un guión simple (la cámara "algo" podría ayudarle a detectar la proporción de pantalla). Esa es mi idea. Perdón por mi mal ingles.

Pruebe el nuevo unit3d ui con anclaje.

Para todos los elementos de la interfaz de usuario, debe utilizar el sistema de interfaz de usuario de la unidad, que es la mejor forma de admitir múltiples plataformas y relaciones de aspecto.

El siguiente contenido se basa en este artículo: Este artículo dice básicamente las mismas cosas que estoy diciendo: 1) En cuanto a diseño SOLAMENTE en alta resolución, el artículo dice:

"Otro enfoque consiste en utilizar gráficos de mayor resolución (de hecho, el que tiene la mayor resolución del dispositivo al que desea apuntar) y reducirlo en todos los dispositivos. Sin embargo, esto no es una buena idea porque necesita mucho más memoria y Perderá el rendimiento en dispositivos de gama baja ".

Así que el diseño en alta resolución y luego reducir a escala no es el buen enfoque.

Así como el artículo dice que lo mejor es tener diferentes imágenes para diferentes resoluciones (SD, HD UD) y cargar la imagen correcta cuando el juego se está cargando: el artículo dice: "El mejor enfoque es usar una imagen diferente con la más alta Resolución y el uso de esta versión de la imagen en el iPhone 4 y la versión de baja resolución en un iPhone 3GS, que es efectivamente lo que Apple está haciendo mediante el uso de imágenes con un sufijo @ 2x para el nombre del archivo.

De forma similar, puede crear todos sus gráficos en ultra alta resolución necesarios para el iPad 3 por ejemplo y añadir otro sufijo, y cargar la imagen correcta en función de la resolución de pantalla del dispositivo. Esto se denomina escalado de contenido, ya que el juego se escribió sólo para un solo tamaño de escena "lógico" y todas las imágenes y fuentes se han escalado a la resolución del dispositivo.

Así que mediante este enfoque resolvimos el problema de los dispositivos de destino con distintas RESOLUCIONES. No como el articole dijo que hay otro problema que es el objetivo de los dispositivos con diferentes ASPECT RATIO: Del artichle: "Sin embargo, este enfoque no es suficiente cuando se desea orientar los dispositivos con diferentes relaciones de aspecto"

Para ello, generalmente elijo una relación de aspecto que puede ajustarse al diseño de mi juego y utilizar la siguiente secuencia de comandos para mantener la misma relación de aspecto en diferentes dispositivos:

  /* 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/ 

Esto debe ser único el problema de relaciones de aspecto diferente. Ahora si desea anclar algún objeto de juego el estar siempre en un evento de posición fija si el juego se redimensiona en dispositivos con diferentes relaciones de aspecto, puede utilizar el siguiente script:

 /*** * 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 } 

Espero que esto puede ayudar, para más información, lea el artículo que he vinculado anteriormente.

  • La aplicación que implementa Parse Unity Plugin se bloquea en el dispositivo android pero funciona bien en el editor
  • Cómo trabajar con diferentes resoluciones de pantalla
  • El proyecto Build Unity con Jenkins falló
  • Implementación de los permisos de Android 6.0 en unity3d
  • Juego para móviles con Unity - Plugin para compra en la aplicación
  • Cómo hacer la animación 2D en Unity
  • Intercambiar píxeles de MainTex con otras texturas a través de la superficie Shader (Unity)
  • Mostrar Unity Scene como Sub View en el estudio android
  • Cómo encontrar el origen de un permiso en Unity Android
  • Google Analytics Unity plugin - No se muestran datos
  • Callback Listener en Unity - Cómo llamar al método de archivo de script desde UnityPlayerActivity en Android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.