Convert all ScriptableObjects into enum-like integers for ECS use

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

search on google for “ecs blobasset scriptableobject” and you will find good stuff

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.

If you need an enum, the solution is probably going to involve code-generating an enum based on a list of ScriptableObjects:

  • Have a “EquipmentList” ScriptableObject that holds a List, where the index of each represents the number to store
  • Make this EquipmentList SO be in Resources folder or Addressables
  • Have a script that codegens an enum based on the list in EquipmentList in your EquipmentList’s OnValidate()
  • Use the codegen’d enum in your authoring scripts
  • 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:

  • Drag n drop the equipment SO directly into the authoring component’s inspector
  • 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)
  • 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

4 Likes