Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Unity.Entities;
using Unity.Transforms;
using Unity.Collections;
using Unity.Mathematics;
public class NewComponentDataArraySampleSystem : ComponentSystem
{
ComponentGroup playerGroup, enemyGroup;
protected override void OnCreateManager()
{
// コンポーネントの準備
playerGroup = GetComponentGroup(
ComponentType.ReadOnly<Position>(),
ComponentType.ReadOnly<Player>(),
ComponentType.Create<HitPoint>());
enemyGroup = GetComponentGroup(
ComponentType.ReadOnly<Position>(),
ComponentType.ReadOnly<Enemy>());
RequireForUpdate(playerGroup);
RequireForUpdate(enemyGroup);
}
protected override void OnUpdate()
{
// チャンクからNativeArrayを取得
var playerPositions = playerGroup.ToComponentDataArray<Position>(Allocator.Persistent);
var playerHitpoint = playerGroup.ToComponentDataArray<HitPoint>(Allocator.TempJob);
var enemyPositions = enemyGroup.ToComponentDataArray<Position>(Allocator.TempJob);
// 一定距離になったらPlayerにダメージ的な例
for (int enemyIndex = 0; enemyIndex < enemyPositions.Length; enemyIndex++)
{
for (int playerIndex = 0; playerIndex < playerPositions.Length; playerIndex++)
{
if( math.distance( enemyPositions[enemyIndex].Value, playerPositions[playerIndex].Value) < 1)
{
var hitpoint = playerHitpoint[playerIndex];
hitpoint.Value -= 1;
playerHitpoint[playerIndex] = hitpoint;
}
}
}
// ダメージを反映
playerGroup.CopyFromComponentDataArray(playerHitpoint);
// お片付け
playerPositions.Dispose();
playerHitpoint.Dispose();
enemyPositions.Dispose();
}
}
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.