Guest User

Untitled

a guest
May 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. //Unity2018.2.0b4 [EntityComponentSystem(ECS) ver0.0.12-preview1]
  2. using UnityEngine;
  3. using Unity.Entities;
  4. using Unity.Collections;
  5. using System.Linq;
  6. using System.Collections.Generic;
  7. using Unity.Jobs;
  8.  
  9. struct DATA1 : IComponentData
  10. {
  11. public int val;
  12. }
  13.  
  14. //JobComponentSystemを継承したクラスはComponentData同士の依存関係をうまく処理してくれるとのこと。
  15. class JOB_SYSTEM : JobComponentSystem {
  16.  
  17. //IJobParallelForで書く場合の並列化
  18. [ComputeJobOptimization]
  19. struct JOB : IJobParallelFor {
  20.  
  21. //並列化したいデータ
  22. public ComponentDataArray<DATA1> _data;
  23.  
  24. public void Execute(int index)
  25. {
  26. var P = _data[index];
  27. P.val += 10;
  28. _data[index] = P;
  29. }
  30. }
  31.  
  32.  
  33. //JobComponentSystem
  34. protected override JobHandle OnUpdate(JobHandle inputDeps)
  35. {
  36. //ComponentGroup経由でComponentDataArrayを取得
  37. var data = this.GetComponentGroup(typeof(DATA1)).GetComponentDataArray<DATA1>();
  38.  
  39. //JOBにdataを渡す
  40. var job = new JOB()
  41. {
  42. _data = data
  43. };
  44.  
  45. //実行
  46. var outputdeps = job.Schedule(data.Length, 16, inputDeps);
  47. return outputdeps;
  48. }
  49. }
  50.  
  51.  
  52. class ECS_SCRIPT : MonoBehaviour
  53. {
  54. EntityManager EM;
  55. private void Start()
  56. {
  57. EM = World.Active.GetOrCreateManager<EntityManager>();
  58. var archeType = EM.CreateArchetype(typeof(DATA1));
  59.  
  60. var instance = new NativeArray<Entity>(50000, Allocator.Temp);
  61. EM.CreateEntity(archeType, instance);
  62. instance.Dispose();
  63. }
  64.  
  65. }
Add Comment
Please, Sign In to add comment