Hey Hey everyone,
i decided recently with the release of 1.0.1 to dig now deeper into Unity DOTS and ECS. So far so good im getting good results and nice Progress and compared to previous Versions this seems now more reliable to work with
I’ve testet different approaches to get the Player Prefab spawned but the most failed through i cannot Access non readable static variables from MonoBehaviour and wanted now to ask here for some tipps maybe how to deal with such “Mechanics”.
However i’ve got it with one approach to work, which doesn’t feel very “handy” to me, but it works.
What would you think about this approach to Spawn a Player? Is it “Wrong” or what would be better solutions, which i couldn’t figure out so far?
Here’s how i Spawn my PlayerPrefab Object:
Got an Authoring Object that contains the Prefab as Entity
class Baker : Baker<ConfigAuthoring>
Got an IJob as CreatePlayerJob which get passed a commandbuffer and the prefab to Execute the Job and Instantiate the Player.
[BurstCompile]
public partial struct CreatePlayerJob : IJob
{
public EntityCommandBuffer commandBuffer;
public Entity prefab;
public void Execute()
{
Entity instance = commandBuffer.Instantiate(prefab);
}
}
i gonna make a Call from a GameManager : MonoBehaviour which gets called by the Start Button
public void SpawnPlayer()
{
entity = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity(typeof(SpawnPlayer));
}
To Create a IComponentData of SpawnPlayer (which contains no Data)
public struct SpawnPlayer : IComponentData { }
once the SpawnPlayer Entity Exists we’re running through the ISystem, CreatePlayerJobSystem
public partial struct CreatePlayerJobSystem : ISystem
{
public bool playerSpawned;
[BurstCompile]
public void OnCreate(ref SystemState state)
{
playerSpawned = false;
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
SystemAPI.TryGetSingletonEntity<SpawnPlayer>(out Entity value);
if (value.Index == 0)
{
return;
}
playerSpawned = true;
var configEntity = SystemAPI.GetSingletonEntity<Config>();
var config = SystemAPI.GetComponent<Config>(configEntity);
CreatePlayerJob job = new CreatePlayerJob
{
commandBuffer = new EntityCommandBuffer(Allocator.TempJob),
prefab = config.CharacterTwoPrefab,
};
JobHandle handle = job.Schedule();
handle.Complete();
job.commandBuffer.Playback(state.EntityManager);
job.commandBuffer.Dispose();
state.EntityManager.RemoveComponent<SpawnPlayer>(value);
}
}
as you can see im trying to get a SingletonEntity of type Spawn Player, if the value.Index is 0 then there is no Entity. If it is something else there must be an Entity, then i get my Config and create the CreatePlayerJob
handle it and let the commandbuffer do his thing and then i remove the SpawnPlayer Component to prevent another player Object from getting spawned.
-----------Off
My biggest problem is when i change the Scene from a “Menu Scene” to the “Game Scene” im having trouble to get the Game going as SystemBase and ISystem run right from the Start and i cannot access them from MonoBehaviour properly and i wanna do certain things before they even can Run, i tried to work with Disable them and stuff but none of it worked properly the way i was looking for.
as an OOP Software Engineer, i really have difficulties to change my mindset for Unity DOTS and ECS… does anyone else experience that? Like it just doesn’t feel right to do it that way, or is it just me?
At the End, the player is Spawned and can be controlled, now the Enemies can find the “Player : IComponentData” to navigate to the Player. Here are 1015 Enemies turning on the Player and Translate to him, while there is no significant impact on the cpu (Awesome).
Thanks for some Information, sorry if everything is a little confuse, im confused too
Here are some additional Questions that go through my mind:
Is there a Better way to Instantiate an Entity Prefab after loading a new Scene, once the Player pressed the Start Button?
Are there downsides of doing it my way?
Why i cannot call Managed Stuff from ISystem? And how can i use the SystemBase as a Bridge between MonoBehaviour and ISystem properly?