Lets say that I have this basic setup in the inspector:
TestScreen
Canvas
Image1
Image2
…and that I have a ConversionSystem that converts the following authorings to entities:
TestScreenAuthoring (on TestScreen GO)
ImageAuthoring (on Image1 GO)
ImageAuthoring (on Image2 GO)
…and I would like the TestScreen to store Image1Entity and Image2Entity in its component data. My first reflex was to do the following procedure:
ImageConversion creates the entities for my images from their authoring components
TestScreen creates the screen entity, and get the image entities with GetPrimaryEntity from their injected GameObject.
The problem is that the image entities never get created when TestScreen is there, even if have used UpdateAfter(typeof(ImageConversion).
So the question is: Is there a way to get an entity from another conversion, through GetPrimaryEntity or something similar? Or I have to always make sure that two conversions are not child nor parent from another conversion?
This all seems quite limitative in my opinion, especially when dealing with UI which are often a composition of multiple entities.
You could add GameObjectEntity to all your children and the when conversion get the Entity from there.
I’m not sure if GameObjectEntity is going to be deprecated in the future or not but that is how I used to approach that problem.
I use a MonoBehavioube but you may be able to use just the authoring component with a reference to the child transform. After conversion of the parent use ConvertToEntity on the child and the authoring component gets picked up during conversion. In order to link the children to the parent you’ll either need to assign the entity reference to a property in Authoring after conversion or find them via other means.
Your conversion system should not be creating new entities, but rather using the created entities made by the GameObjectConversionMappingSystem. Don’t use GameObjectEntity.
If I were to tackle this, (as I don’t know if there yet an automatic way to convert a hierarchy if you want to use monobehaviours)
I would make the two new entities manually in TestScreenAuthoring with conversionSystem.CreateAdditionalEntity() (using the the gameobject which TestScreenAuthoring is attached to) and then with references to the two ImageScreenAuthoring scripts on the root authoring, manually add any component objects to their respective entities.
// https://discussions.unity.com/t/763336/6
[UpdateInGroup(typeof(GameObjectAfterConversionGroup))]
public class Converter : GameObjectConversionSystem {
protected override void OnUpdate() {
//Only loop through entities in the hybrid world, thanks to DreamingImLatios
Entities.ForEach((TestScreenAuthor testScreenAuthor) => {
//Get the entity that was created for this monobehaviour
var testScreenEntity = GetPrimaryEntity(testScreenAuthor);
//Get the ComponentData
var testScreenComponentData = DstEntityManager.GetComponentData<TestScreen>(testScreenEntity);
//Get the entities for the Image monobehaviours pointed to by the TestScreenAuthor
var Image1Entity = GetPrimaryEntity(testScreenAuthor.Image1Ref);
var Image2Entity = GetPrimaryEntity(testScreenAuthor.Image2Ref);
//Set the data on the component
testScreenComponentData.Image1 = Image1Entity;
testScreenComponentData.Image2 = Image2Entity;
//Set the component data back on the entity
DstEntityManager.SetComponentData<TestScreen>(testScreenEntity, testScreenComponentData);
});
}
}
Edit: I thought [UpdateInGroup(typeof(GameObjectAfterConversionGroup))] means that component generation has finished, but now I’m not sure.