7

我知道存在一些功能,如 lineRenderer 等,但我想使用两个点(以 Vector3 形式)在场景中创建一条直线。我不想使用任何键或鼠标来画线,我只想在触发某些事件或单击播放按钮后看到场景中的线。

谁能帮我?

4

4 回答 4

7
//For creating line renderer object
lineRenderer = new GameObject("Line").AddComponent<LineRenderer>();
lineRenderer.startColor = Color.black;
lineRenderer.endColor = Color.black;
lineRenderer.startWidth = 0.01f;
lineRenderer.endWidth = 0.01f;
lineRenderer.positionCount = 2;
lineRenderer.useWorldSpace = true;    
                
//For drawing line in the world space, provide the x,y,z values
lineRenderer.SetPosition(0, new Vector3(x,y,z)); //x,y and z position of the starting point of the line
lineRenderer.SetPosition(1, new Vector3(x,y,z)); //x,y and z position of the end point of the line
于 2020-01-16T14:28:05.543 回答
3

好的,我已经通过像这样使用 LineRenderer 解决了这个问题:

var line: GameObject=GameObject.Find("/LineRenderer");
fence = Instantiate(line,Pos,Rotation);
fence.setPosition(0,p1);
fence.setPosition(1,p2);

感谢您在上面的所有回答

于 2013-10-08T23:36:04.450 回答
1

如果您想要 3D 空间中的线条,请尝试创建 LineRenderer,此处为示例:http ://rockonflash.wordpress.com/2010/04/17/how-to-do-lasers-in-unity3d/

此处的文档:http: //docs.unity3d.com/Documentation//Components/class-LineRenderer.html

对于 2D 线(onGUI),请尝试:

 function OnGUI () {
    GUIUtility.ScaleAroundPivot (Vector2(0.5, 0.5), Vector2(328.0, 328.0));
    GUI.Label (Rect (200, 200, 256, 256), textureToDisplay);
 }

此讨论中还有其他选项:http: //forum.unity3d.com/threads/17066-How-to-draw-a-GUI-2D-quot-line-quot

于 2013-10-07T23:52:54.503 回答
-1

另一个可能满足您需求的选项是在您的场景中使用 Gizmo。因为 Gizmo 应用在一个单独的矩阵中,所以你可以用它们做很多有趣的事情。

基础的:

void OnDrawGizmos ()
{
    Gizmos.color = new Color(1f, 0f, 0f, 0.5f);
    Gizmos.DrawLine(positionA, positionB);
}

会带你到那里。然而,我最近一直在使用的东西是偏移 Gizmo 矩阵,然后在单位空间中渲染所有内容。

void OnDrawGizmos ()
{
        Matrix4x4 rotationMatrix = Matrix4x4.TRS(transform.position, transform.rotation, positionA - positionB);
        Gizmos.matrix = rotationMatrix;
        Gizmos.DrawWriteCube(Vector3.zero, Vector3.one);
}

两者都很有趣,但是当您开始尝试表示旋转或需要以其他方式偏移的内容时,第二个实例可以帮助您。

于 2013-10-08T01:46:30.783 回答