Created
August 31, 2018 14:30
-
-
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
This file contains hidden or 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
//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