using System.Collections.Generic;
using UnityEngine;
namespace GameKit.Utilities
{
public static class Transforms
{
///
/// Returns a position for the rectTransform ensuring it's fully on the screen.
///
/// Preferred position for the rectTransform.
/// How much padding the transform must be from the screen edges.
public static Vector3 GetOnScreenPosition(this RectTransform rectTransform, Vector3 desiredPosition, Vector2 padding)
{
Vector2 scale = new Vector2(rectTransform.localScale.x, rectTransform.localScale.y);
//Value of which the tooltip would exceed screen bounds.
//If there would be overshoot then adjust to be just on the edge of the overshooting side.
float overshoot;
float halfWidthRequired = ((rectTransform.sizeDelta.x * scale.x) / 2f) + padding.x;
overshoot = (Screen.width - (desiredPosition.x + halfWidthRequired));
//If overshooting on the right.
if (overshoot < 0f)
desiredPosition.x += overshoot;
overshoot = (desiredPosition.x - halfWidthRequired);
//If overshooting on the left.
if (overshoot < 0f)
desiredPosition.x = halfWidthRequired;
float halfHeightRequired = ((rectTransform.sizeDelta.y * scale.y) / 2f) + padding.y;
overshoot = (Screen.height - (desiredPosition.y + halfHeightRequired));
//If overshooting on the right.
if (overshoot < 0f)
desiredPosition.y += overshoot;
overshoot = (desiredPosition.y - halfHeightRequired);
//If overshooting on the left.
if (overshoot < 0f)
desiredPosition.y = halfHeightRequired;
return desiredPosition;
}
///
/// Sets a parent for src while maintaining position, rotation, and scale of src.
///
/// Transform to become a child of.
public static void SetParentAndKeepTransform(this Transform src, Transform parent)
{
Vector3 pos = src.position;
Quaternion rot = src.rotation;
Vector3 scale = src.localScale;
src.SetParent(parent);
src.position = pos;
src.rotation = rot;
src.localScale = scale;
}
///
/// Destroys all children under the specified transform.
///
///
public static void DestroyChildren(this Transform t, bool destroyImmediately = false)
{
foreach (Transform child in t)
{
if (destroyImmediately)
MonoBehaviour.DestroyImmediate(child.gameObject);
else
MonoBehaviour.Destroy(child.gameObject);
}
}
///
/// Destroys all children of a type under the specified transform.
///
///
public static void DestroyChildren(this Transform t, bool destroyImmediately = false) where T : MonoBehaviour
{
T[] children = t.GetComponentsInChildren();
foreach (T child in children)
{
if (destroyImmediately)
MonoBehaviour.DestroyImmediate(child.gameObject);
else
MonoBehaviour.Destroy(child.gameObject);
}
}
///
/// Gets components in children and optionally parent.
///
///
///
///
///
///
public static void GetComponentsInChildren(this Transform parent, List results, bool includeParent = true, bool includeInactive = false) where T : Component
{
if (!includeParent)
{
List current = GameKit.Utilities.CollectionCaches.RetrieveList();
for (int i = 0; i < parent.childCount; i++)
{
parent.GetChild(i).GetComponentsInChildren(includeInactive, current);
results.AddRange(current);
}
GameKit.Utilities.CollectionCaches.Store(current);
}
else
{
parent.GetComponentsInChildren(includeInactive, results);
}
}
///
/// Returns the position of this transform.
///
public static Vector3 GetPosition(this Transform t, bool localSpace)
{
return (localSpace) ? t.localPosition : t.position;
}
///
/// Returns the rotation of this transform.
///
public static Quaternion GetRotation(this Transform t, bool localSpace)
{
return (localSpace) ? t.localRotation : t.rotation;
}
///
/// Returns the scale of this transform.
///
public static Vector3 GetScale(this Transform t)
{
return t.localScale;
}
///
/// Sets the position of this transform.
///
///
///
public static void SetPosition(this Transform t, bool localSpace, Vector3 pos)
{
if (localSpace)
t.localPosition = pos;
else
t.position = pos;
}
///
/// Sets the position of this transform.
///
///
///
public static void SetRotation(this Transform t, bool localSpace, Quaternion rot)
{
if (localSpace)
t.localRotation = rot;
else
t.rotation = rot;
}
///
/// Sets the position of this transform.
///
///
///
public static void SetScale(this Transform t, Vector3 scale)
{
t.localScale = scale;
}
}
}