gpt4 book ai didi

c# - 获取 LineRenderer 的位置

转载 作者:太空宇宙 更新时间:2023-11-03 21:03:50 25 4
gpt4 key购买 nike

我想知道是否有办法获取线条渲染器中节点的位置。在我正在处理的项目中,我有一个 PseudoLine 游戏对象,上面有一个线渲染器。当我画一条线时,我克隆 PseudoLine 来创建一条新线。简单地使用:

Instantiate(gameObject);

我想做的是用预制件创建新的游戏对象,它上面还有一个线渲染器。我想将 PseudoLine 的位置复制到我的新游戏对象的线渲染器中。像这样:

GameObject tempLine = Instantiate(line);
tempLine.GetComponent<LineRenderer>().SetPositions(transform.gameObject.GetComponent<LineRenderer>().Positions);

我检查了文档,但找不到任何有用的内置函数。我该如何解决这个问题?

最佳答案

您可以使用 LineRenderer.GetPositionsLineRenderer.GetPosition获取 LineRenderer 位置的函数。

推荐使用LineRenderer.GetPositions在这种情况下起作用,因为您正在制作职位和 LineRenderer.GetPositions 的完整副本会很快的。

为此,您需要创建新的 Vector3 数组,该数组的长度应为 LineRenderer.numPositions要复制的旧 LineRenderer 的值。在 Unity(5.6) 的新版本中,此变量已重命名为 LineRenderer.positionCount .


GameObject oldLine = gameObject;
GameObject newLine = Instantiate(oldLine);

LineRenderer oldLineComponent = oldLine.GetComponent<LineRenderer>();

//Get old Position Length
Vector3[] newPos = new Vector3[oldLineComponent.positionCount];
//Get old Positions
oldLineComponent.GetPositions(newPos);

//Copy Old postion to the new LineRenderer
newLine.GetComponent<LineRenderer>().SetPositions(newPos);

关于c# - 获取 LineRenderer 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42635915/

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