Last active
November 9, 2018 06:12
-
-
Save kou-yeung/64783678026cc688a8c9f259f9f2d11a 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; | |
using Unity.Transforms; | |
[System.Serializable] | |
public struct Move : IComponentData // ※ MonoBehaviour -> IComponentData に変更 | |
{ | |
} | |
// ※ 公式のECS対応を真似して ComponentDataWrapper 提供。後ほどファイル名をリネームしてGameObject にアタッチできるようにしましょう | |
public class MoveComponent : ComponentDataWrapper<Move>{} | |
[BurstCompile] | |
class MoveSystem : JobComponentSystem | |
{ | |
/// <summary> | |
/// この Group は Game Object Entity をアタッチしたGameObjectをフィルターの設定のようなものです | |
/// 今回の場合、ComponentArray<Move> をもっているため、 | |
/// Move コンポーネントを収集してくれます。 | |
/// </summary> | |
private struct Group | |
{ | |
public readonly int Length; | |
public ComponentDataArray<Move> moveArray; // ※ ComponentDataArray に変更 | |
public ComponentDataArray<Rotation> rotationArray; // ※ TransformAccessArray の代わりに Rotation を使う | |
} | |
// データを Inject するよってマジック的な書き方・・・ | |
[Inject] Group group; | |
protected override JobHandle OnUpdate(JobHandle inputDeps) | |
{ | |
var job = new Job | |
{ | |
deltaTime = Time.deltaTime, | |
moveArray = group.moveArray, | |
rotationArray = group.rotationArray | |
}; | |
return job.Schedule(group.Length, 64, base.OnUpdate(inputDeps)); | |
} | |
/// <summary> | |
/// 並列処理 Job | |
/// </summary> | |
[BurstCompile] | |
struct Job : IJobParallelFor // ※ Transform は使用しないため、IJobParallelForTransform -> IJobParallelFor | |
{ | |
public float deltaTime; | |
public ComponentDataArray<Move> moveArray; // ※ ComponentDataArray に変更 | |
public ComponentDataArray<Rotation> rotationArray; // ※ ComponentDataArray<Rotation> を追加 | |
public void Execute(int index)//, TransformAccess transform) | |
{ | |
//moveArray[index]; // コンポーネントのデータをアクセス必要な場合 | |
// 既存の実装 | |
// transform.localRotation = transform.localRotation * Quaternion.AngleAxis(100 * deltaTime, Vector3.one); | |
// 代わりに rotation を更新する | |
var rotation = rotationArray[index]; | |
rotation.Value = rotationArray[index].Value * Quaternion.AngleAxis(100 * deltaTime, Vector3.one); | |
rotationArray[index] = rotation; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment