Created
May 20, 2018 11:00
-
-
Save kuma360/87588bc3b279a1c5cc518fade4aae539 to your computer and use it in GitHub Desktop.
ECSとJobComponentSystemからMonoBehaviourが持っている配列へデータを書き出す。
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; | |
using Unity.Mathematics; | |
struct DATA1 : IComponentData | |
{ | |
public int val; | |
} | |
class JOB_SYSTEM1 : JobComponentSystem | |
{ | |
[ComputeJobOptimization] | |
struct JOB : IJobParallelFor | |
{ | |
public ComponentDataArray<DATA1> _data1; | |
public void Execute(int index) | |
{ | |
var P = _data1[index]; | |
P.val += 10; | |
_data1[index] = P; | |
} | |
} | |
protected override JobHandle OnUpdate(JobHandle inputDeps) | |
{ | |
var data1 = this.GetComponentGroup(typeof(DATA1)).GetComponentDataArray<DATA1>(); | |
var job = new JOB() | |
{ | |
_data1 = data1 | |
}; | |
var outputdeps = job.Schedule(data1.Length, 16, inputDeps); | |
return outputdeps; | |
} | |
} | |
//JOBSystem外の配列に書き込む。 | |
class JOB_SYSTEM2 : JobComponentSystem | |
{ | |
[ComputeJobOptimization] | |
struct JOB : IJobParallelFor | |
{ | |
public NativeArray<float3> _vertex; | |
public ComponentDataArray<DATA1> _data1; | |
public void Execute(int index) | |
{ | |
_vertex[index] = _data1[index].val; | |
} | |
} | |
protected override JobHandle OnUpdate(JobHandle inputDeps) | |
{ | |
var data1 = this.GetComponentGroup(typeof(DATA1)).GetComponentDataArray<DATA1>(); | |
var job = new JOB() | |
{ | |
_data1 = data1, | |
_vertex = ECS_SCRIPT._vertex | |
}; | |
var outputdeps = job.Schedule(data1.Length, 16, inputDeps); | |
return outputdeps; | |
} | |
} | |
class ECS_SCRIPT : MonoBehaviour | |
{ | |
EntityManager EM; | |
List<Entity> entityList; | |
public static NativeArray<float3> _vertex; | |
private void Start() | |
{ | |
EM = World.Active.GetOrCreateManager<EntityManager>(); | |
var archeType = EM.CreateArchetype(typeof(DATA1)); | |
var instance = new NativeArray<Entity>(100000, Allocator.Temp); | |
EM.CreateEntity(archeType, instance); | |
instance.Dispose(); | |
//バッファを用意 | |
_vertex = new NativeArray<float3>(100000, Allocator.Persistent); | |
} | |
private void OnDisable() | |
{ | |
//バッファを破棄 | |
_vertex.Dispose(); | |
} | |
private void Update() | |
{ | |
//_vertexへの書き込み終了を待つ。 | |
EM.CompleteAllJobs(); | |
//取得できたデータにアクセス。 | |
Debug.Log(_vertex[0].x + ":" + _vertex[0].y + ":" + _vertex[0].z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment