Search Unity

using scriptableObject variable within dropdownlist

Discussion in 'Scripting' started by egg_citizen, Jun 5, 2019.

  1. egg_citizen

    egg_citizen

    Joined:
    Jan 3, 2019
    Posts:
    19
    I hope somebody can help me with this, because after searching for 2 days, I still can't find the answer to my problem, which I thought should be something easy :s.

    So here goes.

    I have made the following scriptable object:
    Code (CSharp):
    1.     [CreateAssetMenu(menuName = "Generator/Create Description")]
    2.     public class Descriptions : ScriptableObject
    3.     {
    4.  
    5.         public string description;
    6.  
    7.     }
    I've created a couple of objects using this. (so multiple Descriptions objects exist)

    In game, I want to let the user choose from one of the Descriptions.description via a dropdownlist.
    So in the dropdownlist, I want to (only) show all the "description" values.

    So if I have the following general code added to the dropdownlist:
    Code (CSharp):
    1.   {
    2.         List<string> m_DropOptions;
    3.         Dropdown m_Dropdown;
    4.  
    5.  
    6.         void Start()
    7.         {
    8.             m_DropOptions = new List<string> { "Option 1", "Option 2" }
    9.             m_Dropdown = GetComponent<Dropdown>();
    10.             m_Dropdown.AddOptions(m_DropOptions);
    11.         }
    12. }
    How do I fill up m_DropOptions with all of the Descriptions.description? (instead of just typing in, like the example above)
    I hope somebody can help me with this, because I need to reference a couple of different scriptable objects this way to build up a character.
     
  2. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    In your dropdownlist code you'll want to have a reference to the Description scriptable objects, so a serialized array of those. Then you can loop over those and fill your dropOptions list.

    The index of the value will be the same for your string and scriptableobject arrays.
     
  3. egg_citizen

    egg_citizen

    Joined:
    Jan 3, 2019
    Posts:
    19
    I was hoping I could do it without adding every Description as a reference, by some kind of search or find.
    I was able to find the name of the Descriptions that way, but couldn't get more then that somehow. (meaning, I could fill the list with the names of the Description files, but didn't find a way to open the scriptable objects using those names)
     
  4. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
  5. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    This is editor only.
    Why? Its not really the Unity Way.
     
  6. egg_citizen

    egg_citizen

    Joined:
    Jan 3, 2019
    Posts:
    19
    Because then I don't have to change the list every time I add a new one.
    That being said, you're probably right.

    And about the link nilsdr shared, it's the code that got me the names of the scriptable objects, but I couldn't get into the objects like that.

    I'm pretty sure there should be a way to do so... but for now, I shouldn't be looking for too much trouble and bite the bullet to just add references. Thnx for setting me straight I guess :rolleyes:. Sometimes I just have my mind set on some kind of functionality and I have a hard time letting it go.
     
  7. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Sorry, thought it was for use in the editor.

    The call to findassets returns guids which can be translated to a path and then you can load it.
     
  8. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    There's a few ways to do it:

    1. You can have one `DescriptionDataManager` scriptableobject that has an array of all Descriptions. You add them as a child to the manager (https://answers.unity.com/questions/1164341/can-a-scriptableobject-contain-a-list-of-scriptabl.html).
    2. You create some editor script that finds all descriptions in OnValidate.
    3. You load them from the Streaming Assets https://docs.unity3d.com/Manual/StreamingAssets.html
    4. You load them from an assetbundle https://docs.unity3d.com/Manual/LoadingResourcesatRuntime.html
     
  9. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    Just throwing this option out there.... With Odin Inspector you could do this because they have a powerful ValueDropDown property that you can customize to do fancy things like that. The only downside is that it's not free because instead of buying Odin and improving the editor, Unity bought an add-tech company. :D
     
  10. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Use the Resources system.

    Put the SO assets you created in a folder/subfolder called "Resources/AllDescriptions" (folder name is important, it needs to be exactly "Resources" and directly under "Assets" folder, subfolder as you want). Then in your code do this:

    Code (CSharp):
    1. // You'll get an array of all Descriptions SOs. "AllDescriptions" is the subfolder name
    2. var allDescriptions = Resources.LoadAll<Descriptions>("AllDescriptions");