Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Feb 22, 2017 at 06:41 AM by Commoble for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Commoble · Feb 22, 2017 at 08:34 AM · 2deditor-scriptingeventdrag-and-droponscenegui

Custom Editor: How to get a reference to an existing object in the scene at the mouse position after drag-and-dropping a prefab into the scene?

This is what I'm trying to accomplish: On drag-and-dropping a prefab into the scene, if another object with the same tag exists at that position (this is a 2D scene, mind you), delete it and replace it with the new object (keeping the old object's position in the scene and the hierarchy).

The part I'm having a problem with is checking if any existing objects are at the drop position and getting a reference to them, and after a couple hours of googling and going through the manual I'm still stumped.

     [CustomEditor(typeof(CommonTile))]
     public class CommonTileInspector : Editor
     {
         void OnSceneGUI()
         {
             if (Event.current.type == EventType.DragExited)
             {
                 GameObject oldObj = this.getObjectAtDropPosition();
                 GameObject newObj = ((CommonTile)this.target).gameObject;
 
                 if (oldObj != null && oldObj.CompareTag(newObj.tag))
                 {
                     newObj.transform.SetParent(oldObj.transform.parent);
                     newObj.transform.position = oldObj.transform.position;
                     Undo.DestroyObjectImmediate(oldObj);
                 }
                 Event.current.Use();
             }
         }
 
         private GameObject getObjectAtDropPosition()
         {
             // this is the part that stumps me
         }
     }



HandleUtility's PickGameObject and PickRectObjects functions look promising, but I can't figure out how to get a mouse position at this point; getting Event.current.mousePosition just gives me the same negative numbers regardless of where I drop the object (likely related to the object not actually existing in the scene until after it's been dropped).

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

  • Sort: 
avatar image
0
Best Answer

Answer by Commoble · Feb 22, 2017 at 06:39 AM

Well, nevermind, I figured it out. Use the new object's transform to get a worldspace rect around that object, and convert it to screen space via the current camera.

Only problem is Undo.DeleteObjectImmediate doesn't delete the old object (and using plain ol' Object.DeleteImmediate doesn't register an undo by itself), but that's a separate problem and I'll make a separate question if it keeps bothering me. Gonna close this one.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[Editor] Get Scroll Wheel while dragging 0 Answers

Editor-only circular contour around prefab instances 2 Answers

Why animation events do not change sprite position? 1 Answer

Editor: calling event.use() prevents gizmos from being drawn 1 Answer

Initiating a Drag event by code 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