Setting sprite pivot through editor script

I’m setting sprite pivots in an editor script with the following code:

TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
textureImporter.spritePivot = new Vector2(1f, 0f);
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);

However, the custom pivot doesn’t seem to get set in the sprite until I actually select the sprite in the editor and set the pivot mode to custom. After the pivot mode is set to custom, changes I make through code reflects on the sprite successfully.

Is there some thing else I need to set to make a sprite’s pivot to custom?

Try this:

TextureImporterSettings texSettings = new TextureImporterSettings();

texSettings.spriteAlignment = (int)SpriteAlignment.Custom;

And then set your custom pivot.

WARNING Never forget about ReadTextureSettings() if you don’t want to mess things up :slight_smile:

Actually, you can also change other texture-related stuff via TextureImporterSettings, so maybe it would be better/cleaner to configure everything that way.

Did you try AssetDatabase.WriteImportSettingsIfDirty(path)