1
\$\begingroup\$

I have a bunch of hex tiles that I want to import into a tile palette. However their pivot point needs to be set to y 0.35 when the default is 0.5

I could edit the import settings in the sprite inspector or change the pivot point in the sprite editor but that takes forever with hundreds of tiles.

Is there any way I could automate that?

\$\endgroup\$
0
\$\begingroup\$

this can be done with an Editor script and the AssetImporter:

  1. Start by creating a TextureImporter

// spritesheetPath with Assets folder as root TextureImporter importer = AssetImporter.GetAtPath( spritesheetPath ) as TextureImporter;

  1. Get the SpriteMetaData array using importer.spritesheet
  2. 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.
  3. Flag the asset using EditorUtility.SetDirty ( importer ).
  4. 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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.