Skip to content

Commit 1686186

Browse files
committed
feat: GameObjectExtensions, MaterialExtensions, RectTransformExtensions, RendererExtensions
1 parent 70ec91d commit 1686186

File tree

7 files changed

+172
-0
lines changed

7 files changed

+172
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using UnityEngine;
2+
3+
namespace StansAssets.Foundation.Extensions
4+
{
5+
/// <summary>
6+
/// Unity `GameObject` extension methods.
7+
/// </summary>
8+
public static class GameObjectExtensions
9+
{
10+
/// <summary>
11+
/// Set layer to all GameObject children, including inactive.
12+
/// </summary>
13+
/// <param name="gameObject">Target GameObject.</param>
14+
/// <param name="layerNumber">Layer number.</param>
15+
public static void SetLayerRecursively(this GameObject gameObject, int layerNumber)
16+
{
17+
foreach (var trans in gameObject.GetComponentsInChildren<Transform>(true))
18+
trans.gameObject.layer = layerNumber;
19+
}
20+
21+
/// <summary>
22+
/// Renderer Bounds of the game object.
23+
/// </summary>
24+
/// <param name="go">GameObject you want calculate bounds for.</param>
25+
/// <returns>Calculated game object bounds.</returns>
26+
public static Bounds GetRendererBounds(this GameObject go)
27+
{
28+
return CalculateBounds(go);
29+
}
30+
31+
static Bounds CalculateBounds(GameObject obj)
32+
{
33+
var hasBounds = false;
34+
var bounds = new Bounds(Vector3.zero, Vector3.zero);
35+
var childrenRenderer = obj.GetComponentsInChildren<Renderer>();
36+
37+
38+
var rnd = obj.GetComponent<Renderer>();
39+
if (rnd != null)
40+
{
41+
bounds = rnd.bounds;
42+
hasBounds = true;
43+
}
44+
45+
foreach (var child in childrenRenderer)
46+
if (!hasBounds)
47+
{
48+
bounds = child.bounds;
49+
hasBounds = true;
50+
}
51+
else
52+
{
53+
bounds.Encapsulate(child.bounds);
54+
}
55+
56+
return bounds;
57+
}
58+
}
59+
60+
}

Runtime/Extensions/UnityEngine/GameObjectExtensions.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using UnityEngine;
2+
3+
namespace StansAssets.Foundation.Extensions
4+
{
5+
/// <summary>
6+
/// Unity `Material` extension methods.
7+
/// </summary>
8+
public static class MaterialExtensions
9+
{
10+
/// <summary>
11+
/// Set's alpha channel for the Material `_Color` property
12+
/// </summary>
13+
/// <param name="material">Material to operate with.</param>
14+
/// <param name="value">Alpha channel value.</param>
15+
public static void SetAlpha(this Material material, float value)
16+
{
17+
if (material.HasProperty("_Color"))
18+
{
19+
var color = material.color;
20+
color.a = value;
21+
material.color = color;
22+
}
23+
}
24+
}
25+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using UnityEngine;
2+
3+
namespace StansAssets.Foundation.Extensions
4+
{
5+
/// <summary>
6+
/// Unity `RectTransform` extension methods.
7+
/// </summary>
8+
public static class RectTransformExtensions
9+
{
10+
/// <summary>
11+
/// Resets `anchorMin`, `anchorMax`, `offsetMin`, `offsetMax` to `Vector2.zero`.
12+
/// </summary>
13+
/// <param name="rectTransform">RectTransform to operate with.</param>
14+
public static void Reset(this RectTransform rectTransform)
15+
{
16+
rectTransform.anchorMin = Vector2.zero;
17+
rectTransform.anchorMax = Vector2.one;
18+
rectTransform.offsetMin = Vector2.zero;
19+
rectTransform.offsetMax = Vector2.zero;
20+
}
21+
22+
/// <summary>
23+
/// Get's the screen rect of provided RectTransform.
24+
/// </summary>
25+
/// <param name="rectTransform">RectTransform to operate with.</param>
26+
/// <returns>Screen rect.</returns>
27+
public static Rect GetScreenRect(this RectTransform rectTransform)
28+
{
29+
var rtCorners = new Vector3[4];
30+
rectTransform.GetWorldCorners(rtCorners);
31+
var rtRect = new Rect(new Vector2(rtCorners[0].x, rtCorners[0].y), new Vector2(rtCorners[3].x - rtCorners[0].x, rtCorners[1].y - rtCorners[0].y));
32+
33+
var canvas = rectTransform.GetComponentInParent<Canvas>();
34+
var canvasCorners = new Vector3[4];
35+
canvas.GetComponent<RectTransform>().GetWorldCorners(canvasCorners);
36+
var cRect = new Rect(new Vector2(canvasCorners[0].x, canvasCorners[0].y), new Vector2(canvasCorners[3].x - canvasCorners[0].x, canvasCorners[1].y - canvasCorners[0].y));
37+
38+
var screenWidth = Screen.width;
39+
var screenHeight = Screen.height;
40+
41+
var size = new Vector2(screenWidth / cRect.size.x * rtRect.size.x, screenHeight / cRect.size.y * rtRect.size.y);
42+
var rect = new Rect(screenWidth * ((rtRect.x - cRect.x) / cRect.size.x), screenHeight * ((-cRect.y + rtRect.y) / cRect.size.y), size.x, size.y);
43+
return rect;
44+
}
45+
}
46+
}

Runtime/Extensions/UnityEngine/RectTransformExtensions.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using UnityEngine;
2+
3+
namespace StansAssets.Foundation.Extensions
4+
{
5+
/// <summary>
6+
/// Unity `Renderer` extension methods.
7+
/// </summary>
8+
public static class RendererExtensions
9+
{
10+
/// <summary>
11+
/// Methods to check if renderer is visible from a certain point.
12+
/// </summary>
13+
/// <param name="renderer">Renderer to operate with.</param>
14+
/// <param name="camera">Camera instance tha will be used for calculation.</param>
15+
/// <returns></returns>
16+
public static bool IsVisibleFrom(this Renderer renderer, Camera camera)
17+
{
18+
var planes = GeometryUtility.CalculateFrustumPlanes(camera);
19+
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
20+
}
21+
22+
/// <summary>
23+
/// Renderer Bounds of the renderer. Method also take children in consideration.
24+
/// </summary>
25+
/// <param name="renderer">Renderer to operate with.</param>
26+
/// <returns>Calculated renderer bounds.</returns>
27+
public static Bounds GetRendererBounds(this Renderer renderer)
28+
{
29+
return renderer.gameObject.GetRendererBounds();
30+
}
31+
}
32+
}

Runtime/Extensions/UnityEngine/RendererExtensions.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)