private static void SetPivotCenterOfMass() { Texture2D texture = Selection.activeObject as Texture2D; string path = AssetDatabase.GetAssetPath(texture); TextureImporter tempImporter = (TextureImporter)AssetImporter.GetAtPath(path); SpriteMetaData[] meta = new SpriteMetaData[tempImporter.spritesheet.Length]; texture.SetReadable(true); for (int i = 0; i < meta.Length; i++) { meta[i].name = i.ToString(); meta[i].alignment = (int)SpriteAlignment.Custom; Vector2 centerOfMass = texture.CenterOfMass(tempImporter.spritesheet[i].rect); meta[i].pivot = new Vector2(centerOfMass.x / tempImporter.spritesheet[i].rect.width, centerOfMass.y / tempImporter.spritesheet[i].rect.height); meta[i].rect = tempImporter.spritesheet[i].rect; } tempImporter.spritesheet = meta; tempImporter.textureType = TextureImporterType.Sprite; AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(texture)); AssetDatabase.Refresh(); }
public static Vector2 CenterOfMass(this Texture2D texture) { return(texture.CenterOfMass(new Rect(0, 0, texture.width, texture.height))); }