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 /
avatar image
0
Question by hexagonius · Jan 31, 2017 at 02:15 PM · gameobjectdrageventsystemswap

How to swap gameObject during EventSystem drag?

I am not referring to the usual selected gameObjects used on UIs. I make use of the interfaces IBegin- /IEnd- /DragHandler in conjunction with the EventSystem for dragging gameobjects through the scene.
What I want to know is, when I deactivate the gameobject I am currently dragging, how can I hot-swap it with another so I don't have to click on it again. Right now it just looses focus.

Comment
Add comment · Show 8
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 Glurth · Jan 31, 2017 at 06:33 PM 0
Share

Have you tired setting Selection.activeObject to the object you want selected? https://docs.unity3d.com/ScriptReference/Selection-activeObject.html There are a a bunch of various Selection members, and I always get 'em confused, so a different one might work better...

avatar image hexagonius Glurth · Feb 01, 2017 at 08:17 PM 0
Share

Selection is an Editor class. and the problem is not a question about better or worse but how.

avatar image Glurth hexagonius · Feb 01, 2017 at 08:42 PM 0
Share

Need more details. What do you currently use for the focus? Are you talking about objects with the Selectable Interface? (https://docs.unity3d.com/ScriptReference/UI.Selectable.html)
if so, perhaps https://docs.unity3d.com/ScriptReference/UI.Selectable.Select.html will suit your needs?

No UnityEditor library classes: ok, any other restrictions we should know about?

Show more comments
avatar image hexagonius · Feb 02, 2017 at 06:35 PM 0
Share

cannot reply I your last post @Glurth. I did what you did already. notice though that with the first object dragged the EventSystem is already null, but still received input. I know the repo but didn't want to dig until nobody knows an answer

avatar image Glurth hexagonius · Feb 02, 2017 at 06:50 PM 0
Share

"notice though that with the first object dragged the EventSystem is already null, but still received input."

An object does not need to be the "selected object" to receive input/events. Nor does receiving input/events automatically select it. Unless, of course, specified otherwise (which is what you need to do.)

Sometimes people want the object to become selected OnPointerOver, sometimes it's OnPointerUp, or whatever; so you need to specify. Sounds like adding the SetSelectedGameObject call inside OnDragStart is what you are looking for.

The Selectable class for example only does this in Select(), and the InputField class, as another example, does this in OnPointerDown https://github.com/$$anonymous$$attRix/UnityDecompiled/blob/753fde37d331b2100f93cc5f9eb343f1dcff5eee/UnityEngine.UI/UnityEngine.UI/InputField.cs

avatar image hexagonius Glurth · Feb 03, 2017 at 10:18 AM 0
Share

I already used SetSelectedGameObject. but despite having a selected gameobject the EventSystem still does not send drag events to the new one.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Glurth · Feb 06, 2017 at 06:49 PM

You will need to track the drag focus yourself to do this.

Replace the StandaloneInputModule component, on your EventSystem game object, with this one:

 public class DragInputModule : StandaloneInputModule
 {
     static public GameObject dragFocusObject;
 
     protected override void ProcessDrag(PointerEventData pointerEvent)
     {
         if (!pointerEvent.dragging)
             dragFocusObject = null;
         if (dragFocusObject != null)
             pointerEvent.pointerDrag = dragFocusObject;
         
         base.ProcessDrag(pointerEvent);
     }
 }

The important part to note is that the PointerEventData itself, contains the reference to the dragged object. This is how the dragged object reference is passed to the EventSystem.

Thought the only relevant line in this usage sample is:

 DragInputModule.dragFocusObject = anotherObject;

Here's the whole sample class anyway (at the 5 second mark, it switches the drag focus to "anotherObject"- it is assumed this object is an IDragHandler for the effect to be seen (I put two of these in my test scene):

 public class DragMe : MonoBehaviour, IDragHandler
 {
     public GameObject anotherObject;
     static public bool swapDone = false;
     private void Update()
     {
         if (DragInputModule.dragFocusObject == null)
             Debug.Log("currentDragObject is null");
         else
             Debug.Log(DragInputModule.dragFocusObject.name + " is the drag obect");
         
         if ((Time.realtimeSinceStartup > 5) && (swapDone==false))
         {
             swapDone = true;
             DragInputModule.dragFocusObject = anotherObject;
         }
     }
 
     public void OnDrag(PointerEventData data)
     {
         //do some actual drag move stuff
         transform.position += new Vector3(data.delta.x, data.delta.y, 0) * .01f;
     }    
 }

Please note: I've only done limited testing, but initial tests show this working.

Comment
Add comment · Show 4 · 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 Glurth · Feb 06, 2017 at 07:27 PM 0
Share

I noticed the debuglogs were not reflectng what I expected. Additional test showed the following code actually worked in the ProcessDrag override. It implies that only the first pointer event data, after pointerEvent.dragging becomes true, needs to be changed!

 protected override void ProcessDrag(PointerEventData pointerEvent)
 {
  if(pointerEvent.dragging && (dragFocusObject != null))
             pointerEvent.pointerDrag = dragFocusObject;
         base.ProcessDrag(pointerEvent);
         dragFocusObject = null; 
 }
avatar image hexagonius Glurth · Feb 07, 2017 at 08:20 PM 0
Share

oh, that looks like something. pointerDrag... yeah, I'll have a look thanks

avatar image Chris_Entropy · Mar 20, 2017 at 05:39 PM 0
Share

Thank you, a real life saver!

avatar image maha-sh · May 16 at 05:44 PM 0
Share

Hello ,the swab are not work with me ?

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

81 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

Related Questions

EventSystem IDragHandler not working on iOS 1 Answer

Drag and Swap two object using touch 0 Answers

How would you override Editor object dragging? 0 Answers

Restricting "Drag GameObject" script to Z Axis? 1 Answer

Make EventSystem ignore a collider 0 Answers


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