Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class SimpleLine : MonoBehaviour
- {
- public List<Vector3> controlPoints;
- }
- .....
- using UnityEngine;
- using UnityEditor;
- using System.Collections;
- using System.Collections.Generic;
- [CustomEditor(typeof(SimpleLine))]
- public class SimpleLineEditor : Editor
- {
- SimpleLine sl;
- private bool listenToControlId;
- private int[] controlIds;
- public void OnEnable()
- {
- sl = (SimpleLine)target;
- }
- public void OnSceneGUI()
- {
- if (sl == null || sl.controlPoints == null)
- return;
- Undo.RecordObject(target, "Move");
- Vector3 offset = sl.transform.position;
- offset.z = 0;
- Handles.color = Color.cyan;
- Handles.DrawLine(offset - Vector3.up * 10, offset + Vector3.up * 10);
- Handles.DrawLine(offset, offset + Vector3.right * 10);
- float scale = HandleUtility.GetHandleSize(sl.transform.position);
- float handleSize = 0.02f * scale;
- Vector3 lastPoint = Vector3.zero;
- if (Event.current.type == EventType.MouseDown)
- listenToControlId = true;
- if (controlIds == null || controlIds.Length != sl.controlPoints.Count)
- controlIds = new int[sl.controlPoints.Count];
- // point editor
- for (int i = 0; i < sl.controlPoints.Count; i++)
- {
- var point = sl.controlPoints[i];
- Handles.color = Color.yellow;
- point = Handles.FreeMoveHandle(point + offset, Quaternion.identity, handleSize, Vector3.zero, (controlID, position, rotation, size) =>
- {
- controlIds[i] = controlID;
- Handles.RectangleCap(controlID, position, rotation, size);
- }) - offset;
- if (GUIUtility.keyboardControl == controlIds[i] && GUIUtility.keyboardControl != 0)
- activeNode = i;
- sl.controlPoints[i] = point;
- if (i > 0)
- Handles.DrawLine(lastPoint + offset, point + offset);
- lastPoint = point;
- }
- for (int i = 0; i < sl.controlPoints.Count; i++)
- {
- if (activeNode == i)
- {
- var point = sl.controlPoints[i];
- Handles.color = Color.yellow;
- point = Handles.PositionHandle(point + offset, Quaternion.identity) - offset;
- sl.controlPoints[i] = point;
- }
- }
- if (GUI.changed)
- EditorUtility.SetDirty(sl);
- }
- public void CustomRectangleCap(int controlID, Vector3 position, Quaternion rotation, float size)
- {
- Handles.RectangleCap(controlID, position, rotation, size);
- }
- public int activeNode { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement