I need to make a generic method in C# that builds a list from items in my ScriptableObject.
Below is a screen shot of the SO in the inspector. The SO is made from a spreadsheet using QuickSheet.
I want to be able to take any SO that is in the same category (has the same row names ID, State, En, Es, but different values) and extract each element ID, State, etc. to its own list.
I still do not have a generic method, but I am a little closer.
This is sudo code for what I would like:
private string ActionString(int id, string targetLanguage, object[] scriptableObjectArry)
{
// Search the scriptable objects elements to find the ID
// Search that element to find the targetLanguage string
// Return the string
}
The code below allows me to search through the ScriptableObject and return what I need. However, the ScriptableObject is embedded in the code, for a new ScriptableObject I have to copy and paste then manually edit the ScriptableObject part of the code.
[SerializeField] AppleActions appleActions; // <--- ScriptableObject
private string ActionString(int id, string targetLanguage)
{
List<AppleActionsData> items = new List<AppleActionsData>(); // 1 Make a list
items.AddRange(appleActions.dataArray); // 2 Add the SO data
AppleActionsData foundID = items.Find(x => x.ID[0] == id); // 4 Search for the ID and add it to a list
var foundAction = foundID.GetType().GetProperty(targetLanguage).GetValue(foundID, null); // 5 Get the language data from the element
var strings = ((IEnumerable)foundAction).Cast<object>() // 6 Convert language data to a string array
.Select(x => x == null ? x : x.ToString())
.ToArray();
string actionString = strings[0].ToString();
return actionString;
}