HeavenStudio/Assets/GUIWindows/Scripts/GUIWindow.cs
minenice55 1c6eae8005
Resource Optimization (#812)
* right foot creep

* look around stay low

* optimize the fan club prefab

use anim layers for the monkeys

* dansa med oss

* klappa era hander

* cossack sandvich

* soldier of dance

add speed halving / doubling functionality to choreographies

* fix selection of starting dance

* asset re-organization

* organize the last of the 1.0 games

* Wall

* floating windows can no longer become larger than the actual screen
2024-03-30 02:52:14 +00:00

68 lines
2.3 KiB
C#

using UnityEngine;
namespace Rellac.Windows
{
/// <summary>
/// Simple script to destroy the target GameObject when window is closed
/// </summary>
public class GUIWindow : MonoBehaviour
{
[SerializeField] private float maxWidth = 0;
[SerializeField] private float maxHeight = 0;
/// <summary>
/// Close window by destroying this GameObject
/// </summary>
public void CloseWindow()
{
Destroy(gameObject);
}
private void Update()
{
// limit window size
float finalMaxWidth = Mathf.Min(maxWidth, Screen.width);
float finalMaxHeight = Mathf.Min(maxHeight, Screen.height);
RectTransform rectTransform = GetComponent<RectTransform>();
if (maxWidth > 0 && rectTransform.rect.width > finalMaxWidth)
{
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, finalMaxWidth);
}
if (maxHeight > 0 && rectTransform.rect.height > finalMaxHeight)
{
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, finalMaxHeight);
}
// keep in bounds of parent
RectTransform parent = transform.parent.GetComponent<RectTransform>();
if (parent != null)
{
Vector3[] corners = new Vector3[4];
parent.GetWorldCorners(corners);
Vector3 min = corners[0];
Vector3 max = corners[2];
Vector3[] myCorners = new Vector3[4];
rectTransform.GetWorldCorners(myCorners);
if (myCorners[0].x < min.x)
{
rectTransform.localPosition += new Vector3(min.x - myCorners[0].x, 0, 0);
}
if (myCorners[2].x > max.x)
{
rectTransform.localPosition -= new Vector3(myCorners[2].x - max.x, 0, 0);
}
if (myCorners[0].y < min.y)
{
rectTransform.localPosition += new Vector3(0, min.y - myCorners[0].y, 0);
}
if (myCorners[2].y > max.y)
{
rectTransform.localPosition -= new Vector3(0, myCorners[2].y - max.y, 0);
}
}
}
}
}