-
-
Save kou-yeung/98323d64be6b8c1cd482b51add6531d0 to your computer and use it in GitHub Desktop.
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
using UnityEngine; | |
using Unity.Entities; | |
using Unity.Jobs; | |
using Unity.Burst; | |
using UnityEngine.Jobs; | |
public class Move : MonoBehaviour | |
{ | |
///// MEMO : ComponentSystem.OnUpdate() から更新するため、MonoBehaviourの Update() は使用しません。 | |
//void Update() | |
//{ | |
// transform.localRotation = transform.localRotation * Quaternion.AngleAxis(100 * Time.deltaTime, Vector3.one); | |
//} | |
} | |
[BurstCompile] | |
class MoveSystem : JobComponentSystem | |
{ | |
/// <summary> | |
/// この Group は Game Object Entity をアタッチしたGameObjectをフィルターの設定のようなものです | |
/// 今回の場合、ComponentArray<Move> をもっているため、 | |
/// Move コンポーネントを収集してくれます。 | |
/// </summary> | |
private struct Group | |
{ | |
public readonly int Length; | |
public ComponentArray<Move> moveArray; | |
public TransformAccessArray transformArray; // Transformを収集してくれる | |
} | |
// データを Inject するよってマジック的な書き方・・・ | |
[Inject] Group group; | |
protected override JobHandle OnUpdate(JobHandle inputDeps) | |
{ | |
/// 並列に書き換え | |
//var deltaTime = Time.deltaTime; | |
//for (int i = 0; i < group.Length; i++) | |
//{ | |
// var move = group.moveArray[i]; | |
// var transform = move.transform; | |
// transform.localRotation = transform.localRotation * Quaternion.AngleAxis(100 * deltaTime, Vector3.one); | |
//} | |
//return base.OnUpdate(inputDeps); | |
var job = new Job { deltaTime = Time.deltaTime, moveArray = group.moveArray }; | |
return job.Schedule(group.transformArray, base.OnUpdate(inputDeps)); | |
} | |
/// <summary> | |
/// 並列処理 Job | |
/// </summary> | |
struct Job : IJobParallelForTransform | |
{ | |
public float deltaTime; | |
public ComponentArray<Move> moveArray; | |
public void Execute(int index, TransformAccess transform) | |
{ | |
// moveArray[index]; // コンポーネントのデータをアクセス必要な場合 | |
transform.localRotation = transform.localRotation * Quaternion.AngleAxis(100 * deltaTime, Vector3.one); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment