Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- public class RelativeRect {
- float _x;
- float _y;
- float _width;
- float _height;
- public enum Pin {
- TopLeft = 0,
- TopRight = 1,
- BottomLeft = 2,
- BottomRight = 3
- }
- public float X {
- get {
- return _x;
- }
- set {
- _x = value;
- }
- }
- public float Y {
- get {
- return _y;
- }
- set {
- _y = value;
- }
- }
- public float Width {
- get {
- return _width;
- }
- set {
- _width = value;
- }
- }
- public float Height {
- get {
- return _height;
- }
- set {
- _height = value;
- }
- }
- public Rect ScreenCoordinates {
- get {
- return GetExact(_x, _y, _width, _height, false);
- }
- }
- public RelativeRect(float x, float y, float width, float height, bool LockRatio = false) {
- if (!LockRatio) {
- _x = x;
- _y = y;
- _width = width;
- _height = height;
- } else {
- Rect rect;
- rect = new Rect((x / 100) * Screen.width,
- (y / 100) * Screen.height,
- (width / 100) * Screen.width,
- (width / height) * ((width / 100) * Screen.width));
- _x = rect.x;
- _y = rect.y;
- _width = rect.width;
- _height = rect.height;
- }
- }
- public RelativeRect(Rect rect, bool LockRatio = false) {
- if (!LockRatio) {
- _x = rect.x;
- _y = rect.y;
- _width = rect.width;
- _height = rect.height;
- } else {
- rect = new Rect((rect.x / 100) * Screen.width,
- (rect.y / 100) * Screen.height,
- (rect.width / 100) * Screen.width,
- (rect.width / rect.height) * ((rect.width / 100) * Screen.width));
- _x = rect.x;
- _y = rect.y;
- _width = rect.width;
- _height = rect.height;
- }
- }
- public static Rect GetExact(float x, float y, float width, float height, bool LockRatio = false, float Offset = 0, bool correctiveMovement = false) {
- Rect rect;
- if (!LockRatio) {
- rect = new Rect((x / 100) * Screen.width,
- (y / 100) * Screen.height,
- (width / 100) * Screen.width,
- (height / 100) * Screen.height);
- } else {
- rect = new Rect((x / 100) * Screen.width,
- (y / 100) * Screen.height,
- (width / 100) * Screen.width,
- (width / height) * ((width / 100) * Screen.width));
- if (correctiveMovement) {
- if (rect.y + rect.height + Offset > Screen.height) {
- rect.y -= ((rect.y + rect.height + Offset) - Screen.height);
- }
- }
- }
- return rect;
- }
- public static Rect GetExact(Rect position, bool LockRatio = false, float Offset = 0, bool correctiveMovement = false) {
- Rect rect;
- if (!LockRatio) {
- rect = new Rect((position.x / 100) * Screen.width,
- (position.y / 100) * Screen.height,
- (position.width / 100) * Screen.width,
- (position.height / 100) * Screen.height);
- } else {
- rect = new Rect((position.x / 100) * Screen.width,
- (position.y / 100) * Screen.height,
- (position.width / 100) * Screen.width,
- (position.width / position.height) * ((position.width / 100) * Screen.width));
- if (correctiveMovement) {
- if (rect.y + rect.height + Offset > Screen.height) {
- rect.y -= ((rect.y + rect.height + Offset) - Screen.height);
- }
- }
- }
- return rect;
- }
- public static Rect GetExact(Rect position, Pin sideToPin, bool LockRatio = false) {
- Rect rect;
- if (!LockRatio) {
- rect = new Rect((position.x / 100) * Screen.width,
- (position.y / 100) * Screen.height,
- (position.width / 100) * Screen.width,
- (position.height / 100) * Screen.height);
- } else {
- rect = new Rect((position.x / 100) * Screen.width,
- (position.y / 100) * Screen.height,
- (position.width / 100) * Screen.width,
- (position.width / position.height) * ((position.width / 100) * Screen.width));
- }
- switch (sideToPin) {
- case Pin.TopLeft:
- rect.y = 0;
- rect.x = 0;
- break;
- case Pin.TopRight:
- rect.y = 0;
- rect.x = 0;
- break;
- case Pin.BottomLeft:
- rect.y = Screen.height - rect.height;
- rect.x = 0;
- break;
- case Pin.BottomRight:
- rect.y = Screen.height - rect.height;
- rect.x = Screen.width - rect.width;
- break;
- }
- return rect;
- }
- public static float GetExactWidth(float relativeWidth) {
- return GetExact(0, 0, relativeWidth, 0, false, 0, false).width;
- }
- public static float GetExactHeight(float relativeHeight) {
- return GetExact(0, 0, 0, relativeHeight, false, 0, false).height;
- }
- public static Rect GetExact(Rect rect) {
- rect = new Rect((rect.x / 100) * Screen.width,
- (rect.y / 100) * Screen.height,
- (rect.width / 100) * Screen.width,
- (rect.height / 100) * Screen.height);
- return rect;
- }
- public static float GetRelativeText(float size) {
- return (size / 100f) * Screen.width;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment