Legacy Documentation: Version 5.5
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Handles.DrawLine

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function DrawLine(p1: Vector3, p2: Vector3): void;
public static void DrawLine(Vector3 p1, Vector3 p2);

Description

Draw a line from p1 to p2.


Draw Line in the Scene view.

The following example uses DrawLine to draw a line between a GameObject and the GameObjects defined in a list.

To use this example, save the following script into the Assets/Editor folder:

#pragma strict
@CustomEditor(DrawLine)
public class DrawLineEditor extends Editor {
	function OnSceneGUI() {
		var t: DrawLine = target as DrawLine;
		if (t == null || t.GameObjects == null)return ;
		var center: Vector3 = t.transform.position;
		for (var i: int = 0; i < t.GameObjects.Length; i++) {
			if (t.GameObjects[i] != null)
				Handles.DrawLine(center, t.GameObjects[i].transform.position);
		}
	}
}
using UnityEngine;
using UnityEditor;

[CustomEditor( typeof( DrawLine ) )] public class DrawLineEditor : Editor { void OnSceneGUI( ) { DrawLine t = target as DrawLine;

if( t == null || t.GameObjects == null ) return;

Vector3 center = t.transform.position;

for( int i = 0; i < t.GameObjects.Length; i++ ) { if( t.GameObjects[i] != null ) Handles.DrawLine( center, t.GameObjects[i].transform.position ); } } }

...then attach this script to the anchor GameObject which you would like to see lines come from. Drop GameObjects you would like the lines to go to into the array in the script's inspector:

#pragma strict
@ExecuteInEditMode
public class DrawLine extends MonoBehaviour {
	public var GameObjects: GameObject[];
}
using UnityEngine;

[ExecuteInEditMode] public class DrawLine : MonoBehaviour { public GameObject[] GameObjects; }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961