Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question Convert all ScriptableObjects into enum-like integers for ECS use

Discussion in 'Entity Component System' started by Abbrew, Jul 6, 2022.

  1. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    Is there a way to be able to assign all ScriptableObjects of one type to Monobehaviors, and in the conversion code map each ScriptableObject to an incrementing number? For example, if there was a Armor SO with the following settings:

    Chestplate
    Helmet
    Greaves
    Shield

    then they will be mapped to

    Chestplate | 1
    Helmet | 2
    Greaves | 3
    Shield | 4

    and if a Monobehavior assigns a field as, say, Helmet, then it gets converted to "2" and the number is injected into an IComponentData
     
  2. Razmot

    Razmot

    Joined:
    Apr 27, 2013
    Posts:
    346
    search on google for "ecs blobasset scriptableobject" and you will find good stuff
     
  3. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    It's interesting content, but it only converts the ScriptableObject into an equivalent ECS format. I need it to be in an Enum-like format.
     
  4. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    If you need an enum, the solution is probably going to involve code-generating an enum based on a list of ScriptableObjects:
    1. Have a "EquipmentList" ScriptableObject that holds a List<EquipmentScriptableObject>, where the index of each represents the number to store
    2. Make this EquipmentList SO be in Resources folder or Addressables
    3. Have a script that codegens an enum based on the list in EquipmentList in your EquipmentList's OnValidate()
    4. Use the codegen'd enum in your authoring scripts
    5. If you need to retrieve the actual SO from the enum value, load the EquipmentList SO from Resources/Addressables and get the corresponding EquipmentScriptableObject from the list based on enum value (index in the list)
    ________________

    But if a simple int would do, you could skip the codegen part and simply store an int during conversion based on the index of the SO assigned in the authoring component:
    1. Drag n drop the equipment SO directly into the authoring component's inspector
    2. Conversion code loads all equipment SOs from Resources/Addressables, and tries to find the index of equipment SO from previous point in that list of all equipments (if you want more control, you could also manually define a list of equipments in its own SO, like in the first solution)
    3. Conversion code simply stores the index (int) in the ECS component

    ________________

    A third solution would be writing a custom inspector / propertyDrawer that tries to load all SOs of a certain type from Resources/Addressables, and draws a UI dropdown from it
     
    Last edited: Jul 7, 2022
    Abbrew, toomasio, bb8_1 and 1 other person like this.