Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

DragAndDrop prefabs from EditorWindow with control over placement

Discussion in 'Immediate Mode GUI (IMGUI)' started by MBaadsgaard, May 1, 2014.

  1. MBaadsgaard

    MBaadsgaard

    Joined:
    Mar 19, 2013
    Posts:
    39
    Sorry for the long title, I just want to make sure it says enough about the problem.

    Basically, I am trying to build a level editor, where I would like to drag prefabs from window, into scene view, and make it grid-snap unto the XZ-plane, to build flat top-down levels fast.

    Problem is just that the generic data sent with the drag-drop command DOES result in pulling around a prefab in the window, but it wil move it somewhat in the view plane.

    I wanted to control this with OnSceneGUI, but that is not called when deriving from EditorWindow class.

    So now I imagine I somehow have to hand over control of the dragged object to an Editor script, OR SOMETHING LIKE THAT...
    So before this gets completely stupidly hackish..

    Has anyone else tried something like this, and found a way to make it work? Maybe by using an Editor-script in some way, or have a different, smarter way to do it?
     
  2. MBaadsgaard

    MBaadsgaard

    Joined:
    Mar 19, 2013
    Posts:
    39
    OKay so now I can add a script, that will made a custom editor move the object around, and on mouse event, destroy script so it gets placed.

    So question is... How do I make an instance of a prefab, and add a script to it when I start dragging? :S
     
  3. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    Hey, i am also trying to make an Editor for my Game, did you ever solved your problem?
     
  4. MBaadsgaard

    MBaadsgaard

    Joined:
    Mar 19, 2013
    Posts:
    39
    Yea, but I stopped halfway through. I'll dig up the project later, and post something
     
  5. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    Very nice, thanks mate.
     
  6. MBaadsgaard

    MBaadsgaard

    Joined:
    Mar 19, 2013
    Posts:
    39
    My script instances a prefab when clicking a button in my editor window and adds a script that will make it positioned on the grid closest to the mouse cursor. When you click the script is deleted.

    Would that be good enough?
     
  7. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    Yes, this is exact what i search. :)
     
  8. MBaadsgaard

    MBaadsgaard

    Joined:
    Mar 19, 2013
    Posts:
    39
    Alright, just needed to know if I should go through the trouble and clean out the worst.
    It's still a mess, sorry about that. The package should be 3 scripts. One for the Editor Window, one for when the object is being placed and one for editor behavior while placing.
     

    Attached Files:

    Arkade likes this.
  9. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    Thank you very much, it works perfect.
     
  10. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    One thing, i change your script a bit to integrate to my editor. So now i need a function that open this prefabs window when i press a GUIButton.

    I cant find this part in your script.
     
  11. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    Just as a heads up the OnSceneGUI delegate is public and you can make any class subscribe to it. This allows you to do scene GUI on any class :)

    Code (CSharp):
    1.         SceneView.onSceneGUIDelegate += WhateverYouWant;
     
  12. grimmreefer

    grimmreefer

    Joined:
    Jan 15, 2015
    Posts:
    31
    Thanks, worked very fine.
     
  13. dbdenny

    dbdenny

    Joined:
    Mar 13, 2020
    Posts:
    12