I am trying to create a character creation screen using some assets I bought on the asset store.
My game is a top-down 2D so the character has 4 directions (and different sprites for each direction ofc), each direction has up to 50 different body-parts and each body-part has its own spriterenderer, so its very customizable. This is where the problem is, the asset came with a Script that updates all 4 directions sprites with a single method after editing the “main direction” (which is down). This is great but the Script only works in Editor since it uses “UnityEditor.AssetDatabase” to set sprites for all directions.
I didn’t realize this until I did a build of my game and my changes in the character creation screen werent saved. I’ve tried a bunch of different solutions but I cannot figure it out. The creator of the asset named sprites like follows (all inside the same atlas):
A set of the same hair:
Down: Head ObjectsHairhair_9_down_tintable
Up: Head ObjectsHairhair_9_up_tintable
Side (right direction is mirror of left): Head ObjectsHairhair_9_up_tintable
A set of the same shirt:
Down: Equipment Down|Shirts|shirt_down_chest_tintable
Up: Equipment Up|Shirts|shirt_up_chest_tintable
Side (right direction is mirror of left): Equipment Side|Shirts|shirt_side_chest_tintable
And so on, as you can see the names are identical apart from the direction, this is what he uses to find and update all the directions. In order to update all this in the editor using his script he does this:
public void UpdateSprites(bool updateColorOnly)
{
UpdateCachedSpriteRenderersForAllDirections();
UpdateSpritesForDirection(cachedDownSpriteRenderers, "down", updateColorOnly);
UpdateSpritesForDirection(cachedUpSpriteRenderers, "up", updateColorOnly);
if (rightDirection != leftDirection && rightDirection != null)
{
UpdateSpritesForDirection(cachedLeftSpriteRenderers, "side", updateColorOnly);
UpdateSpritesForDirection(cachedRightSpriteRenderers, "side", updateColorOnly);
}
else
{
UpdateSpritesForDirection(cachedLeftSpriteRenderers, "side", updateColorOnly);
}
}
private void UpdateSpritesForDirection(SpriteRenderer[] cachedSpriteRenderers, string direction, bool updateColorOnly)
{
if (cachedSpriteRenderers != null && cachedSpriteRenderers.Length > 0)
{
bool isMainDirection = direction == "down";
bool updateSprites = isMainDirection || !updateColorOnly;
//...
//each slot on the character directions has a unique name, so "__hair slot" finds the spriterenderer for the hair on all directions.
SpriteRenderer hairSlotSR = GetSpriteRendererBySlotName(cachedSpriteRenderers, "__hair slot");
UpdateSprite(hairSlotSR, hair, hairColor, direction, isMainDirection, updateSprites);
//... this is done for each slot on the character (50-ish times)
}
private void UpdateSprite(SpriteRenderer spriteRenderer, Sprite sprite, Color color, string direction, bool isMainDirection, bool updateSprite)
{
#if UNITY_EDITOR
if (spriteRenderer != null)
{
spriteRenderer.color = color;
if (updateSprite)
{
if (isMainDirection)
{
spriteRenderer.sprite = sprite;
}
else
{
if (sprite != null)
{
string spriteName = sprite.name;
spriteName = spriteName.Replace("down", direction);
spriteName = spriteName.Replace("Down", direction.Substring(0, 1).ToUpper() + direction.Substring(1, direction.Length - 1));
string[] assets = UnityEditor.AssetDatabase.FindAssets(spriteName);
for (int i = 0; i < assets.Length; i++)
{
string path = UnityEditor.AssetDatabase.GUIDToAssetPath(assets[0]);
if (UnityEditor.AssetDatabase.GetMainAssetTypeAtPath(path) == typeof(Sprite))
{
Sprite loadedSprite = UnityEditor.AssetDatabase.LoadAssetAtPath<Sprite>(path);
spriteRenderer.sprite = sprite;
break;
}
else if (UnityEditor.AssetDatabase.GetMainAssetTypeAtPath(path) == typeof(Texture2D))
{
bool set = false;
Object[] spriteArray = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(path);
for (int j = 0; j < spriteArray.Length; j++)
{
if (spriteArray[j].GetType() == typeof(Sprite) && spriteArray[j].name == spriteName)
{
spriteRenderer.sprite = spriteArray[j] as Sprite;
set = true;
break;
}
}
if (set)
{
break;
}
}
}
}
}
}
}
#endif
}
The last method is where most of the magic happens, just wanted to show the flow. Is there any way I can do what is done their but without using “UnityEditor.AssetDatabase”?
Here is an image of how its constructed in the editor, showing the “__hair slot” we set in the script above:
