Advertisement
snlehton

Using Handles to edit line segments

Feb 24th, 2015
1,913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class SimpleLine : MonoBehaviour
  6. {
  7.     public List<Vector3> controlPoints;
  8. }
  9.  
  10. .....
  11.  
  12. using UnityEngine;
  13. using UnityEditor;
  14. using System.Collections;
  15. using System.Collections.Generic;
  16.  
  17. [CustomEditor(typeof(SimpleLine))]
  18. public class SimpleLineEditor : Editor
  19. {
  20.     SimpleLine sl;
  21.     private bool listenToControlId;
  22.  
  23.     private int[] controlIds;
  24.  
  25.     public void OnEnable()
  26.     {
  27.         sl = (SimpleLine)target;      
  28.     }
  29.  
  30.     public void OnSceneGUI()
  31.     {  
  32.  
  33.         if (sl == null || sl.controlPoints == null)
  34.             return;
  35.  
  36.         Undo.RecordObject(target, "Move");          
  37.  
  38.         Vector3 offset = sl.transform.position;
  39.         offset.z = 0;
  40.  
  41.         Handles.color = Color.cyan;
  42.         Handles.DrawLine(offset - Vector3.up * 10, offset + Vector3.up * 10);
  43.         Handles.DrawLine(offset, offset + Vector3.right * 10);
  44.  
  45.         float scale = HandleUtility.GetHandleSize(sl.transform.position);
  46.         float handleSize = 0.02f * scale;
  47.  
  48.         Vector3 lastPoint = Vector3.zero;
  49.  
  50.         if (Event.current.type == EventType.MouseDown)
  51.             listenToControlId = true;
  52.  
  53.         if (controlIds == null || controlIds.Length != sl.controlPoints.Count)
  54.             controlIds = new int[sl.controlPoints.Count];
  55.  
  56.         // point editor
  57.         for (int i = 0; i < sl.controlPoints.Count; i++)
  58.         {
  59.             var point = sl.controlPoints[i];
  60.  
  61.             Handles.color = Color.yellow;
  62.  
  63.            
  64.             point = Handles.FreeMoveHandle(point + offset, Quaternion.identity, handleSize, Vector3.zero, (controlID, position, rotation, size) =>
  65.             {
  66.                 controlIds[i] = controlID;
  67.                 Handles.RectangleCap(controlID, position, rotation, size);
  68.             }) - offset;
  69.  
  70.             if (GUIUtility.keyboardControl == controlIds[i] && GUIUtility.keyboardControl != 0)
  71.                 activeNode = i;          
  72.  
  73.             sl.controlPoints[i] = point;              
  74.  
  75.             if (i > 0)
  76.                 Handles.DrawLine(lastPoint + offset, point + offset);
  77.             lastPoint = point;
  78.         }
  79.  
  80.  
  81.         for (int i = 0; i < sl.controlPoints.Count; i++)
  82.         {          
  83.             if (activeNode == i)
  84.             {
  85.                 var point = sl.controlPoints[i];
  86.  
  87.                 Handles.color = Color.yellow;
  88.                 point = Handles.PositionHandle(point + offset, Quaternion.identity) - offset;
  89.                 sl.controlPoints[i] = point;
  90.             }            
  91.         }
  92.  
  93.         if (GUI.changed)
  94.             EditorUtility.SetDirty(sl);
  95.  
  96.     }
  97.  
  98.     public void CustomRectangleCap(int controlID, Vector3 position, Quaternion rotation, float size)
  99.     {
  100.         Handles.RectangleCap(controlID, position, rotation, size);
  101.     }
  102.  
  103.     public int activeNode { get; set; }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement