Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to Drag and Select multiple Handles in Editor Script

Discussion in 'Editor & General Support' started by MikeUpchat, Feb 25, 2021.

  1. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,056
    I have a line drawing script that uses an array of Vector3 values to draw between and I have an editor script that displays a Handles.PositionHandle at each one of those points so I can drag them around, all works fine but I would like to be able to drag and select multiple handles and move them at the same time. Are there any examples of how this can be done?
     
  2. Dorodo

    Dorodo

    Joined:
    Mar 8, 2015
    Posts:
    44
    Bumping this question. Any ideas? My guess is that I could add a logic using button handles but that'd require to manually select each button. Is it possible to detect multiple buttons in a single selection?
     
  3. jokigenki

    jokigenki

    Joined:
    Jul 19, 2014
    Posts:
    41
    Old question, but the top result when searching for this question, and I couldn't find an actual answer, so posting my solution. This will get you an array of selected handle ids, which you can then use when updating them.

    If you're ok with selecting the handles one-by-one:

    Code (CSharp):
    1. private readonly List<int> Selection = new();
    2.  
    3. private int GetHandleId(int vertexId) {
    4.     // Not sure if there's a better way to do this?
    5.     return 1000 + vertexId;
    6. }
    7.  
    8. // pass in an array of the potential vertices you might want to edit
    9. private void UpdateSelection(Vector3[] vertices) {
    10.     var current1 = Event.current;
    11.     if (current1.type == EventType.MouseDown) {
    12.         for (var i = 0; i < vertices.Length; i++) {
    13.             if (HandleUtility.nearestControl != GetHandleId(i) || current1.button != 0) continue;
    14.             if (Event.current.shift) {
    15.                 if (Selection.Contains(i)) {
    16.                     Selection.Remove(i);
    17.                 } else {
    18.                     Selection.Add(i);
    19.                 }
    20.             }
    21.             else {
    22.                 Selection.Clear();
    23.             }
    24.         }
    25.     }
    26. }