1.拿到EntityManager 和 GameObjectConversionSettings
2.通过GameObjectConversionUtility.ConvertGameObjectHierarchy 将需要转换为实体的预制体转换为Entity
3.将Entity Instantiate 到EntityManager 中
具体代码如下
public class ConvertToEntityFactory : MonoBehaviour
{
private EntityManager mEntityManager = null;
private GameObjectConversionSettings mSetting = null;
[SerializeField]
private GameObject _NeedConvertToEntityGameoObject;
[SerializeField] private int sum;
// Start is called before the first frame update
void Start()
{
mEntityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
mSetting = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, null);
Entity needToEntity= GameObjectConversionUtility.ConvertGameObjectHierarchy(_NeedConvertToEntityGameoObject, mSetting);
for (int i = 0; i < sum; ++i)
{
Entity entity = mEntityManager.Instantiate(needToEntity);
// 转换完成
}
World.DefaultGameObjectInjectionWorld.EntityManager.DestroyEntity(needToEntity);
}