Search Unity

Question How do I loop through a folder of ScriptableObjects and get the information from all of them?

Discussion in 'Scripting' started by alexb2008, Mar 14, 2023.

  1. alexb2008

    alexb2008

    Joined:
    Mar 16, 2022
    Posts:
    34
    I have a file (currently with only one ScriptableObject but I will eventually add more which is why I need this) and I have another script which I want to have loop through that file and look at the information of all of the SO scripts. Here is the script for each of the SO scripts in the file:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu(menuName ="Add recipie")]
    6. public class craftingRecipies : ScriptableObject
    7. {
    8.     public string[] itemsList;
    9.     public int[] itemsNum;
    10.  
    11.     public string[] outputList;
    12.     public int[] outputNum;
    13. }
    14.  
    How would I go about looping through the File and checking all of this info?
    Where possible, the better the performance the better but at this point in time performance is not the main issue so I am not that fussy
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Collections / groups of ScriptableObjects can either be dragged into an array / list type variable or even loaded en-masse with calls such as
    Resources.LoadAll<T>().


    Then you just iterate each one, and from the look of your data above, iterate each items list and items num within it.

    You may wish to think ahead of the curve and provide ScriptableObject "Ingredients" and then make ScriptableObject "outputs" or "products" rather than all that strings and numbers stuff bare like that.

    Also, you may wish to get your plural / singular correct: You name it
    craftingRecipies
    but I think it is really just ONE recipe, right? So it should be
    CraftingRecipe


    And here's a bunch of them ("recipes" is the correct pluralization of "recipe"):

    Code (csharp):
    1. public CraftingRecipe[] AllRecipes;
    to load at runtime (see Resources.LoadAll() rules in documentation!!!!!!!):

    Code (csharp):
    1. CraftingRecipes[] LoadedRecipes = Resources.LoadAll<CraftingRecipe>( "CraftingRecipesFolder/");
    Fun fact: if you say "recipe" out loud enough time or type it enough times, it starts to look really really really weird as a word.
     
    alexb2008 likes this.
  3. alexb2008

    alexb2008

    Joined:
    Mar 16, 2022
    Posts:
    34
    because the file is filled with c# scripts, in this case the Objects[] would have to be a script[] of sorts. is there way to do a list/array of scripts?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    That's not really how it's done. You might want to review how basic ScriptableObjects work and how you define one class and then make as many instances of it as you want as asset files on disk.

    It's kinda like this:


    Plenty more of where that comes from... see what Youtube suggests for you.