问题描述
我想知道是否有一种方法可以获取线渲染器中节点的位置.在我正在研究的项目中,我有一个PseudoLine
游戏对象,在该对象上有一个线条渲染器.画一条线时,我克隆PseudoLine
来创建一条新线.简单地使用:
I wonder if there is a way to get the positions of nodes in a line renderer. In the project I'm working on I have a PseudoLine
game object on which I have a line renderer. When I draw a line, I clone PseudoLine
to create a new line. Using simply:
Instantiate(gameObject);
我想做的是使用预制件创建新的游戏对象,该预制件上还带有线条渲染器.我想将PseudoLine
的位置复制到新游戏对象的线条渲染器中.像这样:
What I want to do is create new gameobjects with a prefab, which also has a line renderer on it. I want to copy the positions of PseudoLine
to my new game object's line renderer. Something like this:
GameObject tempLine = Instantiate(line);
tempLine.GetComponent<LineRenderer>().SetPositions(transform.gameObject.GetComponent<LineRenderer>().Positions);
我检查了文档,但找不到任何有用的内置函数.我该如何解决这个问题?
I checked the documentation and couldn't find any helpful built-in functions. How can I resolve this problem?
推荐答案
您可以使用 LineRenderer.GetPositions
和 LineRenderer.GetPosition
函数来获取LineRenderer
的位置.
You can use the LineRenderer.GetPositions
and the LineRenderer.GetPosition
functions to get the position of the LineRenderer
.
在这种情况下,建议使用 LineRenderer.GetPositions
函数,因为您正在复制位置的完整副本,并且 LineRenderer.GetPositions
将很快为此.
It's recommended to use the LineRenderer.GetPositions
function in this case since you are making a whole copy of the postions and LineRenderer.GetPositions
will be fast for this.
为此,您需要创建新的Vector3
数组,并且此数组的长度应为您要复制的旧LineRenderer
的 LineRenderer.numPositions
值.在新版本的 Unity(5.6)中,此变量已重命名为 LineRenderer.positionCount
.
To do this you need to create new Vector3
array and the length of this array should be the LineRenderer.numPositions
value of the old LineRenderer
you want to duplicate. In new version of Unity(5.6), this variable has been renamed to 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);
这篇关于获取LineRenderer的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!