1

I have the following situation I need an answer to: I have a parent object with children. These children all have unique meshes. Whenever these children are selected in the SceneView, the parent needs to be selected in stead. The children should never have their inspectors exposed (not even a fraction of a second).

How do I do this?

There are two possible solutions that I have come up with which are not the solution I wish to go for.

  • First being using [SelectionBase] attribute on the parent object. This work perfectly, but only once. The first time the child is selected, the parent gets selected. However, when I click the child again, it still gets selected.

  • Second solution was to react on Selecten.onSelectionChanged. This however is too slow. I can set the selection to the parent if a child gets selected, but the child gets exposed for a few frames still.

Is there an instant solution to this which can guarantee me the functionality of SelectionBase, but then every time in stead of only the first time I click it?

Thanks in advance! :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • What is the issue you are trying to avoid in the first place? Why may your child's Inspector not be exposed even for a ms? – derHugo Feb 01 '21 at 15:43
  • And maybe it would help you to use this http://answers.unity.com/answers/1668491/view.html – derHugo Feb 01 '21 at 15:49
  • I have a component that managed several object parented under it (meshes, splines). I use a custom set of gizmos to manipulate these objects. I dont want to expose the inspector (since i hide the object in the hierarchy too) but I do want to retain the possibility to click these objects in the scene, only to have the (targeted) parent selected. The not even a ms is a preference. I know it is possible in the Unity internals, since they do it with SelectionBase, i just want to tap into that piece of code for myself. – Mathijs van Nimwegen Feb 01 '21 at 16:04
  • I have tried the inspector lock method. This works somewhat better, its is not perfect. For example, i lose the option to manually lock the inspector now since that functionallity is now managed by the script itself. – Mathijs van Nimwegen Feb 02 '21 at 09:58

1 Answers1

0

I have found a way to do exactly what i want. I combine the [SelectionBase] attribute with a piece of code in the editor OnSceneGui.

  • First add the [SelectionBase] attribute to your class

  • Second add this code to its editor class

      private void OnSceneGUI()
      {
          HandleUtility.AddDefaultControl(0);
    
          //Get the transform of the component with the selection base attribute
          Transform selectionBaseTransform = component.transform;
    
          //Detect mouse events
          if (Event.current.type == EventType.MouseDown)
          {
              //get picked object at mouse position
              GameObject pickedObject = HandleUtility.PickGameObject(Event.current.mousePosition, true);
    
              //If selected null or a non child of the component gameobject
              if (pickedObject == null || !pickedObject.transform.IsChildOf(selectionBaseTransform))
              {
                  //Set selection to the picked object
                  Selection.activeObject = pickedObject;
              }
          }
      }
    

This allows the first pick to select the component. From then on, only when you select non-child objects in the scene, selection will actually change.