Advertisement
Guest User

Untitled

a guest
Jun 17th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1.         private float CalculateMenuWidth()
  2.         {
  3.             var menuRect = GameObject.Find("Panel").GetComponent<RectTransform>();
  4.  
  5.             var worldSpaceCorners = new Vector3[4];
  6.             menuRect.GetWorldCorners(worldSpaceCorners);
  7.  
  8.             return Mathf.Abs(worldSpaceCorners[2].x - worldSpaceCorners[0].x);
  9.         }
  10.  
  11.         private void ContainWithin(Rect rect)
  12.         {
  13.             // Make sure camera zoom fits inside level
  14.             if (this.cameraComponent.orthographic)
  15.             {
  16.                 var h = rect.height;
  17.                 var w = rect.width / this.cameraComponent.aspect;
  18.                 var newSize = Mathf.Min(w, h) * 0.5f;
  19.  
  20.                 if (this.cameraComponent.orthographicSize > newSize)
  21.                 {
  22.                     this.cameraComponent.orthographicSize = newSize;
  23.                     this.ZoomMin = (int)this.cameraComponent.orthographicSize;
  24.                 }
  25.             }
  26.  
  27.             // Create padding around the map so the UI buttons don't obscure it for ray casting, viewing, etc.
  28.             var paddingRight = this.CalculateMenuWidth();
  29.             var paddingLeft = 0;
  30.             var paddingTop = 0;
  31.             var paddingBottom = 0;
  32.  
  33.             this.cameraRect = this.cameraComponent.GetCameraWorldRect();
  34.             var correction = Vector3.zero;
  35.  
  36.             if (this.cameraRect.xMin < rect.xMin + paddingLeft)
  37.             {
  38.                 correction.x -= this.cameraRect.xMin - (rect.xMin + paddingLeft);
  39.             }
  40.  
  41.             if (this.cameraRect.xMax > rect.xMax - paddingRight)
  42.             {
  43.                 correction.x -= this.cameraRect.xMax - (rect.xMax - paddingRight);
  44.             }
  45.  
  46.             if (this.cameraRect.yMin < rect.yMin + paddingBottom)
  47.             {
  48.                 correction.y -= this.cameraRect.yMin - (rect.yMin + paddingBottom);
  49.             }
  50.  
  51.             if (this.cameraRect.yMax > rect.yMax - paddingTop)
  52.             {
  53.                 correction.y -= this.cameraRect.yMax - (rect.yMax - paddingTop);
  54.             }
  55.  
  56.             this.cameraComponent.transform.position += correction;
  57.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement