this can be done with an Editor script and the AssetImporter:
- Start by creating a TextureImporter
// spritesheetPath with Assets folder as root
TextureImporter importer = AssetImporter.GetAtPath( spritesheetPath ) as TextureImporter;
- Get the SpriteMetaData array using
importer.spritesheet
- Edit the pivot properties (pivots are relative to sprite's rect, in [0-1] range as shown in the inspector), set the alignment to Custom and replace/re-assign the spritesheet array.
- Flag the asset using
EditorUtility.SetDirty ( importer ).
- Commit your changes with
importer.SaveAndReimport ()
Here's a complete script with all the bells and whistles:
using UnityEditor;
using UnityEngine;
public class SpritesToolsWindow : EditorWindow {
[MenuItem ( "Window/My Utilities/Sprites Tools" )]
public static void SpriteTools () {
SpritesToolsWindow spriteTools = GetWindow<SpritesToolsWindow> ( "Sprites Tools", true );
spriteTools.Show ();
if ( Selection.activeObject != null && Selection.activeObject.GetType () == typeof ( Texture2D ) )
spriteTools.spritesheet = ( Texture2D ) Selection.activeObject;
}
public Texture2D spritesheet;
private Vector2 m_CommonPivot;
private string m_Log;
private void OnGUI () {
spritesheet = EditorGUILayout.ObjectField ( "Spritesheet", spritesheet, typeof ( Texture2D ), allowSceneObjects: false ) as Texture2D;
m_CommonPivot = EditorGUILayout.Vector2Field ( "Pivot", m_CommonPivot );
using ( new EditorGUI.DisabledGroupScope ( spritesheet == null ) )
if ( GUILayout.Button ( "Edit sprites" ) )
EditSprites ();
m_Log = EditorGUILayout.TextArea ( m_Log );
}
private void EditSprites () {
if ( spritesheet != null ) {
string spritesheetPath = AssetDatabase.GetAssetPath( spritesheet );
if ( !string.IsNullOrEmpty ( spritesheetPath ) ) {
TextureImporter importer = AssetImporter.GetAtPath( spritesheetPath ) as TextureImporter;
if ( importer != null && importer.spritesheet != null && importer.spriteImportMode == SpriteImportMode.Multiple ) {
SpriteMetaData[] spritesMetaData = importer.spritesheet;
for ( int i = 0; i < spritesMetaData.Length; i++ ) {
SpriteMetaData metaData = spritesMetaData [ i ];
metaData.pivot = m_CommonPivot;
metaData.alignment = ( int ) SpriteAlignment.Custom;
spritesMetaData [ i ] = metaData;
}
importer.spritesheet = spritesMetaData; // seems like this setter internally change stuff (needed)
EditorUtility.SetDirty ( importer );
importer.SaveAndReimport ();
m_Log += string.Format ( "Edited {0} sprites in {1}\n", spritesMetaData.Length, spritesheetPath );
return;
}
m_Log += "Texture is not a spritesheet.\n";
}
}
m_Log += "Could not complete action.\n";
}
}
Cheers!