Search Unity

Feedback More options for GameObjectConversionUtility

Discussion in 'Entity Component System' started by Guedez, Jun 25, 2020.

  1. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    I really wanted a proper replacement for this:
    Code (CSharp):
    1. World World = World.DefaultGameObjectInjectionWorld;
    2.             GameObjectConversionSystem convertor = World.GetExistingSystem<GameObjectConversionSystem>();
    3.             EntityManager em = World.EntityManager;
    4.  
    5.             Entity = em.CreateEntity();
    6.  
    7.             object[] parameters = new object[] { Entity, em, convertor };
    8.             foreach (var component in GetComponents<Component>()) {
    9.                 Type t = component.GetType();
    10.                 try {
    11.                     if (t.FullName.EndsWith("Authoring")) {
    12.                         System.Reflection.MethodInfo methodInfo = t.GetMethod("Convert");
    13.                         if (methodInfo != null) {
    14.                             methodInfo.Invoke(component, parameters);
    15.                         }
    16.                     }
    17.                 } catch (Exception) { }
    18.             }
    In my use case, I neither want to delete the original GameObject, nor want to mess with it's children. I just need to add it's Authoring components to it's respective Entity representation. It's a player object that I want to both interact with Entity Systems (and therefore needs to have an Entity to represent it) and I want it to use the PlayerController I have which is GameObject only.
    In the future I might make it into 100% Entity, but that's probably at least 2-3 years in the future, so I will still need the hybrid approach for a long while
     
  2. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992