Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created March 10, 2019 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/feba5240f0fdbf0708f4b5eb80a2d206 to your computer and use it in GitHub Desktop.
Save tsubaki/feba5240f0fdbf0708f4b5eb80a2d206 to your computer and use it in GitHub Desktop.
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