Skip to content

Commit

Permalink
rectTransform Extensions #84
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinkievicz committed Jan 18, 2022
1 parent 8108b04 commit 0c6682d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Runtime/Utilities/RectTransformExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Linq;
using UnityEngine;

namespace StansAssets.Foundation
{
/// <summary>
/// Rect Transform Utility methods.
/// </summary>
public static class RectTransformExtensions
//
{
/// <summary>
/// Method to get Rect related to ScreenSpace, from given RectTransform.
/// This will give the real position of this Rect on screen.
/// </summary>
/// <param name="transform">Original RectTransform of some object</param>
/// <returns>New Rect instance.</returns>
public static Rect RectTransformToScreenSpace(RectTransform transform)
{
Vector2 size = Vector2.Scale(transform.rect.size, transform.lossyScale);
Rect rect = new Rect(transform.position.x, Screen.height - transform.position.y, size.x, size.y);
rect.x -= (transform.pivot.x * size.x);
rect.y -= ((1.0f - transform.pivot.y) * size.y);
return rect;
}
public static void SetLeft(this RectTransform rt, float left)
{
rt.offsetMin = new Vector2(left, rt.offsetMin.y);
}

public static void SetRight(this RectTransform rt, float right)
{
rt.offsetMax = new Vector2(-right, rt.offsetMax.y);
}

public static void SetTop(this RectTransform rt, float top)
{
rt.offsetMax = new Vector2(rt.offsetMax.x, -top);
}

public static void SetBottom(this RectTransform rt, float bottom)
{
rt.offsetMin = new Vector2(rt.offsetMin.x, bottom);
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Utilities/RectTransformExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0c6682d

Please sign in to comment.