I attempted to simplify the conversion of many kinds of prefabs to entities by using the following approach, where I include FixedList128Bytes<Entity> inside an IComponentData. However, this resulted in InvalidEntity values. I suspect that FixedList128Bytes<Entity> is not compatible with the Baking process in Unity.
Here’s the code I used:
public struct Prefabs : IComponentData
{
public FixedList128Bytes<Entity> CH_01;
public FixedList128Bytes<Entity> CH_02;
.
.
.
}
class PrefabsAuthoring : MonoBehaviour
{
[Header("Character_01")]
public GameObject[] CH_01_GO;
[Header("Character_02")]
public GameObject[] CH_02_GO;
.
.
.
}
class PrefabsBaker : Baker<PrefabsAuthoring>
{
public override void Bake(PrefabsAuthoring authoring)
{
var entity = GetEntity(TransformUsageFlags.Dynamic);
var prefabs = new PrefabsBakerBaker();
AddPrefabsToList(authoring.CH_01_GO, ref prefabs.Ch_01);
AddPrefabsToList(authoring.CH_02_GO, ref prefabs.CH_02);
AddComponent(entity);
}
private void AddPrefabsToList(GameObject[] prefabsGO, ref FixedList128Bytes<Entity> entityList)
{
for (int i = 0; i < prefabsGO.Length; i++)
{
var item = prefabsGO[i] != null ? GetEntity(prefabsGO[i], TransformUsageFlags.Dynamic) : Entity.Null;
entityList.Add(item);
}
}
}
The Issue:
It seems that FixedList128Bytes<Entity> inside an IComponentData doesn’t work well with Unity’s Baking process, as it leads to InvalidEntity entries.
Question:
What would be the best approach to simplify the conversion of many kinds of prefabs into entities while avoiding this issue? Any hints or alternatives would be greatly appreciated!
---- Postscript ----
Why do I want to use FixedList?
It is because I want to prepare a SpawnRequest:IComponentData to be paired with Prefabs:IComponentData and Instantiate with SpawnSystem, as shown below.
public struct SpawnRequest : IComponentData {
public FixedList128Bytes<int> CH_01_SpawnNums;
public FixedList128Bytes<int> CH_02_SpawnNums;
.
.
.
}
public partial struct SpawnSystem : ISystem
{
public void OnUpdate(ref SystemState state){
...
for (int i = 0; i < prefabs.CH_01.Length; i++)
{
for (int i = 0; i < spawnRequest.CH_01_SpawnNums; i++)
{
ECB.Instantiate(prefabs.CH_01[i);
}
}
}
}