获取LineRenderer的位置
我想知道是否有办法在行渲染器中获取节点的位置。 在我正在研究的项目中,我有一个PseudoLine
游戏对象,我有一个线渲染器。 当我画一条线时,我克隆PseudoLine
来创建一个新线。 简单地使用:
Instantiate(gameObject);
我想要做的是使用预制件创建新的游戏对象,预制件上还有一个线条渲染器。 我想将PseudoLine
的位置复制到我的新游戏对象的线条渲染器中。 像这样的东西:
GameObject tempLine = Instantiate(line); tempLine.GetComponent().SetPositions(transform.gameObject.GetComponent().Positions);
我检查了文档,找不到任何有用的内置函数。 我该如何解决这个问题?
您可以使用LineRenderer.GetPositions
和LineRenderer.GetPosition
函数来获取LineRenderer.GetPosition
的位置。
在这种情况下,建议使用LineRenderer.GetPositions
函数,因为您正在制作postions的整个副本,而LineRenderer.GetPositions
将会很快。
为此,您需要创建新的Vector3
数组,此数组的长度应为要复制的旧LineRenderer
的LineRenderer.numPositions
值。 在新版Unity(5.6)中 ,此变量已重命名为LineRenderer.positionCount
。
GameObject oldLine = gameObject; GameObject newLine = Instantiate(oldLine); LineRenderer oldLineComponent = oldLine.GetComponent(); //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 ().SetPositions(newPos);