(SO = Scriptable Object)
We've 2 options here. Depending if these SO are serialized or instantiated during play.
Option 1 - SO are serialized
Easier option is to create another monobehaviour or scriptable object and place all the instantiated object there.
public class CharacterList : ScriptableObject
{
public Character[] characterList;
}
The other way will require you to search for the SO in the assets folder by scripting using AssetDatabase.FindAssets and AssetDatabase.LoadAssetAtPath.
In this case the code will look like this:
public class CharacterList
{
List<Character> characterList = new List<Character>();
void PopulateList()
{
string[] assetNames = AssetDatabase.FindAssets("Your_Filter", new[] { "Assets/YourFolder" });
characterList.Clear();
foreach (string SOName in assetNames)
{
var SOpath = AssetDatabase.GUIDToAssetPath(SOName);
var character = AssetDatabase.LoadAssetAtPath<Character>(SOpath);
characterList.Add(character);
}
}
}
How to find them.
VIA NAME
You can replace "Your_Filter" to retrieve your SO. In this case the SO should share come kind of commonality in their names.
VIA FOLDER
You can replace new[] { "Assets/YourFolder" } with your folder. In this case all the SO should be in the same folder AND this folder should contain ONLY these SO.
Option 2 - SO are instantiated
In this case I would suggest to implement some kind of factory.
Instead of creating the SO with the usual code:
ScriptableObject.CreateInstance<YourSo>();
I would use a factory the Create the Instance and store it.
So you will have something like this.
public static class CharacterGenerator
{
static List<Character> characterList = new List<Character>();
public static Character CreateCharacter()
{
var character = ScriptableObject.CreateInstance<YourSo>();
characterList.Add(character);
return character;
}
}
So whenever you want to create a new character you will call CharacterGenerator.CreateCharacter();
Remember also to Clear the list at the start of the game (or when you think it is required).
Character
Scriptable Object that I have created in aList
ofCharacter
objects. Let us say that I have created 100 instances ofCharacter
Class, then I want to store all those 100 instances ofCharacter
in thatList
. Now the List is in another Class cakkedCharacterList
. So whenever I want to see an Item of the list, I will have to writeCharacterListInstance.ListName[i]