Hello everyone, a few weeks ago I was looking for a way of copying slices and pivots from one sprite sheet to another, and I could find anything but a plugin called easy sprite sheet.
So after a few days of research I just make it to work and I wanted to share the code with you.
There were two threads that helped me:
Maybe it is not the best way of doing it but could give anyone the idea if someone is struggling with this too as I was.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class MenuItems : MonoBehaviour {
[MenuItem("Sprites/Paste default Slices-Pivots")]
static void PasteDefaultSlicesPivotsCharacter()
{
TextureImporter defaultTextureImporter;
string[] nameFilter = new string[1];
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];
defaultTextureImporter = AssetImporter.GetAtPath("Assets/Resources/{nameofyourtemplatetexture.ext}") as TextureImporter;
defaultTextureImporter.isReadable = true;
List<SpriteMetaData> defaultData = new List<SpriteMetaData>();
for (int j = 0; j < defaultTextureImporter.spritesheet.Length; j++)
{
if(defaultTextureImporter.spritesheet[j].name.Substring(defaultTextureImporter.spritesheet[j].name.IndexOf('_'), (defaultTextureImporter.spritesheet[j].name.Length - defaultTextureImporter.spritesheet[j].name.IndexOf('_'))) ==
d.name.Substring(d.name.IndexOf('_'), (d.name.Length - d.name.IndexOf('_'))))
{
d.alignment = defaultTextureImporter.spritesheet[j].alignment;
d.border = defaultTextureImporter.spritesheet[j].border;
d.pivot = defaultTextureImporter.spritesheet[j].pivot;
d.rect = defaultTextureImporter.spritesheet[j].rect;
Debug.Log("Slice and pivot copied to " + d.name + " from " + defaultTextureImporter.spritesheet[j].name);
}
}
newData.Add(d);
}
ti.spritesheet = newData.ToArray();
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
}
}
static Object[] GetSelectedTextures()
{
return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
}
}
I have a default template where are my slices and pivots, so I just put the name of this default texture where it says “{nameofyourtemplatetexture.ext}”. You could also make that template selectable and not hardcoded but for what I’m doing that works. Note that for this code to work, you have to create a folder on top of the Asset folder called Editor, this code has to be inside this folder. You then will notice a new menu called Sprites on the menu bar in Unity Editor.
With the code above you can share your slices, pivots, borders etc between spritesheets, if then you want to swap sprites between this new sliced spritesheets you can use the following code:
using UnityEngine;
using System.Collections;
public class ReSkinAnimation : MonoBehaviour {
public Sprite spriteSheet;
private Sprite[] subSprites;
private string currentSpriteName;
private string newSpriteName;
private string currentSpriteNumber;
void Start () {
if(spriteSheet == null) return;
currentSpriteName = GetComponent<SpriteRenderer>().sprite.name.Substring(0, GetComponent<SpriteRenderer>().sprite.name.IndexOf('_'));
newSpriteName = spriteSheet.name.Substring(0, spriteSheet.name.IndexOf('_'));
if(currentSpriteName == newSpriteName) return;
subSprites = Resources.LoadAll<Sprite>(newSpriteName);
}
void LateUpdate () {
if(spriteSheet == null) return;
if(currentSpriteName == spriteSheet.name) return;
currentSpriteNumber = GetComponent<SpriteRenderer>().sprite.name.Substring(GetComponent<SpriteRenderer>().sprite.name.IndexOf('_')+1);//(GetComponent<SpriteRenderer>().sprite.name.Length - GetComponent<SpriteRenderer>().sprite.name.IndexOf('_')+1)));
GetComponent<SpriteRenderer>().sprite = subSprites[int.Parse(currentSpriteNumber)];
}
}
Just add this code as a component for the gameobjects that you want to swap their spritesheet, and pass to the public variable spriteSheet, the new sprite you want to use. Note that this gameobject needs to have already a default spritesheet so it can be swapped.
Hope it helps at least one you since I was not able to find too much information about this topics.
See ya.
