【发布时间】:2022-12-18 23:19:33
【问题描述】:
我正在关注 Microsoft tutorial 使用 Azure Spatial Anchors 创建一个新的 HoloLens Unity 应用程序并且给定的代码有一些错误。
错误'distance' cannot be declared in this scope because that name is used in an enclosing local scope 是第一个遇到的错误。我试图通过在distance前面评论float来解决它,但后来我得到了Cannot use local variable 'distance' before it is declared + Cannot infer the type of implicitly-typed deconstruction variable 'distance'.
private bool IsAnchorNearby(Vector3 position, out GameObject anchorGameObject)
{
anchorGameObject = null;
if (_foundOrCreatedAnchorGameObjects.Count <= 0)
{
return false;
}
//Iterate over existing anchor gameobjects to find the nearest
var (distance, closestObject) = _foundOrCreatedAnchorGameObjects.Aggregate(
new Tuple<float, GameObject>(Mathf.Infinity, null),
(minPair, gameobject) =>
{
Vector3 gameObjectPosition = gameobject.transform.position;
float distance = (position - gameObjectPosition).magnitude;
return distance < minPair.Item1 ? new Tuple<float, GameObject>(distance, gameobject) : minPair;
});
if (distance <= 0.15f)
{
//Found an anchor within 15cm
anchorGameObject = closestObject;
return true;
}
else
{
return false;
}
}
本教程的代码有什么问题?
【问题讨论】:
-
在 lambda 表达式 (
(minPair, gameobject) => {...}) 中为distance使用另一个名称:float d = ...; return d;。 lambda 表达式就像IsAnchorNearby方法中的另一个方法。