public static void FindNearestOutpost(Vector3 unitPosition, Action<Outpost> callBack) { FindNearestOutpostQueryRequest request = new FindNearestOutpostQueryRequest() { location = unitPosition, CallBack = callBack }; Instance.FindNearestOutpostQueryQueue.Enqueue(request); Instance.TryProcessNextFindOutpost(); }
IEnumerator QueryOutpost(FindNearestOutpostQueryRequest request) { var outposts = FindObjectsOfType<Outpost>(); if (outposts != null) { float minDistance = float.MaxValue; Outpost closestOutpost = null; foreach (var post in outposts) { float dist = Vector2.Distance(request.location, post.transform.position); if (dist < minDistance) { minDistance = dist; closestOutpost = post; } } yield return null; request.CallBack(closestOutpost); Instance.IsProcessingOutpostQuery = false; Instance.TryProcessNextFindOutpost(); } }