Search Unity

(3D) Camera display adapter to design aspect

Discussion in 'Community Learning & Teaching' started by IsGreen, Apr 26, 2015.

  1. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    In Unity we can have 3D and 2D objects.

    Each adapted to display differently:

    - 3D objects: Setting Camera.rect and Camera.pixelRect
    -The Sprites are like 3D planes but are drawings on both sides. That is, their aspect would be adjusted as a 3D object.
    - UI system has CanvasScaler component.
    - GUI system used GUI.matrix.
    - GUIText and GUITexture use screen coordinate as GUI but do not use a matrix variable, you need set the Rect directly.

    _____________________________________________________________

    (3D) Camera display adapter to design aspect

    Used to 3D objects and sprites (2D). Resolutions and aspect, from lowest to highest:


    5: 4 --> aspect(width/height) 1.25 for example 1280x1024
    4: 3 --> 1.33333 ... f.e. 800x600
    3: 2 --> 1.5 f.e. 1600x1024
    16:10 -> 1.6 f.e. 1280x768
    16: 9 -> 1.77777 ... f.e. 1280x720

    Would adjust the rendering area by Camera.rect, and would place in the center of screen with Camera.pixelRect:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class DesignAdapter : MonoBehaviour {
    4.  
    5.     public enum Aspect {Aspect5by4, Aspect4by3, Aspect3by2,Aspect16by10,Aspect16by9};
    6.  
    7.     new public Camera camera;
    8.     public Aspect originalAspect = Aspect.Aspect5by4;
    9.  
    10.     float[] ratios = new float[]{1.25f,1.333f,1.5f,1.6f,1.777f};
    11.  
    12.     void Reduce(Aspect currentAspect){
    13.  
    14.         float percentage = (ratios[(int)currentAspect] - ratios[(int)originalAspect])
    15.                             / ratios[(int)currentAspect];
    16.         this.camera.rect = new Rect(0f,0f,1f - percentage , 1f);
    17.         this.camera.pixelRect = new Rect(Screen.width * percentage * 0.5f , 0f , Screen.width * (1f - percentage), (float)Screen.height);
    18.  
    19.     }
    20.  
    21.     void Grow(Aspect currentAspect){
    22.  
    23.         float percentage = (ratios[(int)originalAspect] - ratios[(int)currentAspect])
    24.                             / ratios[(int)originalAspect];
    25.         this.camera.rect = new Rect(0f,0f,1f,1f - percentage);
    26.         this.camera.pixelRect = new Rect(0f, Screen.height * percentage * 0.5f , (float)Screen.width , Screen.height * (1f - percentage));
    27.  
    28.     }
    29.  
    30.     void Awake(){
    31.  
    32.         float screenAspect = this.camera.aspect;
    33.         Aspect currentAspect;
    34.  
    35.         if(screenAspect < 1.29f)  currentAspect = Aspect.Aspect5by4;
    36.         else if(screenAspect < 1.4f) currentAspect = Aspect.Aspect4by3;
    37.         else if(screenAspect < 1.6f) currentAspect = Aspect.Aspect3by2;
    38.         else if(screenAspect < 1.7) currentAspect = Aspect.Aspect16by10;
    39.         else currentAspect = Aspect.Aspect16by9;
    40.  
    41.         if(currentAspect > this.originalAspect) this.Reduce(currentAspect);
    42.         if(currentAspect < this.originalAspect) this.Grow(currentAspect);
    43.  
    44.     }
    45.  
    46. }
    47.