Skip to content

Instantly share code, notes, and snippets.

@hotcakesdeluxe
Created August 31, 2018 14:30
Show Gist options
  • Save hotcakesdeluxe/4fd40170e865befc63fc3266c7f65a78 to your computer and use it in GitHub Desktop.
Save hotcakesdeluxe/4fd40170e865befc63fc3266c7f65a78 to your computer and use it in GitHub Desktop.
swapping sprites in unity. unity doesn't support SVG sprite sheets yet so this is a decent work around for animating
//useful for multiple sprites that cannot be put into a sheet
//unity current does not support slicing sheets for SVG import so this allows you to take multiple SVGs and animate them in editor
//based on an script from Anima2D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class SpriteSwap : MonoBehaviour
{
public SpriteRenderer m_rend;
public SpriteRenderer cachedSpriteRenderer{
get{
if(!m_rend){
m_rend = GetComponent<SpriteRenderer>();
}
return m_rend;
}
}
[SerializeField]
int m_Frame = 0;
int m_OldFrame = 0;
[SerializeField]
public Sprite[] m_Frames;
public Sprite[] frames
{
get
{
return m_Frames;
}
set
{
m_Frames = value;
}
}
public int frame
{
get
{
return (int)m_Frame;
}
set
{
m_Frame = value;
}
}
void LateUpdate()
{
if(m_OldFrame != frame &&
m_Frames != null &&
m_Frames.Length > 0 && m_Frames.Length > frame && cachedSpriteRenderer){
m_OldFrame = frame;
cachedSpriteRenderer.sprite = m_Frames[frame];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment