You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

104 lines
3.1 KiB

using System;
using UnityEngine;
using UnityEngine.Serialization;
[ExecuteInEditMode]
public class Dimensions : MonoBehaviour {
public bool isUpdating;
[FormerlySerializedAs("panelHeightPixels")]
[Tooltip("Player panels height")]
public float panelHeightPixelsMinimum;
[Tooltip("Size in Unity units")]
public Vector2 playGroundSize;
[Tooltip("Height of the empty space between board and death zone")]
public float emptySpaceHeight;
public Camera mainCamera;
public EdgeCollider2D topC, bottomC, leftC, rightC;
public SpriteRenderer playGround;
public SpriteRenderer playerPanelTop;
public SpriteRenderer playerPanelBottom;
public SpriteRenderer background;
public static Dimensions Singleton;
private void Awake() {
Singleton = this;
GetComponents();
SetDimensions();
if (Application.isPlaying)
isUpdating = false;
}
private void GetComponents() {
topL = topC.GetComponent<LineRenderer>();
bottomL = bottomC.GetComponent<LineRenderer>();
}
public Vector2 PlaySize { get; private set; }
public Vector2 PlaySizeBoards { get; private set; }
public float PanelHeightPixels { get; private set; }
private LineRenderer topL, bottomL;
private void SetDimensions() {
float heightByPanels = 1 / (1 - 2 * panelHeightPixelsMinimum / mainCamera.pixelHeight) * playGroundSize.y;
float heightByPlayground = playGroundSize.x / mainCamera.aspect;
float height = Mathf.Max(heightByPanels, heightByPlayground);
float width = mainCamera.aspect * height;
float panelHeight = (height - playGroundSize.y) / 2;
mainCamera.orthographicSize = height / 2;
float top = playGroundSize.y / 2;
float right = playGroundSize.x / 2;
PanelHeightPixels = panelHeight / height * mainCamera.pixelHeight;
PlaySize = playGroundSize;
PlaySizeBoards = playGroundSize - new Vector2(0, emptySpaceHeight * 2);
Vector2 v1 = new Vector3(-right, 0);
Vector2 v2 = new Vector3(right, 0);
Vector2 v3 = new Vector3(0, top);
Vector2 v4 = new Vector3(0, -top);
topC.transform.position = new Vector2(0, top);
bottomC.transform.position = new Vector2(0, -top);
leftC.transform.position = new Vector2(-right, 0);
rightC.transform.position = new Vector2(right, 0);
topC.points = new[] {v1, v2};
bottomC.points = new[] {v1, v2};
leftC.points = new[] {v3, v4};
rightC.points = new[] {v3, v4};
playGround.transform.rotation = Quaternion.Euler(0, 0, 90);
playGround.size = new Vector2(playGroundSize.y, playGroundSize.x);
const float lineWidth = 0.5f;
Vector3 offset = Vector3.up * lineWidth / 2;
topL.positionCount = bottomL.positionCount = 2;
var v12 = new[] {(Vector3) v1 - offset, (Vector3) v2 - offset};
topL.SetPositions(v12);
v12 = new[] {(Vector3) v1 + offset, (Vector3) v2 + offset};
bottomL.SetPositions(v12);
playerPanelTop.transform.position = new Vector2(0, (height + PlaySize.y) / 4);
playerPanelBottom.transform.position = new Vector2(0, -(height + PlaySize.y) / 4);
playerPanelTop.size = playerPanelBottom.size = new Vector2(width, panelHeight);
background.size = new Vector2(width, playGroundSize.y);
}
private void Update() {
if (!isUpdating)
return;
SetDimensions();
}
}