Hi,
I’m writing a small script that will allow the player to customize a 2d character. I’m having trouble with a specific aspect in my approach, but I’d also be interested in hearing if there is a better overall approach.
Here’s what I’m attempting:
- Create List<List>. This is a list of body part lists
// I’m not really using this yet in my script, but I know I will later. - Create List for each body part
- Set string with path to root folder where there is a folder for each body part
- Find a GameObject for each body part
- Call function to add body parts to body part list with body part list and string as arguments
- Append string argument to root folder to find appropriate body part folder
- Use Resources.LoadAll() with completed folder path and count length of returned object and store as an int
//This is where it gets borked. When I log this to the console, it returns 0 even though I have the path correctly specified and there are 3 .png assets inside the folder. This causes the subsequent for loop to be skipped. Lines 69-86 are the problem areas. - Unload Resources
- Foreach loop that uses Resources.Load() to load in sprite using file path and add it to the appropriate List
//Probably borked too since I don’t think I’m appropriately addressing files, but logging out the string does show me the correct file path. - Add the completed parts list to the list of parts list
- Repeat for each body part list
- Create starting sprite by getting sprite renderer component on each body part GameObject and setting it to render the first sprite in each of the body part lists
- Checking for key input on updates to increment the the sprite renders to look at the next body part variation in the respective body part list.
So it’s not super clean yet, but I’m pretty sure my approach on #7 and #9 are totally wrong. I would love any help in solving this problem. The ultimate intent is to have some folders that follow a naming structure so I can just drop new assets in it over time and increase the amount of options without further work.
Thanks!
George
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KarlaCustomizer : MonoBehaviour
{
public List<List<Sprite>>
allPartsList = new List<List<Sprite>>();
public List<Sprite>
bodies = new List<Sprite>(),
faces = new List<Sprite>(),
hairs = new List<Sprite>(),
legs = new List<Sprite>(),
activeParts = new List<Sprite>();
public GameObject
body,
face,
hair,
leg;
public int
activeBody,
activeFace,
activeHair,
activeLeg;
public Vector2
bodyLocation,
faceLocation,
hairLocation,
legLocation;
private SpriteRenderer
spriteRendererBody,
spriteRendererFace,
spriteRendererHair,
spriteRendererLeg;
public string rootFolder;
void Start()
{
rootFolder = "Assets/Characters/Karla/KarlaParts/Resources/";
body = GameObject.Find("body");
face = GameObject.Find("face");
hair = GameObject.Find("hair");
leg = GameObject.Find("leg");
BuildPartList();
}
void BuildPartList()
{
GetParts(bodies, "body");
GetParts(faces, "face");
GetParts(hairs, "hair");
GetParts(legs, "leg");
BuildStartingSprite();
}
void GetParts(List<Sprite> partList, string partName)
{
print(rootFolder + partName);
var partCounter = Resources.LoadAll(rootFolder + partName);
print(partCounter.Length);
int partCount = partCounter.Length;
Resources.UnloadUnusedAssets();
for (int i = 0; i <= partCount; i++)
{
string number = i.ToString();
Sprite newSprite = Resources.Load<Sprite>(rootFolder + partName + "/" + partName + number);
print(rootFolder + partName + "/" + partName + number);
partList.Add(newSprite);
}
allPartsList.Add(partList);
}
void BuildStartingSprite()
{
spriteRendererBody = body.GetComponent<SpriteRenderer>();
activeBody = 0;
spriteRendererBody.sprite = bodies[activeBody];
spriteRendererFace = face.GetComponent<SpriteRenderer>();
activeFace = 0;
spriteRendererFace.sprite = faces[activeFace];
spriteRendererHair = hair.GetComponent<SpriteRenderer>();
activeHair = 0;
spriteRendererHair.sprite = hairs[activeHair];
spriteRendererLeg = leg.GetComponent<SpriteRenderer>();
activeLeg = 0;
spriteRendererLeg.sprite = legs[activeLeg];
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
SpriteUpdater("body");
}
if (Input.GetKeyDown(KeyCode.S))
{
SpriteUpdater("face");
}
if (Input.GetKeyDown(KeyCode.D))
{
SpriteUpdater("hair");
}
if (Input.GetKeyDown(KeyCode.F))
{
SpriteUpdater("leg");
}
}
void SpriteUpdater(string bodyPart)
{
if (bodyPart == "body")
{
if (activeBody < bodies.Count)
{
activeBody++;
}
else activeBody = 0;
spriteRendererBody.sprite = bodies[activeBody];
print("bodyUpdate");
}
if (bodyPart == "face")
{
if (activeFace < faces.Count)
{
activeFace++;
}
else activeFace = 0;
spriteRendererFace.sprite = faces[activeFace];
print("faceUpdate");
}
if(bodyPart == "hair")
{
if (activeHair < hairs.Count)
{
activeHair++;
}
else activeHair = 0;
spriteRendererHair.sprite = hairs[activeHair];
print("hairUpdate");
}
if(bodyPart == "leg")
{
if (activeLeg < legs.Count)
{
activeLeg ++;
}
else activeLeg = 0;
spriteRendererLeg.sprite = legs[activeLeg];
print("legUpdate");
}
}
}