Skip to content

Instantly share code, notes, and snippets.

Created January 26, 2018 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/220c2b9977d4fc3418abf00f6f5d0f8c to your computer and use it in GitHub Desktop.
Save anonymous/220c2b9977d4fc3418abf00f6f5d0f8c to your computer and use it in GitHub Desktop.
using UnityEngine;
using PaintCraft.Controllers;
using UnityEngine.Assertions;
using UnityEngine.UI;
[RequireComponent(typeof(RectTransform))]
public class RectToCanvasPosition : MonoBehaviour {
public ScreenCameraController ScreenCameraController;
RectTransform _rt;
private CanvasScaler _cs;
void Awake(){
_rt =GetComponent<RectTransform>();
_rt.hasChanged = true;
Assert.IsNotNull(_rt);
_cs = GetComponentInParent<CanvasScaler>();
}
void Update () {
if (_rt.hasChanged)
{
_rt.hasChanged = false;
Vector3[] corners = GetScreenRect(_rt);
ScreenCameraController.CameraSize.ViewPortOffset.left = (int)corners[0].x;
ScreenCameraController.CameraSize.ViewPortOffset.bottom = (int)corners[0].y;
ScreenCameraController.CameraSize.ViewPortOffset.right = Screen.width - (int)corners[2].x;
ScreenCameraController.CameraSize.ViewPortOffset.top = Screen.height - (int)corners[2].y;
ScreenCameraController.CameraSize.Init(ScreenCameraController.Camera, ScreenCameraController.Canvas);
ScreenCameraController.CameraSize.ResetSize();
}
}
public Vector3[] GetScreenRect(RectTransform rectTransform)
{
Vector3[] corners = new Vector3[4];
rectTransform.GetWorldCorners(corners);
if (_cs != null)
{
for (int j = 0; j < 4; j++)
{
corners[j] = corners[j] * _cs.scaleFactor;
}
}
return corners;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment