Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RequireForUpdateの例
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.Collections;
public class DistanceCheckSystem : ComponentSystem
{
ComponentGroup itemGroup, aiGroup;
protected override void OnCreateManager()
{
base.OnCreateManager();
itemGroup = GetComponentGroup(ComponentType.ReadOnly<Item>(), ComponentType.ReadOnly<Translation>());
aiGroup = GetComponentGroup( ComponentType.ReadOnly<AI>(), ComponentType.ReadOnly<Translation>());
//グループが揃わなければ動作しないようにする
RequireForUpdate(itemGroup);
RequireForUpdate(aiGroup);
}
protected override void OnUpdate()
{
// 複数のComponentGroupをまたぐのでForEachではなくToComponentDataArrayを使う
var itemPositions = itemGroup.ToComponentDataArray<Translation>(Allocator.TempJob);
var aiPositions = aiGroup.ToComponentDataArray<Translation>(Allocator.TempJob);
for(int aiIndex =0; aiIndex < aiPositions.Length; aiIndex++)
{
for(int itemIndex = 0; itemIndex < itemPositions.Length; itemIndex++)
{
var distance = math.distance(aiPositions[aiIndex].Value, itemPositions[itemIndex].Value);
if (distance < 1)
{
Debug.Log($"{distance} find item. {distance}");
continue;
}
}
}
itemPositions.Dispose();
aiPositions.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.