Unity ECS Ideas
using Unity.Entities; | |
using Unity.Jobs; | |
public abstract class CompositeParallelJobSystem<T1, T2> : JobComponentSystem | |
where T1 : struct, IJobParallelFor | |
where T2 : struct, IJobParallelFor | |
{ | |
protected ComponentGroup group; | |
protected abstract ComponentGroup CreateGroup(); | |
protected abstract T1 CreateJob1(); | |
protected abstract T2 CreateJob2(); | |
protected override void OnCreateManager(int capacity) => group = CreateGroup(); | |
protected override JobHandle OnUpdate(JobHandle inputDeps) | |
{ | |
var job1 = CreateJob1(); | |
var job2 = CreateJob2(); | |
var count = group.CalculateLength(); | |
var job1Handle = job1.Schedule(count, 64, inputDeps); | |
var job2Handle = job2.Schedule(count, 64, inputDeps); | |
return JobHandle.CombineDependencies(job1Handle, job2Handle); | |
} | |
} |
using Unity.Entities; | |
using Unity.Jobs; | |
[ComputeJobOptimization] | |
public struct CopyConstantJob<TComponent> : IJobParallelFor where TComponent : struct, IComponentData | |
{ | |
private TComponent constant; | |
private ComponentDataArray<TComponent> components; | |
public CopyConstantJob(ComponentGroup group, TComponent constant) | |
{ | |
this.constant = constant; | |
this.components = group.GetComponentDataArray<TComponent>(); | |
} | |
public void Execute(int index) => components[index] = constant; | |
} |
using Unity.Entities; | |
using Unity.Mathematics; | |
using Unity.Transforms; | |
using Unity.Transforms2D; | |
using UnityEngine; | |
public class PlayerInputSystem : CompositeParallelJobSystem<CopyConstantJob<Heading2D>, CopyConstantJob<MoveSpeed>> | |
{ | |
protected override ComponentGroup CreateGroup() => | |
GetComponentGroup(ComponentType.ReadOnly<Player>(), typeof(Heading2D), typeof(MoveSpeed)); | |
protected override CopyConstantJob<Heading2D> CreateJob1() => new CopyConstantJob<Heading2D>(group, | |
new Heading2D { Value = new float2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) }); | |
protected override CopyConstantJob<MoveSpeed> CreateJob2() => new CopyConstantJob<MoveSpeed>(group, | |
new MoveSpeed { speed = Input.GetKey(KeyCode.LeftShift) ? 20 : 10 }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment