Last active
November 23, 2019 20:19
-
-
Save SiarheiPilat/de03cbf184a68dbacf5f70da85297efe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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