Search Unity

Selecting a sprite in the scene as soon as it hits the sprite

Discussion in 'Immediate Mode GUI (IMGUI)' started by FeastSC2, Aug 31, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    How can I make it so that the bounding box created by the mouse selection selects a sprite as soon as it hits the non-transparent part of the sprite in the editor?

    Natively in Unity, a sprite is only selected once the sprite is completely englobed in the selection bounding box. Which I find very uncomfortable to use.

    2017-08-31_15-41-53.gif
     
  2. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    487
    I don't think it's really possible to change Unity's default behaviour. The scene view code seems to call HandleUtility.PickRectObjects, so you can't really change it.

    -sam
     
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Ok, too bad!
     
  4. LaireonGames

    LaireonGames

    Joined:
    Nov 16, 2013
    Posts:
    705
    Should be fairly easy. You can hook into the scene view events with a delegate:

    Code (CSharp):
    1.  SceneView.onSceneGUIDelegate += YourMethodHere;
    Then within your ustom method you Raycast the mouse:

    Code (CSharp):
    1.  Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
    Then see what it hit with a Physics.RayCast. If it hit a sprite then set the Selection.ActiveGameObject to the gameobject that your raycast hit
     
    FeastSC2 likes this.
  5. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Cool, this implies that I have Colliders on all my sprites right?
     
  6. LaireonGames

    LaireonGames

    Joined:
    Nov 16, 2013
    Posts:
    705
    For using the raycast and physics yup it would indeed. You could also do something more manual for ones without colliders.

    E.G you could find all sprites in your scene (GameObject.Find) and loop through them to see if they would intersect your ray
     
    FeastSC2 likes this.
  7. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    How can I get the box selection of the mouse? Because clicking on a sprite already works as intended, however the issue is the "drag" selection. If I knew how to get that info, I could do a BoxCast easily to get the sprites like you suggested.
     
  8. LaireonGames

    LaireonGames

    Joined:
    Nov 16, 2013
    Posts:
    705
    That I don't know since there isn't really anything exposed to the Selection class:

    https://docs.unity3d.com/ScriptReference/Selection.html

    But with what i posted above you can try and make one manually, you can determine where the first mouse down was and compare it to the current mouse position (then use it for your box cast)
     
    FeastSC2 likes this.
  9. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    487
    That's why I mentioned HandleUtility.PickRectObjects. That's what the default box selection code uses, which you can't override.

    -sam