JobHandle SpawnBoltFromEntityList(NativeList <Entity> entityList, Entity prefabEntity, bool isboltFromPlayerList, JobHandle jobDepency) { JobHandle jobDepencyToReturn = jobDepency; if (entityList.Length == 0) { return(jobDepencyToReturn); } UnityEngine.Profiling.Profiler.BeginSample("SpawnBoltFromEntityList"); Entity boltCopy = EntityManager.Instantiate(prefabEntity); EntityManager.RemoveComponent <EntityPrefabData>(boltCopy); //Allocate the amount of entities we need in one shot NativeArray <Entity> newSpawnedBoltEntityArray = new NativeArray <Entity>(entityList.Length, Allocator.TempJob); EntityManager.Instantiate(boltCopy, newSpawnedBoltEntityArray); EntityManager.DestroyEntity(boltCopy); //If the bolts are from players we just set the BoltMoveData directly, they are not enough generated to warrant creating a job if (isboltFromPlayerList) { //For players bolt just set the new bolt data directly //(the cost of starting a job is not work the low amount of data to set) for (int i = 0; i < entityList.Length; i++) { PlayerSpawnBoltData spawnBoltData = EntityManager.GetComponentData <PlayerSpawnBoltData>(entityList[i]); BoltMoveData moveData = EntityManager.GetComponentData <BoltMoveData>(newSpawnedBoltEntityArray[i]); moveData.position = spawnBoltData.spawnPosition; moveData.forwardDirection = spawnBoltData.spawnDirection; EntityManager.SetComponentData <BoltMoveData>(newSpawnedBoltEntityArray[i], moveData); } newSpawnedBoltEntityArray.Dispose(); } else { //For AI bolts, create a job to set the boltMoveData of the new entity //Use GetComponentDataFromEntity the get the components we need SetAIBoltMoveDataJob setAiBoldMoveDataJob = new SetAIBoltMoveDataJob { spawningFromEntityList = entityList, spawnedBoltEntityArray = newSpawnedBoltEntityArray, aiSpawnBoltDataFromEntity = GetComponentDataFromEntity <AISpawnBoltData>(), boldMoveDataFromEntity = GetComponentDataFromEntity <BoltMoveData>(), }; jobDepencyToReturn = setAiBoldMoveDataJob.Schedule(newSpawnedBoltEntityArray.Length, MonoBehaviourECSBridge.Instance.GetJobBatchCount(newSpawnedBoltEntityArray.Length), jobDepency); } UnityEngine.Profiling.Profiler.EndSample(); return(jobDepencyToReturn); }
JobHandle SpawnBoltFromEntityList(NativeList <Entity> entityList, NativeArray <Entity> newSpawnedBoltEntityArray, bool isboltFromPlayerList, JobHandle jobDepency) { JobHandle jobDepencyToReturn = jobDepency; if (entityList.Length == 0) { return(jobDepencyToReturn); } UnityEngine.Profiling.Profiler.BeginSample("SpawnBoltFromEntityList"); //If the bolts are from players we just set the BoltMoveData directly, they are not enough generated to warrant creating a job if (isboltFromPlayerList) { //For players bolt just set the new bolt data directly //(the cost of starting a job is not work the low amount of data to set) for (int i = 0; i < entityList.Length; i++) { PlayerSpawnBoltData spawnBoltData = EntityManager.GetComponentData <PlayerSpawnBoltData>(entityList[i]); Position newPosition = new Position() { Value = spawnBoltData.spawnPosition, }; EntityManager.SetComponentData <Position>(newSpawnedBoltEntityArray[i], newPosition); Rotation newRotation = new Rotation() { Value = quaternion.LookRotation(new float3(0, -1, 0), new float3(0, 0, 1)), }; EntityManager.SetComponentData <Rotation>(newSpawnedBoltEntityArray[i], newRotation); BoltMoveData boltMoveData = EntityManager.GetComponentData <BoltMoveData>(newSpawnedBoltEntityArray[i]); boltMoveData.forwardDirection = spawnBoltData.spawnDirection; EntityManager.SetComponentData <BoltMoveData>(newSpawnedBoltEntityArray[i], boltMoveData); } newSpawnedBoltEntityArray.Dispose(); } else { //For AI bolts, create a job to set the boltMoveData of the new entity //Use GetComponentDataFromEntity the get the components we need SetAIBoltMoveDataJob setAiBoldMoveDataJob = new SetAIBoltMoveDataJob { spawningFromEntityList = entityList, spawnedBoltEntityArray = newSpawnedBoltEntityArray, aiSpawnBoltDataFromEntity = GetComponentDataFromEntity <AISpawnBoltData>(), boltMoveDataFromEntity = GetComponentDataFromEntity <BoltMoveData>(), boltPositionFromEntity = GetComponentDataFromEntity <Position>(), boltRotationFromEntity = GetComponentDataFromEntity <Rotation>(), }; jobDepencyToReturn = setAiBoldMoveDataJob.Schedule(newSpawnedBoltEntityArray.Length, MonoBehaviourECSBridge.Instance.GetJobBatchCount(newSpawnedBoltEntityArray.Length), jobDepency); } UnityEngine.Profiling.Profiler.EndSample(); return(jobDepencyToReturn); }