Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Ben 14 · Oct 05, 2010 at 08:06 PM · editordragtilesnap

Editor drag&drop override

Hi, I'm trying to override the default drag&drop behavior of assets from the project pane into the scene view, so that I can snap them to a grid while they are being dragged. Think of it as drag&dropping prefab tiles unto a grid in a tile-based level editor.

I've written a CustomEditor which intercepts the Drag events in the scene view, something like this (C#) :

void OnSceneGUI()
{
    if (Event.current.type == EventType.DragUpdated)
    {
        foreach (Object obj in DragAndDrop.objectReferences)
        {
            GameObject go = (GameObject)obj;
            go.transform.position = myGrid.SnapToNearestTileCenter(go.transform.position);
        }
    }
}

This does get called with a valid dragged object, but the transform position change is not propagated to the scene view, nor is it when the drag is released. Any idea on how to achieve that?

Thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Ben 14 · Oct 05, 2010 at 09:49 PM

Answering my own question: this seems to work fine.

 private GameObject draggedObj;
 void OnSceneGUI()
 {
     if (Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform)
     {
         DragAndDrop.visualMode = DragAndDropVisualMode.Copy; // show a drag-add icon on the mouse cursor
 
         if (draggedObj == null)
             draggedObj = (GameObject)Object.Instantiate(DragAndDrop.objectReferences[0]);
 
         // compute mouse position on the world y=0 plane
         Ray mouseRay = Camera.current.ScreenPointToRay(new Vector3(Event.current.mousePosition.x, Screen.height - Event.current.mousePosition.y, 0.0f));
         if (mouseRay.direction.y < 0.0f)
         {
             float t = -mouseRay.origin.y / mouseRay.direction.y;
             Vector3 mouseWorldPos = mouseRay.origin + t * mouseRay.direction;
             mouseWorldPos.y = 0.0f;
 
             draggedObj.transform.position = terrain.SnapToNearestTileCenter(mouseWorldPos);
         }
 
         if (Event.current.type == EventType.DragPerform)
         {
             DragAndDrop.AcceptDrag();
             draggedObj = null;
         }
 
         Event.current.Use();
     }
 }

The idea is that the Event.current.Use() intercepts the drag so that the scene view doesn't do anything on its own. Instead, I instantiate a dragged object myself, which I can place as I like during the DragUpdated events.

Probably not overly clean, so if you've got better ideas / suggestions don't hesitate.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoyo · Dec 30, 2010 at 08:29 PM 0
Share

Awesome, thanks Ben! I was just trying to get drag/drop to work in custom editor window, and you got me on the right track.

avatar image DaveA · Feb 24, 2011 at 01:38 AM 0
Share

Sorry, I'm still fuzzy on this. I inserted this script in my Editor folder but it does not get called. Can you please post any other code or setup steps needed to get this going? The docs are very sparse. The 'customeditor' for example.....

avatar image DaveA · Feb 24, 2011 at 01:59 AM 0
Share

Ah found clues here: http://answers.unity3d.com/questions/19797/align-with-view-programmatically

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Wheres the snap to pixel option in the shader/material editor? 1 Answer

How does ray snapping in the editor work? -1 Answers

Dragging an object smoothly 2 Answers

How would you override Editor object dragging? 0 Answers

How to fix mouse click and drag hang in Unity 5.2.3.p2 OSX 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges