Last active
May 20, 2018 01:51
-
-
Save kuma360/e158568aa99437e51bd7920dab4f90f9 to your computer and use it in GitHub Desktop.
ECSをJobComponentSystemのIJobProcessComponentDataで並列化する時の書き方
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Unity2018.2.0b4 [EntityComponentSystem(ECS) ver0.0.12-preview1] | |
using UnityEngine; | |
using Unity.Entities; | |
using Unity.Collections; | |
using System.Linq; | |
using System.Collections.Generic; | |
using Unity.Jobs; | |
struct DATA1 : IComponentData | |
{ | |
public int val; | |
} | |
//JobComponentSystemを継承したクラスはComponentData同士の依存関係をうまく処理してくれるとのこと。 | |
class JOB_SYSTEM : JobComponentSystem { | |
//IJobProcessComponentDataを継承すると、 | |
//指定のComponentDataを簡単に並列化できる。 | |
[ComputeJobOptimization] | |
struct JOB : IJobProcessComponentData<DATA1> { | |
public void Execute(ref DATA1 data) | |
{ | |
data.val += 10; | |
} | |
} | |
//JobComponentSystem | |
protected override JobHandle OnUpdate(JobHandle inputDeps) | |
{ | |
//ジョブ同士の依存情報を持ったJobHandleが引数で入ってくるのでそれを第3引数に渡す。 | |
//第2引数は内部ループの分割数だそうです。 | |
var outputdeps = new JOB().Schedule(this, 16, inputDeps); | |
//戻り値で新しい依存情報が戻ってくるのでそれを返却する。 | |
return outputdeps; | |
} | |
} | |
class ECS_SCRIPT : MonoBehaviour | |
{ | |
EntityManager EM; | |
private void Start() | |
{ | |
EM = World.Active.GetOrCreateManager<EntityManager>(); | |
var archeType = EM.CreateArchetype(typeof(DATA1)); | |
var instance = new NativeArray<Entity>(50000, Allocator.Temp); | |
EM.CreateEntity(archeType, instance); | |
instance.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment