Skip to content

Instantly share code, notes, and snippets.

@SiarheiPilat
Last active November 23, 2019 20:20
Show Gist options
  • Save SiarheiPilat/c4f3fffcd2fe73838c2c799c85e85b62 to your computer and use it in GitHub Desktop.
Save SiarheiPilat/c4f3fffcd2fe73838c2c799c85e85b62 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
/// <summary>
///
/// Copies the pivot coordinates of a first slice of the spritesheet and applies them to the pivots of other slices in the same spritesheet.
/// This script only works for spritesheets that were sliced using the grid option.
/// How to: select your spritesheet in the project files and go to the menu item to run the command.
///
/// Idea stolen from: https://answers.unity.com/questions/761009/unity2d-sprite-editor-multiple-pivot-point-edit.html
/// Original code: https://www.reddit.com/r/Unity3D/comments/1vnvko/bulk_setting_a_sprites_pivot_in_a_spritesheet/
///
/// </summary>
public class SpritesheetPivotAlign : MonoBehaviour
{
[MenuItem("Tools/Sprites/Align pivots in spritesheet")]
static void SetPivots()
{
Object[] textures = GetSelectedTextures();
Selection.objects = new Object[0];
foreach (Texture2D texture in textures)
{
string path = AssetDatabase.GetAssetPath(texture);
TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;
ti.isReadable = true;
List<SpriteMetaData> newData = new List<SpriteMetaData>();
for (int i = 0; i < ti.spritesheet.Length; i++)
{
SpriteMetaData d = ti.spritesheet[i];
d.alignment = 9;
d.pivot = ti.spritesheet[0].pivot;
newData.Add(d);
}
ti.spritesheet = newData.ToArray();
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
}
}
static Object[] GetSelectedTextures()
{
return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment