Skip to content

Instantly share code, notes, and snippets.

@SiarheiPilat
Last active November 23, 2019 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiarheiPilat/de03cbf184a68dbacf5f70da85297efe to your computer and use it in GitHub Desktop.
Save SiarheiPilat/de03cbf184a68dbacf5f70da85297efe to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
/// <summary>
/// Copies pivot from one sprite to another.
/// Adapted from: https://forum.unity.com/threads/copy-spritesheet-slices-and-pivots-solved.301340/
/// </summary>
[CanEditMultipleObjects]
public class SpritePivotCopier : EditorWindow
{
Object copyFrom;
Object copyTo;
[MenuItem("Tools/Sprites/Copy pivot")]
public static void ShowWindow()
{
GetWindow(typeof(SpritePivotCopier));
}
void OnGUI()
{
GUILayout.BeginHorizontal();
GUILayout.Label("Copy from:", EditorStyles.boldLabel);
copyFrom = EditorGUILayout.ObjectField(copyFrom, typeof(Texture2D), false, GUILayout.Width(220));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Copy to:", EditorStyles.boldLabel);
copyTo = EditorGUILayout.ObjectField(copyTo, typeof(Texture2D), false, GUILayout.Width(220));
GUILayout.EndHorizontal();
GUILayout.Space(25f);
EditorGUI.BeginDisabledGroup(!copyFrom || !copyTo ? true : false);
if (GUILayout.Button("Copy pivot"))
{
CopyPivot();
}
EditorGUI.EndDisabledGroup();
}
void CopyPivot()
{
string copyFromPath = AssetDatabase.GetAssetPath(copyFrom);
TextureImporter ti1 = AssetImporter.GetAtPath(copyFromPath) as TextureImporter;
ti1.isReadable = true;
string copyToPath = AssetDatabase.GetAssetPath(copyTo);
TextureImporter ti2 = AssetImporter.GetAtPath(copyToPath) as TextureImporter;
ti2.isReadable = true;
ti2.spritePivot = ti1.spritePivot;
AssetDatabase.ImportAsset(copyToPath, ImportAssetOptions.ForceUpdate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment