Skip to content

Instantly share code, notes, and snippets.

@noio
Created June 22, 2015 08:25
Show Gist options
  • Save noio/66a914ae152eda3a170b to your computer and use it in GitHub Desktop.
Save noio/66a914ae152eda3a170b to your computer and use it in GitHub Desktop.
Replace Sprites in Animation
public static Sprite GetSpriteWithWordReplaced(Sprite sprite, string word, string replaceBy)
{
string assetPath = AssetDatabase.GetAssetPath(sprite);
string spriteName = sprite.name;
string newName = spriteName.Replace(word, replaceBy);
string newPath = assetPath.Replace(word, replaceBy);
foreach (Object assetObject in AssetDatabase.LoadAllAssetsAtPath(newPath))
{
Sprite newSprite = assetObject as Sprite;
if (newSprite != null)
{
if (newSprite.name == newName)
{
return newSprite;
}
}
}
return null;
}
[MenuItem("Kingdom/Create Queen Animation")]
static void CreateQueenAnimation()
{
foreach (string guid in Selection.assetGUIDs)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
AnimationClip clip = (AnimationClip) AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip));
if (clip != null)
{
string newPath = path.Replace("king", "queen");
AnimationClip newClip = (AnimationClip) AssetDatabase.LoadAssetAtPath(newPath, typeof(AnimationClip));
if (newClip == null)
{
Debug.LogFormat("Please create animation at {0} first.", newPath);
return;
}
newClip.frameRate = clip.frameRate;
foreach (var binding in AnimationUtility.GetObjectReferenceCurveBindings(clip))
{
ObjectReferenceKeyframe[] keyframes = AnimationUtility.GetObjectReferenceCurve(clip, binding);
for (int i = 0; i < keyframes.Length; i++)
{
Sprite sprite = keyframes[i].value as Sprite;
Sprite newSprite = GetSpriteWithWordReplaced(sprite, "king", "queen");
if (newSprite == null)
{
Debug.LogFormat("Counterpart not found for {0}.", sprite);
return;
}
keyframes[i].value = newSprite;
}
AnimationUtility.SetObjectReferenceCurve(newClip, binding, keyframes);
}
AssetDatabase.SaveAssets();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment