void BuildPlayerEntities(PrefabsDictionary prefabsDictionary)
        {
            //Building entities dynamically should be always preferred
            //and MUST be used if an implementor doesn't need to be
            //a Monobehaviour. You should strive to create implementors
            //not as monobehaviours. Implementors as monobehaviours
            //are meant only to function as bridge between Svelto.ECS
            //and Unity3D. Using implementor as monobehaviour
            //just to read serialized data from the editor, is also
            //a bad practice, use a Json file instead.
            var player = prefabsDictionary.Istantiate("Player");

            List <IImplementor> implementors = new List <IImplementor>();

            //fetching implementors as monobehaviours, used as bridge between
            //Svelto.ECS and Unity3D
            player.GetComponents(implementors);
            //Add not monobehaviour implementors
            implementors.Add(new PlayerInputImplementor());
            implementors.Add(new PlayerHealthImplementor(100));

            _entityFactory.BuildEntity <PlayerEntityDescriptor>(player.GetInstanceID(), implementors.ToArray());

            //unluckily the gun is parented in the original prefab, so there is no easy way to create it
            //explicitly, I have to create if from the existing gameobject.
            var gun = player.GetComponentInChildren <PlayerShootingImplementor>();

            _entityFactory.BuildEntity <PlayerGunEntityDescriptor>(gun.gameObject.GetInstanceID(), new object[] { gun });
        }
Beispiel #2
0
        void BuildPlayerEntities()
        {
            var prefabsDictionary = new PrefabsDictionary();

            var player = prefabsDictionary.Istantiate("Player");

            //Building entities explicitly should be always preferred
            //and MUST be used if an implementor doesn't need to be
            //a Monobehaviour. You should strive to create implementors
            //not as monobehaviours. Implementors as monobehaviours
            //are meant only to function as bridge between Svelto.ECS
            //and Unity3D. Using implementor as monobehaviour
            //just to read serialized data from the editor, is also
            //a bad practice, use a Json file instead.
            //The Player Entity is made of EntityViewStruct+Implementors as monobehaviours and
            //EntityStructs. The PlayerInputDataStruct doesn't need to be initialized (yay!!)
            //but the HealthEntityStruct does. Here I show the official method to do it
            var initializer = _entityFactory.BuildEntity <PlayerEntityDescriptor>(player.GetInstanceID(), ECSGroups.Player, player.GetComponents <IImplementor>());

            initializer.Init(new HealthEntityStruct {
                currentHealth = 100
            });

            //unluckily the gun is parented in the original prefab, so there is no easy way to create it
            //explicitly, I have to create if from the existing gameobject.
            var gun = player.GetComponentInChildren <PlayerShootingImplementor>();

            _entityFactory.BuildEntity <PlayerGunEntityDescriptor>(gun.gameObject.GetInstanceID(), ECSGroups.Player, new[] { gun });
        }
        /// <summary>
        /// This is a standard approach to create Entities from already existing GameObject in the scene
        /// It is absolutely not necessary, but convienent in case you prefer this way
        /// </summary>
        /// <param name="contextHolder"></param>
        void ICompositionRoot.OnContextCreated(UnityContext contextHolder)
        {
            var prefabsDictionary = new PrefabsDictionary(Application.persistentDataPath + "/prefabs.json");

            BuildEntitiesFromScene(contextHolder);
            //Entities can also be created dynamically in run-time
            //using the entityFactory; You can, if you wish, create
            //starting entities here.
            BuildPlayerEntities(prefabsDictionary);
            BuildCameraEntity();
        }