UIViewport.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [RequireComponent(typeof(Camera))]
  5. [AddComponentMenu("NGUI/UI/Viewport Camera")]
  6. public class UIViewport : MonoBehaviour
  7. {
  8. private void Start()
  9. {
  10. this.mCam = base.GetComponent<Camera>();
  11. if (this.sourceCamera == null)
  12. {
  13. this.sourceCamera = Camera.main;
  14. }
  15. }
  16. private void LateUpdate()
  17. {
  18. if (this.topLeft != null && this.bottomRight != null)
  19. {
  20. Vector3 vector = this.sourceCamera.WorldToScreenPoint(this.topLeft.position);
  21. Vector3 vector2 = this.sourceCamera.WorldToScreenPoint(this.bottomRight.position);
  22. Rect rect = new Rect(vector.x / (float)UICamera.ScreenWidth, vector2.y / (float)UICamera.ScreenHeight, (vector2.x - vector.x) / (float)UICamera.ScreenWidth, (vector.y - vector2.y) / (float)UICamera.ScreenHeight);
  23. float num = this.fullSize * rect.height;
  24. if (rect != this.mCam.rect)
  25. {
  26. this.mCam.rect = rect;
  27. }
  28. if (this.mCam.orthographicSize != num)
  29. {
  30. this.mCam.orthographicSize = num;
  31. }
  32. }
  33. }
  34. public Camera sourceCamera;
  35. public Transform topLeft;
  36. public Transform bottomRight;
  37. public float fullSize = 1f;
  38. private Camera mCam;
  39. }