- Home /
The best way to load a bunch o resources
Hi I was working on a game, and ther's a lot of stuff that needs to be loaded like customizables. So I did a class like this:
public static class Stuff
{
public static GameObject char1 = Resources.Load<GameObject>("Char1 ");
public static GameObject char2 = Resources.Load<GameObject>("Char2 ");
public static GameObject char3 = Resources.Load<GameObject>("Char3 ");
public static GameObject char4 = Resources.Load<GameObject>("Char4 ");
public static GameObject char5 = Resources.Load<GameObject>("Char5 ");
public static GameObject char6 = Resources.Load<GameObject>("Char6 ");
}
So every time need to use any of this assets I can do: Stuff.char...
But I'm sure there's a better way to do it. If you think this is ok let me know:)
Answer by KoenigX3 · May 24, 2020 at 07:24 PM
You can do this with an array of GameObjects.
const string charString = "Char";
const numberOfCustomizables = 6;
public static GameObject[] customizables;
static Stuff()
{
customizables = new GameObject[numberOfCustomizables];
for(int i = 0; i < numberOfCustomizables; i++)
{
customizables[i] = Resources.Load<GameObject>(string.Concat(charString, (i+1), " "));
}
}
Now you can load resources as defined by the numberOfCustomizables integer.
I added the string.Concat function because it can construct strings in a slightly less memory-consuming way but you can simply use ("Char" + (i+1) + " "); if it is better for you. Also, if you don't use the concat, remove the first line.
Your answer
Follow this Question
Related Questions
Loading your game with specific settings 0 Answers
How can I dynamically load a substance? 1 Answer
Standalone player - Customizing launcher 2 Answers
character creation 2 Answers
Character Code like Minecraft? 1 Answer