points | List of points describing the convex polygon. |
Draw anti-aliased convex polygon specified with point array.
The following example uses DrawAAConvexPolygon to draw an arrow between two objects in the Scene view.
To use this example, save the following script in the Assets/Editor folder:
#pragma strict @CustomEditor(DrawArrow) public class DrawArrowEditor extends Editor { function OnSceneGUI() { var t: DrawArrow = target as DrawArrow; if (t == null || t.From == null || t.To == null)return ; var arrowHead: Vector3[] = new Vector3[3]; var arrowLine: Vector3[] = new Vector3[2]; var start: Vector3 = t.From.transform.position; var end: Vector3 = t.To.transform.position; var forward: Vector3 = (end - start).normalized; var right: Vector3 = Vector3.Cross(Vector3.up, forward).normalized; var size: float = HandleUtility.GetHandleSize(end); var width: float = size * 0.1f; var height: float = size * 0.3f; arrowHead[0] = end; arrowHead[1] = end - forward * height + right * width; arrowHead[2] = end - forward * height - right * width; arrowLine[0] = start; arrowLine[1] = end - forward * height; Handles.color = Color.red; Handles.DrawAAPolyLine(arrowLine); Handles.DrawAAConvexPolygon(arrowHead); } }
using UnityEngine; using UnityEditor;
[CustomEditor( typeof( DrawArrow ) )] public class DrawArrowEditor : Editor { void OnSceneGUI( ) { DrawArrow t = target as DrawArrow;
if( t == null || t.From == null || t.To == null ) return;
Vector3[] arrowHead = new Vector3[3]; Vector3[] arrowLine = new Vector3[2]; Vector3 start = t.From.transform.position; Vector3 end = t.To.transform.position;
Vector3 forward = (end - start).normalized; Vector3 right = Vector3.Cross( Vector3.up, forward ).normalized; float size = HandleUtility.GetHandleSize( end ); float width = size * 0.1f; float height = size * 0.3f;
arrowHead[0] = end; arrowHead[1] = end - forward * height + right * width; arrowHead[2] = end - forward * height - right * width;
arrowLine[0] = start; arrowLine[1] = end - forward * height;
Handles.color = Color.red; Handles.DrawAAPolyLine( arrowLine ); Handles.DrawAAConvexPolygon( arrowHead ); } }
...and attach the following script to a GameObject in the Scene. Drag and drop GameObjects onto the From and To inspectors, corresponding to the GameObjects where you would like to see the arrow drawn:
#pragma strict @ExecuteInEditMode public class DrawArrow extends MonoBehaviour { public var FromTo: GameObject; }
using UnityEngine;
[ExecuteInEditMode] public class DrawArrow : MonoBehaviour { public GameObject From, To; }