Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple script to switch between Unity sprites for different screen resolutions.
using UnityEngine;
using Zedarus;
using System.Collections;
namespace Zedarus.Kickstart
{
public class SpritesManager : MonoBehaviourSingleton<SpritesManager>
{
#region Parameters
public DensityFolder spritesFolder;
public DensityFolder animationsFolder;
public string[] suffixes = new string[] { "-sd", "-hd", "-shd" };
#endregion
#region Controls
public void UpdateAllSpritesOnScene()
{
float timeStamp = Time.realtimeSinceStartup;
SpriteRenderer[] renderers = GameObject.FindObjectsOfType<SpriteRenderer>();
UpdateSprites(renderers);
Animator[] animators = GameObject.FindObjectsOfType<Animator>();
UpdateAnimators(animators);
Logger.Log("Sprites density updated in " + (Time.realtimeSinceStartup - timeStamp) + " seconds");
}
public void UpdateSpritesOnGameObject(GameObject go)
{
SpriteRenderer[] renderers = go.GetComponentsInChildren<SpriteRenderer>();
UpdateSprites(renderers);
Animator[] animators = go.GetComponentsInChildren<Animator>();
UpdateAnimators(animators);
}
private void UpdateSprites(SpriteRenderer[] renderers)
{
Sprite[] sprites = Resources.LoadAll<Sprite>(SpritesFolder);
foreach (SpriteRenderer renderer in renderers)
{
renderer.sprite = GetSprite(renderer.sprite.name, sprites);
}
Resources.UnloadUnusedAssets();
}
private void UpdateAnimators(Animator[] animators)
{
RuntimeAnimatorController[] controllers = Resources.LoadAll<RuntimeAnimatorController>(AnimationsFolder);
foreach (Animator animator in animators)
{
animator.runtimeAnimatorController = GetAnimationController(animator.runtimeAnimatorController.name, controllers);
}
Resources.UnloadUnusedAssets();
}
#endregion
#region Queries
private ResolutionDensity Density
{
get { return ResolutionDensity.HD; }
}
private string SpritesFolder
{
get { return spritesFolder.GetFolderForDensity(Density); }
}
private string AnimationsFolder
{
get { return animationsFolder.GetFolderForDensity(Density); }
}
#endregion
private Sprite GetSprite(string name, Sprite[] sprites)
{
name = RemoveSuffix(name);
string spriteName;
for (int i = 0; i < sprites.Length; i++)
{
spriteName = RemoveSuffix(sprites [i].name);
if (spriteName.Equals(name))
return sprites [i];
}
return null;
}
private RuntimeAnimatorController GetAnimationController(string name, RuntimeAnimatorController[] controllers)
{
name = RemoveSuffix(name);
string controllerName;
for (int i = 0; i < controllers.Length; i++)
{
controllerName = RemoveSuffix(controllers [i].name);
if (controllerName.Equals(name))
return controllers [i];
}
return null;
}
private string RemoveSuffix(string name)
{
for (int i = 0; i < suffixes.Length; i++)
name = name.Replace(suffixes [i], "");
return name;
}
#region Singleton
// Prevents using 'new' with this class
protected SpritesManager()
{
}
#endregion
}
[System.Serializable]
public class DensityFolder
{
public string SD;
public string HD;
public string SHD;
public string GetFolderForDensity(ResolutionDensity density)
{
switch (density)
{
case ResolutionDensity.SD:
return SD;
case ResolutionDensity.SHD:
return SHD;
case ResolutionDensity.HD:
default:
return HD;
}
}
}
}
@jinxology

This comment has been minimized.

Copy link

@jinxology jinxology commented Nov 5, 2014

Is it possible to use this code to swap the sprites used in the animations of the Animator?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.