Skip to content

Commit

Permalink
cleanup and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Reddevildragg committed Jul 20, 2024
1 parent 3242bac commit 3aee50d
Show file tree
Hide file tree
Showing 19 changed files with 1,046 additions and 340 deletions.
20 changes: 18 additions & 2 deletions Packages/Unity_Extensions/Scripts/AspectRatio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@

namespace GG.Extensions
{
/// <summary>
/// Provides utility methods for calculating aspect ratios.
/// </summary>
public static class AspectRatio
{
/// <summary>
/// Calculates the aspect ratio from given width and height integers.
/// </summary>
/// <param name="x">The width component of the resolution.</param>
/// <param name="y">The height component of the resolution.</param>
/// <param name="debug">If true, logs the calculated aspect ratio to the console.</param>
/// <returns>A Vector2 representing the aspect ratio (x:y).</returns>
public static Vector2 GetAspectRatio(int x, int y, bool debug = false)
{
return GetAspectRatio(new Vector2(x, y), debug);
}

/// <summary>
/// Calculates the aspect ratio from a Vector2 representing width and height.
/// </summary>
/// <param name="xy">A Vector2 where x is width and y is height.</param>
/// <param name="debug">If true, logs the calculated aspect ratio to the console.</param>
/// <returns>A Vector2 representing the aspect ratio (x:y).</returns>
public static Vector2 GetAspectRatio(Vector2 xy, bool debug = false)
{
float f = xy.x / xy.y;
Expand All @@ -22,8 +38,8 @@ public static Vector2 GetAspectRatio(Vector2 xy, bool debug = false)
}

if (debug)
Debug.Log("Aspect ratio is " + f * i + ":" + i + " (Resolution: " + xy.x + "x" + xy.y + ")");
Debug.Log($"Aspect ratio is {f * i}:{i} (Resolution: {xy.x}x{xy.y})");
return new Vector2((float)System.Math.Round(f * i, 2), i);
}
}
}
}
123 changes: 106 additions & 17 deletions Packages/Unity_Extensions/Scripts/CameraExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,149 @@ namespace GG.Extensions
{
public static class CameraExtensions
{
public static void LayerCullingShow(this Camera cam, int layerMask) {
/// <summary>
/// Shows layers specified by a bitmask on the camera's culling mask.
/// </summary>
/// <param name="cam">The camera to modify.</param>
/// <param name="layerMask">The layer mask to show.</param>
public static void LayerCullingShow(this Camera cam, int layerMask)
{
cam.cullingMask |= layerMask;
}

public static void LayerCullingShow(this Camera cam, string layer) {
/// <summary>
/// Shows a single layer specified by name on the camera's culling mask.
/// </summary>
/// <param name="cam">The camera to modify.</param>
/// <param name="layer">The name of the layer to show.</param>
public static void LayerCullingShow(this Camera cam, string layer)
{
LayerCullingShow(cam, 1 << LayerMask.NameToLayer(layer));
}


/// <summary>
/// Shows multiple layers specified by names on the camera's culling mask.
/// </summary>
/// <param name="camera">The camera to modify.</param>
/// <param name="layerNames">The names of the layers to show.</param>
public static void LayerCullingShow(this Camera camera, params string[] layerNames)
{
foreach (string layerName in layerNames)
{
LayerCullingShow(camera,layerName);
LayerCullingShow(camera, layerName);
}
}

public static void LayerCullingHide(this Camera cam, int layerMask) {
/// <summary>
/// Hides layers specified by a bitmask from the camera's culling mask.
/// </summary>
/// <param name="cam">The camera to modify.</param>
/// <param name="layerMask">The layer mask to hide.</param>
public static void LayerCullingHide(this Camera cam, int layerMask)
{
cam.cullingMask &= ~layerMask;
}

public static void LayerCullingHide(this Camera cam, string layer) {
/// <summary>
/// Hides a single layer specified by name from the camera's culling mask.
/// </summary>
/// <param name="cam">The camera to modify.</param>
/// <param name="layer">The name of the layer to hide.</param>
public static void LayerCullingHide(this Camera cam, string layer)
{
LayerCullingHide(cam, 1 << LayerMask.NameToLayer(layer));
}


/// <summary>
/// Hides multiple layers specified by names from the camera's culling mask.
/// </summary>
/// <param name="camera">The camera to modify.</param>
/// <param name="layerNames">The names of the layers to hide.</param>
public static void LayerCullingHide(this Camera camera, params string[] layerNames)
{
foreach (string layerName in layerNames)
{
LayerCullingHide(camera,layerName);
LayerCullingHide(camera, layerName);
}
}

public static void LayerCullingToggle(this Camera cam, int layerMask) {
/// <summary>
/// Toggles the visibility of layers specified by a bitmask on the camera's culling mask.
/// </summary>
/// <param name="cam">The camera to modify.</param>
/// <param name="layerMask">The layer mask to toggle.</param>
public static void LayerCullingToggle(this Camera cam, int layerMask)
{
cam.cullingMask ^= layerMask;
}

public static void LayerCullingToggle(this Camera cam, string layer) {
/// <summary>
/// Toggles the visibility of a single layer specified by name on the camera's culling mask.
/// </summary>
/// <param name="cam">The camera to modify.</param>
/// <param name="layer">The name of the layer to toggle.</param>
public static void LayerCullingToggle(this Camera cam, string layer)
{
LayerCullingToggle(cam, 1 << LayerMask.NameToLayer(layer));
}

public static bool LayerCullingIncludes(this Camera cam, int layerMask) {
/// <summary>
/// Checks if the camera's culling mask includes layers specified by a bitmask.
/// </summary>
/// <param name="cam">The camera to check.</param>
/// <param name="layerMask">The layer mask to check for inclusion.</param>
/// <returns>True if the layer mask is included in the camera's culling mask; otherwise, false.</returns>
public static bool LayerCullingIncludes(this Camera cam, int layerMask)
{
return (cam.cullingMask & layerMask) > 0;
}

public static bool LayerCullingIncludes(this Camera cam, string layer) {
/// <summary>
/// Checks if the camera's culling mask includes a single layer specified by name.
/// </summary>
/// <param name="cam">The camera to check.</param>
/// <param name="layer">The name of the layer to check for inclusion.</param>
/// <returns>True if the layer is included in the camera's culling mask; otherwise, false.</returns>
public static bool LayerCullingIncludes(this Camera cam, string layer)
{
return LayerCullingIncludes(cam, 1 << LayerMask.NameToLayer(layer));
}

public static void LayerCullingToggle(this Camera cam, int layerMask, bool isOn) {
/// <summary>
/// Toggles the visibility of layers specified by a bitmask on the camera's culling mask, with an option to force visibility on or off.
/// </summary>
/// <param name="cam">The camera to modify.</param>
/// <param name="layerMask">The layer mask to toggle.</param>
/// <param name="isOn">If true, forces the layer(s) to be visible; if false, forces the layer(s) to be hidden.</param>
public static void LayerCullingToggle(this Camera cam, int layerMask, bool isOn)
{
bool included = LayerCullingIncludes(cam, layerMask);
if (isOn && !included) {
if (isOn && !included)
{
LayerCullingShow(cam, layerMask);
} else if (!isOn && included) {
}
else if (!isOn && included)
{
LayerCullingHide(cam, layerMask);
}
}

public static void LayerCullingToggle(this Camera cam, string layer, bool isOn) {
/// <summary>
/// Toggles the visibility of a single layer specified by name on the camera's culling mask, with an option to force visibility on or off.
/// </summary>
/// <param name="cam">The camera to modify.</param>
/// <param name="layer">The name of the layer to toggle.</param>
/// <param name="isOn">If true, forces the layer to be visible; if false, forces the layer to be hidden.</param>
public static void LayerCullingToggle(this Camera cam, string layer, bool isOn)
{
LayerCullingToggle(cam, 1 << LayerMask.NameToLayer(layer), isOn);
}

/// <summary>
/// Sets the camera's culling mask to show only the specified layers.
/// </summary>
/// <param name="cam">The camera to modify.</param>
/// <param name="layers">A list of layer names to be made visible.</param>
public static void SetCullingMask(this Camera cam, List<string> layers)
{
cam.cullingMask = 0;
Expand All @@ -74,7 +156,14 @@ public static void SetCullingMask(this Camera cam, List<string> layers)
cam.LayerCullingShow(layer);
}
}


/// <summary>
/// Sets the camera's culling mask to show only the specified layer.
/// This method resets the camera's culling mask before showing the specified layer,
/// effectively making only the specified layer visible.
/// </summary>
/// <param name="cam">The camera to modify.</param>
/// <param name="layer">The name of the layer to be made visible.</param>
public static void SetCullingMask(this Camera cam, string layer)
{
cam.cullingMask = 0;
Expand Down
14 changes: 13 additions & 1 deletion Packages/Unity_Extensions/Scripts/ColorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ namespace GG.Extensions
{
public static class ColorExtensions
{
// Note that Color32 and Color implictly convert to each other. You may pass a Color object to this method without first casting it.
/// <summary>
/// Converts a Color32 to a hexadecimal string representation.
/// </summary>
/// <param name="color">The Color32 to convert.</param>
/// <returns>A hexadecimal string representing the color.</returns>
public static string ColorToHex(this Color32 color)
{
string hex = color.r.ToString("X2") + color.g.ToString("X2") + color.b.ToString("X2");
return hex;
}

/// <summary>
/// Converts a hexadecimal string to a Color.
/// </summary>
/// <param name="hex">The hexadecimal string to convert. Can be prefixed with "0x" or "#".</param>
/// <returns>A Color represented by the hexadecimal string.</returns>
/// <remarks>
/// Assumes the color is fully opaque unless an alpha value is specified in the hex string.
/// </remarks>
public static Color HexToColor(string hex)
{
hex = hex.Replace("0x", ""); //in case the string is formatted 0xFFFFFF
Expand Down
6 changes: 4 additions & 2 deletions Packages/Unity_Extensions/Scripts/Colors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ namespace GG.Extensions
{
public class Colors
{
// NOTE: The follwing color names come from the CSS3 specification, Section 4.3 Extended Color Keywords
// http://www.w3.org/TR/css3-color/#svg-color
/// <summary>
/// Generates a random color with each RGB component ranging from 0 to 1.
/// </summary>
/// <returns>A new Color instance with random RGB values.</returns>
public static Color Random
{
get
Expand Down
Loading

0 comments on commit 3aee50d

Please sign in to comment.