Search Unity

DragAndDrop - Get Instantiated Object and other info

Discussion in 'Immediate Mode GUI (IMGUI)' started by BinaryCats, Jul 29, 2017.

  1. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Hi,

    When you click a prefab in the Project Window and drag it into the scene it will Instantiate that prefab and place it into the hierarchy. Is it possibly, and if so how, to get the object that object(s) created from the dragging and dropping.

    I could record the root items of the scene when:
    Code (csharp):
    1. UnityEngine.Event.current.type == EventType.DragPerform
    wait a frame and compare which ones have been added. However I would have thought there would be a nicer way to get the object created from DragAndDrop
     
  2. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    After lots of digging, I finally found a way to do this. Sort of. I'm not sure if you can get the created object, but you can intercept the creation of it and manipulate it as you wish.

    First thing you need to do is subscribe to the `onSceneGUIDelegate`

    Code (csharp):
    1. //Some init
    2.         SceneView.onSceneGUIDelegate += SceneViewDragAndDrop;
    3.  
    Code (csharp):
    1.  
    2.  private void SceneViewDragAndDrop(SceneView sceneView)
    3.     {
    4.         var current = UnityEngine.Event.current;
    5.         var references = DragAndDrop.objectReferences;
    6.         if (current.type == EventType.Repaint || current.type == EventType.Layout) return;
    7.         if (references.Length > 0 && UnityEngine.Event.current.type == EventType.DragPerform)//only check relevant drops
    8.         {
    9.             var obj = GameObject.Instantiate(references[0]);//this example only handles one drop
    10.             var go = (GameObject) GetHidenType(typeof(Editor), "GameObjectInspector").GetField("dragObject").GetValue(null); //Reflect to get the GameObjectInspector  
    11.             if (go)
    12.             {
    13.                 ((GameObject)obj).transform.position = go.transform.position;
    14.                 current.Use(); // don't let the
    15.             }
    16.         }
    17.     }
    18.     public static System.Type GetHidenType(System.Type aBaseClass, string TypeName)
    19.     {
    20.         System.Reflection.Assembly[] AS = System.AppDomain.CurrentDomain.GetAssemblies();
    21.         foreach (var A in AS)
    22.         {
    23.             System.Type[] types = A.GetTypes();
    24.             foreach (var T in types)
    25.             {
    26.                 if (T.IsSubclassOf(aBaseClass) && T.Name == TypeName)
    27.                     return T;
    28.             }
    29.         }
    30.         return null;
    31.     }
    32.  
    GO is the preview object you see in the scene. Setting the current event to use will block creation of go, hence me instantiating the referenced gameobject.

    The refelctioney bit can be done better (i.e. store the fieldinfo on init), but this was a quick example.
    I ripped GetHidenType off someone on the internet, you have to do this because you don't have access to where the dragged gameobject is stored :(
     
    Bromm likes this.
  3. TwoBreadWithCheese

    TwoBreadWithCheese

    Joined:
    Mar 8, 2023
    Posts:
    2
    Is this actually working? GameObjectInspector doesn't has a field called dragObject (it's called m_DragObject now), yet it requires an object reference to call for GetValue(). You put null there, but the m_DragObject variable is not a static variable.

    Have you found the solution now? Thanks in advance!
     
  4. Mirandapal

    Mirandapal

    Joined:
    Mar 10, 2023
    Posts:
    3
    The GameObjectInspector class does not have a field called dragObject, and GetValue() requires an object reference in order to be called. Without a valid reference, the code will not compile. However, if the m_DragObject variable is made static, then the code should work correctly. A software development company typically provides a range of services, including custom software design and development, website design, mobile application development, system integration and maintenance, database design and development, and more. The company's primary focus is to create software applications and products that are functional, efficient, and cost-effective. Depending on the needs of the customer, the MLSDev may also provide consulting services related to strategic planning, process optimization, technical assistance, and more.
     
    Last edited: Mar 24, 2023