Skip to content

Instantly share code, notes, and snippets.

@codephi
Created June 24, 2016 23:20
Show Gist options
  • Save codephi/ff03cfb30135633c43ab588350d64d7c to your computer and use it in GitHub Desktop.
Save codephi/ff03cfb30135633c43ab588350d64d7c to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public static class SpritePivotAlignment
{
public static SpriteAlignment GetSpriteAlignment(GameObject SpriteObject)
{
BoxCollider2D MyBoxCollider = SpriteObject.AddComponent<BoxCollider2D>();
float colX = MyBoxCollider.offset.x;
float colY = MyBoxCollider.offset.y;
if (colX > 0f && colY < 0f)
return (SpriteAlignment.TopLeft);
else if (colX < 0 && colY < 0)
return (SpriteAlignment.TopRight);
else if (colX == 0 && colY < 0)
return (SpriteAlignment.TopCenter);
else if (colX > 0 && colY == 0)
return (SpriteAlignment.LeftCenter);
else if (colX < 0 && colY == 0)
return (SpriteAlignment.RightCenter);
else if (colX > 0 && colY > 0)
return (SpriteAlignment.BottomLeft);
else if (colX < 0 && colY > 0)
return (SpriteAlignment.BottomRight);
else if (colX == 0 && colY > 0)
return (SpriteAlignment.BottomCenter);
else if (colX == 0 && colY == 0)
return (SpriteAlignment.Center);
else
return (SpriteAlignment.Custom);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment