This might not be the optimal answer, but... I guess it really just depends on your project, and how "messy" you want things to be. GUI Helper classes are always a plus, for instance, I just wrote this for my game, because I had found I would always want to anchor some GUI element to a spot on the screen easily:
using UnityEngine;
using System.Collections;
public enum AnchorType
{
TopLeft,
TopCenter,
TopRight,
MiddleLeft,
MiddleCenter,
MiddleRight,
BottomLeft,
BottomCenter,
BottomRight
}
public static class GUIAnchor
{
public static Rect PositionFixed(Vector2 size, Vector2 offset, AnchorType type)
{
return PositionFixed(size.x, size.y, offset.x, offset.y, type);
}
public static Rect PositionFixed(float x, float y, Vector2 offset, AnchorType type)
{
return PositionFixed(x, y, offset.x, offset.y, type);
}
public static Rect PositionFixed(Vector2 size, float xOffset, float yOffset, AnchorType type)
{
return PositionFixed(size.x, size.y, xOffset, yOffset, type);
}
public static Rect PositionFixed(float x, float y, float xOffset, float yOffset, AnchorType type)
{
float top = 0;
float left = 0;
switch (type)
{
case AnchorType.BottomCenter:
case AnchorType.MiddleCenter:
case AnchorType.TopCenter:
left = Screen.width / 2 - x / 2;
break;
case AnchorType.BottomRight:
case AnchorType.MiddleRight:
case AnchorType.TopRight:
left = Screen.width - x - xOffset;
break;
case AnchorType.BottomLeft:
case AnchorType.MiddleLeft:
case AnchorType.TopLeft:
left = xOffset;
break;
}
switch (type)
{
case AnchorType.TopRight:
case AnchorType.TopLeft:
case AnchorType.TopCenter:
top = yOffset;
break;
case AnchorType.MiddleRight:
case AnchorType.MiddleLeft:
case AnchorType.MiddleCenter:
top = Screen.height / 2 - y / 2;
break;
case AnchorType.BottomRight:
case AnchorType.BottomLeft:
case AnchorType.BottomCenter:
top = Screen.height - y - yOffset;
break;
}
return new Rect(left, top, x, y);
}
public static Rect PositionScaled(Vector2 size, Vector2 offset, AnchorType type)
{
return PositionScaled(size.x, size.y, offset.x, offset.y, type);
}
public static Rect PositionScaled(float x, float y, Vector2 offset, AnchorType type)
{
return PositionScaled(x, y, offset.x, offset.y, type);
}
public static Rect PositionScaled(Vector2 size, float xOffset, float yOffset, AnchorType type)
{
return PositionScaled(size.x, size.y, xOffset, yOffset, type);
}
public static Rect PositionScaled(float x, float y, float xOffset, float yOffset, AnchorType type)
{
if (x < 1)
{
x /= 100;
}
if (y < 1)
{
y /= 100;
}
x = Screen.width * x;
y = Screen.height * y;
return PositionFixed(x, y, xOffset, yOffset, type);
}
}
It's not great, it doesn't do much, but it's an example of some GUI code that can be wrapped up statically in another class, making your other code less sloppy.
Anyone else have any other examples like this? And perhaps someone could give me some feedback on this class, perhaps things that could be added to it, or potential bugs.