I know, maybe is a noob question but i can’t find a solution. I found Sprite Alignment but know how to use it. Thanks.
Ok. Here we go. before i give you the solution i want to explain it.
***BoxCollider2D has a property named Center so you can add a BoxCollider2D component to the game object and check his X and Y axis. for example a Centered Pivot will have his BoxCollider2D center at (0,0). So i ll copy the class i wrote hopping you understand it. it problably can be improved. Sorry for my english.
using UnityEngine;
using System.Collections;
public class SpritePivotAlignment {
public SpriteAlignment GetSpriteAlignment(GameObject SpriteObject){
BoxCollider2D MyBoxCollider= SpriteObject.AddComponent<BoxCollider2D> ();
float colX = MyBoxCollider.center.x;
float colY = MyBoxCollider.center.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);
}
}
****How to implement:
SpritePivotAlignment PivotCheck = new SpritePivotAlignment ();
if (PivotCheck.GetSpriteAlignment (MyGameObj)==SpriteAlignment.Center) {
//DO SOMETHING
}
Hope you find this usefull!!! Regards from Argentina
I think it should be Sprite.rect.center normalized in rect coordinates. But I am not sure.