Advertisement
Guest User

Untitled

a guest
Sep 5th, 2014
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CamScript : MonoBehaviour {
  5.    
  6.     void Start ()
  7.     {
  8.         // установка нужного соотношения сторон
  9.         // переменные могут быть установлены как публичные
  10.         // кроме того, возможно автоматически определять это
  11.         // соотношение
  12.         // (но я не знаю как)
  13.         float targetaspect = 16.0f / 9.0f;
  14.        
  15.         // отношение сторон в текущем случае
  16.         float windowaspect = (float)Screen.width / (float)Screen.height;
  17.        
  18.         // устанавливаем высоту окна по нужному значению
  19.         float scaleheight = windowaspect / targetaspect;
  20.        
  21.         // подключаемся к камере
  22.         Camera camera = GetComponent<Camera>();
  23.        
  24.  
  25.         if (scaleheight < 1.0f)
  26.         {  
  27.             Rect rect = camera.rect;
  28.            
  29.             rect.width = 1.0f;
  30.             rect.height = scaleheight;
  31.             rect.x = 0;
  32.             rect.y = (1.0f - scaleheight) / 2.0f;
  33.            
  34.             camera.rect = rect;
  35.         }
  36.  
  37.         {
  38.             float scalewidth = 1.0f / scaleheight;
  39.            
  40.             Rect rect = camera.rect;
  41.            
  42.             rect.width = scalewidth;
  43.             rect.height = 1.0f;
  44.             rect.x = (1.0f - scalewidth) / 2.0f;
  45.             rect.y = 0;
  46.            
  47.             camera.rect = rect;
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement