Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Oct 21, 2018 at 10:04 PM by dan_wipf for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by dan_wipf · Aug 31, 2018 at 08:59 AM · guiinspectoreventseditorgui

GUI Editor Drag and Drop (Inspector)

Hi. I'm creating an custom Asset for Terrain, where I want to Drag and Drop Prefabs onto a GUI.Box.


Now my problem is that I'm making multiple Drag and Drop Boxes (see Code). Now I have a List> which stores which GameObjects are dragged in to the Tool. If I'm working with simple One Group it shows perfectly every where. But if I create a new Group. The GameObjects which are dragged in are always in the first group.. so that sucks and I don't know exactly how I could make those DragAndDrop boxes standalone..


this is my Code:


     void DrawDragAndDrop(int g){
 
         GUIStyle GuistyleBoxDND = new GUIStyle(GUI.skin.box);
         GuistyleBoxDND.alignment = TextAnchor.MiddleCenter;
         GuistyleBoxDND.fontStyle = FontStyle.Italic; 
         GuistyleBoxDND.fontSize = 12;
         GUI.skin.box = GuistyleBoxDND;
         GuistyleBoxDND.normal.background = MakeTex( 2, 2, Color.white);
 
         Rect myRect = GUILayoutUtility.GetRect(0,20,GUILayout.ExpandWidth(true));
         GUI.Box(myRect,"Drag and Drop Prefabs to this Box!",GuistyleBoxDND);
         {
             if (Event.current.type == EventType.DragUpdated) {
                 DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                 Debug.Log("Drag Updated!");
                 Event.current.Use ();
 
             }   
                
             
             if (Event.current.type == EventType.DragPerform) {
                 Debug.Log("Drag Perform!");
                 Debug.Log(DragAndDrop.objectReferences.Length);
                 for(int i = 0; i<DragAndDrop.objectReferences.Length;i++){
                     myTarget.m_GameObjectGroups[g].Add(DragAndDrop.objectReferences[i] as GameObject);
                 }
                 Event.current.Use ();
             }
         }
     }
 



DrawDragAndDrop(int g){...} is called for every Group in myTarget.m_GameObjectGroups.. And for Every GameObject myTarget.m_GameObjectGroups[g] I create a new EditorGUILayout.Objectfield. Which as I said works for one Group perfectly but, after the first Group all GameObjects are Added in the first Group and not the one I intend to..

I think it's because of the Event which starts.. and I'm not creating an event foreach DragAndDrop field..


might somebody is interested helping me. Thanks dan.

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
2
Best Answer

Answer by Bunny83 · Aug 31, 2018 at 09:37 AM

Well, you don't seem to check the mouse position at all. You want to do something like:

 Rect myRect = GUILayoutUtility.GetRect(0,20,GUILayout.ExpandWidth(true));
 GUI.Box(myRect,"Drag and Drop Prefabs to this Box!",GuistyleBoxDND);
 if (myRect.Contains(Event.current.mousePosition))
 {
     if (Event.current.type == EventType.DragUpdated)
     {
         DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
         Debug.Log("Drag Updated!");
         Event.current.Use ();
     }   
     else if (Event.current.type == EventType.DragPerform)
     {
         Debug.Log("Drag Perform!");
         Debug.Log(DragAndDrop.objectReferences.Length);
         for(int i = 0; i<DragAndDrop.objectReferences.Length;i++)
         {
             myTarget.m_GameObjectGroups[groupIndex].Add(DragAndDrop.objectReferences[i] as GameObject);
         }
         Event.current.Use ();
     }
 }


Finally note that you have a parameter called "i" but you also declared a variable "i" inside your for loop. That's not possible. You have to rename one of them. I'm just guessing here since we don't know how the rest of your code looks like but i guess this "i"

myTarget.m_GameObjectGroups[i].

should be your method parameter while this "i"

 .Add(DragAndDrop.objectReferences[i] as GameObject);

should be your for loop variable.

Finally i would recommend to first check if the object the user dragged actually is a GameObject or not. Something like that:

 for(int i = 0; i<DragAndDrop.objectReferences.Length;i++)
 {
     GameObject go = DragAndDrop.objectReferences[i] as GameObject;
     if (go != null)
         myTarget.m_GameObjectGroups[groupIndex].Add(go);
 }

Comment
Add comment · Show 1 · 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 dan_wipf · Aug 31, 2018 at 10:20 AM 0
Share

Perfect. Thank you. I did infect wit the parameter of the groupIndex. =)

     void DrawDragAndDrop(int m){
 
         Event evt = Event.current;
 
         GUIStyle GuistyleBoxDND = new GUIStyle(GUI.skin.box);
         GuistyleBoxDND.alignment = TextAnchor.$$anonymous$$iddleCenter;
         GuistyleBoxDND.fontStyle = FontStyle.Italic; 
         GuistyleBoxDND.fontSize = 12;
         GUI.skin.box = GuistyleBoxDND;
         GuistyleBoxDND.normal.background = $$anonymous$$akeTex( 2, 2, Color.white);
 
         Rect dadRect = new Rect();
         dadRect = GUILayoutUtility.GetRect(0,20,GUILayout.ExpandWidth(true));
         GUI.Box(dadRect,"Drag and Drop Prefabs to this Box!",GuistyleBoxDND);
 
            if (dadRect.Contains(Event.current.mousePosition))
                 {
                     if (Event.current.type == EventType.DragUpdated)
                     {
                         DragAndDrop.visual$$anonymous$$ode = DragAndDropVisual$$anonymous$$ode.Copy;
                         Debug.Log("Drag Updated!");
                         Event.current.Use ();
                     }   
                     else if (Event.current.type == EventType.DragPerform)
                     {
                         Debug.Log("Drag Perform!");
                         Debug.Log(DragAndDrop.objectReferences.Length);
                         for(int i = 0; i<DragAndDrop.objectReferences.Length;i++)
                         {
                             myTarget.m_GameObjectGroups[m].Add(DragAndDrop.objectReferences[i] as GameObject);
                         }
                         Event.current.Use ();
                     }
                 }
  }
 

Follow this Question

Answers Answers and Comments

171 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there an event being fired off when the Inspector is being resized? 1 Answer

OnInspectorGUI - Using the default Object Selection popup. 1 Answer

Editor GUI Like mesh import animation inspector 0 Answers

Unexpected selection state in custom inspector? 1 Answer

Prefix label greyed out when following component disabled 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