Skip to content

Instantly share code, notes, and snippets.

@kou-yeung kou-yeung/Move.cs Secret
Last active Nov 9, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.