本文介绍了获取 LineRenderer 的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法获得线渲染器中节点的位置.在我正在处理的项目中,我有一个 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.GetPositionsLineRenderer.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.numPositions 的值要复制的 LineRenderer.在新版本的 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 的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 20:04