#How to add Component based on a prefab to Entity from a monobehaviour script?

1 messages · Page 1 of 1 (latest)

urban crescent
#

From a button click I want to instantiate a new ProjectileShooter with type(IComponentData) on the player (editing the existing projectileshooter works) this script has a reference to the ProjectilePrefab. However if I want to instantiate the ProjectileShooter it needs to acces the baked Projectile but this seems impossible from a monobehaviour script. How would I do something like this?

  entityManager.AddComponentData(entity, new ProjectileShooter {
      Speed = 25,//math.clamp(Unity.Mathematics.Random.CreateFromIndex(0).NextFloat(), 10f, 100f),
      ProjectileLifeTime = 5,
      Projectile = ENTITY,
  });

 ///////////////////////
    public struct ProjectileShooter : IComponentData
    {
        public float Speed;
        public float ProjectileLifeTime;
        public Entity Projectile; //<----------
    }

DOTS 1.0

torpid totem
#

as for whatever you store on monobehaviour

#

you just convert into other entity

#

and components

urban crescent
#

hmm.. what is the recommended flow to use UI with ECS I miss to see any example in the examples, could you point me to something?

torpid totem
#

do it somewhere in the beginning of updates

#

and then query your UI components from systems

#

SystemAPI.ManagedAPI.GetSingleton<MyMainMenuPanel>().Show()

urban crescent
#

u mean like baking the complete canvas?

torpid totem
#

either you load a prefab

#

and instantiate it

#

or you access it via static reference in scene

#

it's up to you

#

but once you have reference

#

you just do root.GetComponentsInChildren<IRegisterableComponent>().foreach

#

and add them to entities

#

var e = EntityManager.CreateEntity();
EntityManager.AddComponentObject(e, registerableComponent);

urban crescent
#

do you have any refence to some code base, I dont follow, too brainlet

torpid totem
#

nah, nothing public