x


Object picking from the scene view

Hi,

I am surprised that question haven't been asked before. Or my search skills are horrible (Probably).

I want to know if there is a shortcut or anything to pick objects from the scene view instead of having to using the "Select GameObject" dialog or to drag it from the Hierarchy.

This is really frustrating when you have a level with lot of doors and triggers.

Thanks!

more ▼

asked Mar 31 '12 at 09:20 PM

daivuk gravatar image

daivuk
85 1 2 3

Just to understand your question: You want to drag & drop a gameobject from the sceneview onto a script variable or something like that?

Mar 31 '12 at 09:38 PM Bunny83

Yes, exactly. Or when I open the "Select GameObject" dialog, just a picking in the scene view could do also.

Mar 31 '12 at 09:40 PM daivuk
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Unfortunately there's no such function, but you can work with two inspector windows if it'S really a big scene. That way you can select your object with the script so you see the variable in the inspector and then lock one inspector so it stil shows the script even when you select another object.

Now you can simply click on another object in the scene view so it get highlighted in the projectview and you can drag it from the project view to the variable (which is still visible in the locked inspector window)

To lock an inspector window you just have to click the little pad-lock-icon at the top of the window.

This is also the only way to assign a specific component to another object when there are more than one of these components on this object.

edit

I've just created an EditorWindow which allows you to pick the object under the cursor in the scene view. The picked object is shown in the custom window and you can start any kind of a drag & drop operation from there. It supports a "history stack" of the last picked objects if you want ;)

This script has to be named "SceneViewObjectWindow.cs" and should be placed in "Assets/editor/".

Just open the window via the Tools menu and dock it near the inspector window. To pick an object i've assigned the hotkey "ALT + S" (%s). You can modify it if you want a different hotkey (See MenuItem for more information).

//SceneViewObjectWindow.cs
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class SceneViewObjectWindow : EditorWindow
{
    static GameObject m_LastObject;    
    static List<GameObject> m_Stack = new List<GameObject>();

    static bool m_UseStack = false;
    static float m_MaxStackSize = 5;

    [MenuItem("Tools/Open SceneView Object Selector")]
    public static void OpenWindow()
    {
        EditorWindow.GetWindow<SceneViewObjectWindow>();
    }

    [MenuItem("Tools/Select Scene Object &s")]
    public static void SelectObject()
    {
        if (m_LastObject != null)
        {
            if (!m_UseStack)
                m_Stack.Clear();
            m_Stack.Add(m_LastObject);
        }
        EditorWindow.GetWindow<SceneViewObjectWindow>().Repaint();
    }

    void OnGUI()
    {
        Event e = Event.current;
        m_UseStack = GUILayout.Toggle(m_UseStack,"Use Stack");
        if (m_UseStack)
            m_MaxStackSize = GUILayout.HorizontalSlider(m_MaxStackSize,1,20);

        for(int i = m_Stack.Count-1;i>=0;i--)
        {
            GUILayout.BeginHorizontal();
            EditorGUILayout.ObjectField(m_Stack[i],typeof(GameObject));
            if (GUILayoutUtility.GetLastRect().Contains(e.mousePosition) && e.type == EventType.MouseDrag)
            {
                DragAndDrop.PrepareStartDrag ();
                DragAndDrop.objectReferences = new UnityEngine.Object[] {m_Stack[i]};
                DragAndDrop.StartDrag ("drag");
                Event.current.Use();
            }
            if(GUILayout.Button("X",GUILayout.Width(20)))
            {
                m_Stack.RemoveAt(i);
                Repaint();
            }
            GUILayout.EndHorizontal();
            if (e.type == EventType.Repaint && m_Stack[i] == null)
            {
                m_Stack.RemoveAt(i);
                Repaint();
            }
        }
        if (m_UseStack && e.type == EventType.Repaint)
        {
            while(m_Stack.Count > m_MaxStackSize)
                m_Stack.RemoveAt(0);
        }
    }

    [DrawGizmo(GizmoType.NotSelected)]
    static void RenderCustomGizmo(Transform objectTransform, GizmoType gizmoType)
    {
        if (Event.current == null)
            return;
        Ray ray = HandleUtility.GUIPointToWorldRay (Event.current.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast (ray, out hit))
        {
            m_LastObject = hit.transform.gameObject;
        }
    }
}
more ▼

answered Mar 31 '12 at 09:57 PM

Bunny83 gravatar image

Bunny83
45.2k 11 49 207

Ok thank you. sad..

This was part of my 2 only complaints about Unity. Other was to generate new name for objects every time you create them: Door1, Door2, Door3. That would maybe that issue easier to deal with.

Mar 31 '12 at 10:00 PM daivuk

I've added the script to the UnifyCommunity wiki.

Apr 01 '12 at 12:22 AM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2086
x717
x90
x8

asked: Mar 31 '12 at 09:20 PM

Seen: 1145 times

Last Updated: Apr 01 '12 at 08:01 PM