Created
March 10, 2019 13:08
-
-
Save tsubaki/feba5240f0fdbf0708f4b5eb80a2d206 to your computer and use it in GitHub Desktop.
RequireForUpdateの例
This file contains hidden or 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.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