Game Development Reference
In-Depth Information
Now returning to the
CommandBar
script, we can add the following variables to the top
of the script to track the desired anchor point on the screen using this new
enum
:
public bool anchor = true;
public Vector2 anchorOffset = Vector2.zero;
public ScreenPositionAnchorPoint anchorPoint =
ScreenPositionAnchorPoint.BottomCenter;
Then, to work out where exactly on the screen the command bar should be based on this
anchor point, we just need a simple helper function. So, add the following to the bottom of
the
CommandBar
script:
Vector2 CalculateAnchorScreenPosition()
{
Vector2 position = Vector2.zero;
switch (anchorPoint)
{
case ScreenPositionAnchorPoint.BottomLeft:
position.y = -(ScreenHeight / 2) + Height;
position.x = -(ScreenWidth / 2) + buttonSize;
break;
case ScreenPositionAnchorPoint.BottomCenter:
position.y = -(ScreenHeight / 2) + Height;
position.x = -(Width / 2);
break;
case ScreenPositionAnchorPoint.BottomRight:
position.y = -(ScreenHeight / 2) + Height;
position.x = (ScreenWidth / 2) - Width;
break;
case ScreenPositionAnchorPoint.MiddleLeft:
position.y = (Height / 2);
position.x = -(ScreenWidth / 2) + buttonSize;
break;
case ScreenPositionAnchorPoint.MiddleCenter: