Search Unity

Resolved Custom Editor Script Object Selection with Modifier Keys

Discussion in 'Scripting' started by inSight01, Mar 10, 2021.

  1. inSight01

    inSight01

    Joined:
    Apr 18, 2017
    Posts:
    17
    I'm creating a fairly basic script which allows me to select the faces of a mesh in scene view. I got it working for single click selection. However, I'm wanting to implement multi-selection using a modifier key or via mouse drag (similar to ProBuilder's face selection ability which I can use but is overkill for what I need).

    My problem is that whenever I use a modifier key (Shift, Ctrl, Alt etc) or drag the mouse over the object when it's selected the object get's deselected in the scene. When I click back on the object the faces have all been selected. So the multi-select feature works.

    I'm wondering how I can have it so the object remains selected whilst performing those functions?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    20,141
    I know this is gonna sound trite but ... look in the probuilder code and see what they did!

    I'm guessing they did some kind of event studying and consuming events themselves when certain modifier keys were in play, but that's just speculation.
     
  3. inSight01

    inSight01

    Joined:
    Apr 18, 2017
    Posts:
    17
    I have already been doing this. The code itself is a bit out of my scope of understanding. I think ProBuilder may be achieving this via a custom editor window and keeping track of all objects in the scene so when a function is performed it just reselects that object again after the scene is refreshed.

    I'm only working with custom inspectors and this unfortunately cannot be achieved as the code stops running when the object is no longer selected.

    I've been downloading free assets from the asset store and going through the code. I think some achieve this using pickable gizmo attributes. Haven't tried that myself yet but may give it a go when I have the opportunity although I can't see this working for mouse drag rect.

    Thanks. I'll having to Google how to do this.

    EDIT: Almost the first result from a search.

    https://forum.unity.com/threads/sol...t-current-use-not-consume-click-event.842809/

    I think Event.Use(); may be what I am looking for. I'll read more into when I get home.

    EDIT 2: Had a quick test and I believe Event.current.Use is indeed what I am after. Thanks for the advice on "consuming events", it would have taken me a fairly long time to find this as I was previously searching for the terms.
     
    Last edited: Mar 10, 2021
    Kurt-Dekker likes this.
unityunity