Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Camera2D : MonoBehaviour
  4. {
  5.     public Transform Area;
  6.  
  7.     Vector3 CurrentPosition;
  8.     Vector3 CurrentScale;
  9.  
  10.     public void Set()
  11.     {
  12.         float height = Area.localScale.y * 100;
  13.         float width = Area.localScale.x * 100;
  14.  
  15.         float w = Screen.width / width;
  16.         float h = Screen.height / height;
  17.  
  18.         float ratio = w / h;
  19.         float size = (height / 2) / 100f;
  20.  
  21.         if (w < h)
  22.             size /= ratio;
  23.  
  24.         Camera.main.orthographicSize = size;
  25.  
  26.         Vector2 position = Area.transform.position;
  27.  
  28.         Vector3 camPosition = position;
  29.         Vector3 point = Camera.main.WorldToViewportPoint(camPosition);
  30.         Vector3 delta = camPosition - Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z));
  31.         Vector3 destination = transform.position + delta;
  32.  
  33.         transform.position = destination;
  34.     }
  35.  
  36.     public void LateUpdate()
  37.     {
  38.         if (CurrentPosition != Area.transform.position || CurrentScale != Area.transform.localScale) {
  39.             CurrentPosition = Area.transform.position;
  40.             CurrentScale = Area.transform.localScale;
  41.             Set ();
  42.         }
  43.     }
  44.    
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement