gpt4 book ai didi

c# - 坐标轴的合适类型?

转载 作者:太空宇宙 更新时间:2023-11-03 19:10:43 24 4
gpt4 key购买 nike

我在为变量 axis 声明构造函数类型时遇到问题。我尝试了 float ,但显然它不起作用。

private void SortDistAxis(List<Transform> targetsList, var axis)
{
targetsList.Sort(
delegate (Transform t1, Transform t2)
{
return selectedTarget.Find("localChild").transform.InverseTransformPoint(t2.position).axis.CompareTo
(selectedTarget.Find("localChild").transform.InverseTransformPoint(t1.position).axis);
});
}

我可以求助于这种方法,但感觉很脏。还有其他选择吗?

private void SortDistAxis(List<Transform> targetsList, string axis)
{
if (axis == "x")
{
targetsList.Sort(
delegate (Transform t1, Transform t2)
{
return selectedTarget.Find("localChild").transform.InverseTransformPoint(t2.position).x.CompareTo
(selectedTarget.Find("localChild").transform.InverseTransformPoint(t1.position).x);
});
}
if (axis == "y")
{
targetsList.Sort(
delegate (Transform t1, Transform t2)
{
return selectedTarget.Find("localChild").transform.InverseTransformPoint(t2.position).y.CompareTo
(selectedTarget.Find("localChild").transform.InverseTransformPoint(t1.position).y);
});
}
}

最佳答案

也许您可以考虑使用委托(delegate)来获取您想要的轴?

private void SortDistAxis(List<Transform> targetsList, Func<PointF, float> axisFunc)
{
targetsList.Sort(
delegate (Transform t1, Transform t2)
{
return axisFunc(selectedTarget.Find("localChild").transform.InverseTransformPoint(t2.position)).CompareTo
(axisFunc(selectedTarget.Find("localChild").transform.InverseTransformPoint(t1.position)));
});
}

用法是:

SortDistAxis(targetsList, p => p.x);
SortDistAxis(targetsList, p => p.y);

我假设 InverseTransformPoint 返回的类型是 PointF

关于c# - 坐标轴的合适类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20854297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com