Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 Brogan89 · Jun 27, 2017 at 01:57 AM · eventeventsystemdraggingpointer

OnDrag with Instantiated gameObject

I've got a bit of a problem i cant work out, I have a hold down event with OnPointerDown() that will Instantiate an object after x amount of time. But from there I want to transfer the PointerEventData into the instantiated object.

I have tried to initialize my own PointerEventData within the script

  public void Drag()
 {
     var eventData = new PointerEventData(EventSystem.current);
     EventSystem.current.SetSelectedGameObject(gameObject, eventData);
     OnBeginDrag(eventData);
 }

But it doesn't seem to work :/

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

Answer by Piyush_Pandey · Jun 27, 2017 at 11:02 AM

I have tested it in Unity with a sample project. It works fine.

1) The parent is the object on which we tap. There is no need of them to be in a parent child hierarchy.

2) The child is the objects which gets instantiated and starts taking touches.

3) Have tested it on UI object with canvas screen space as camera

4) Both the parent and the child have EventSystems the IDragHandler. When the parent spawns the child , the OnDrag method of the parent passes the PointerEventData to the child. This will make the screen touched go in the sequence: parent -> child. Here the parent is taking the touches but it is passing it on to the child and hence the child moves

I would recommend you to create a sample project, get a hold of how it is working and then apply it to your project.

The parent script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class parent : MonoBehaviour , IPointerDownHandler , IPointerUpHandler , IDragHandler , IBeginDragHandler , IEndDragHandler
 {
     public GameObject childPrefab , instantiatedChild;
 
     //script of the child to get the drag method
     private child childScript;
 
     void Start()
     {
         //cache the prefab
         childPrefab = Resources.Load("child") as GameObject;
     }
 
     public void OnPointerDown(PointerEventData data)
     {
         print("mouse down");
         StartCoroutine("Instantiator");
     }
 
     public void OnBeginDrag(PointerEventData data)
     {
         if (instantiatedChild != null)
         {
             childScript = instantiatedChild.GetComponent<child>();
         }
     }
     public void OnDrag(PointerEventData data)
     {
         if (instantiatedChild != null)
         {
             childScript.OnDrag(data);
         }
     }
     public void OnEndDrag(PointerEventData data)
     {
         instantiatedChild = null;
         childScript = null;
     }
     
     public void OnPointerUp(PointerEventData data)
     {
         //if mouse Up is before 2 seconds, it will not instantiate
         StopCoroutine("Instantiator");
     }
 
     IEnumerator Instantiator()
     {
         yield return new WaitForSeconds(2);
         GameObject go = Instantiate(childPrefab, transform.parent, false ) as GameObject;
         instantiatedChild = go;
     }
 }
 


The child/object which will get the drag after instantiation:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class child : MonoBehaviour , IDragHandler
 {
 
     private Transform thisTransRef;
 
     void Start()
     {
         thisTransRef = transform;
     }
 
     public void OnDrag(PointerEventData eventData)
     {
         var world_point_mousePos = Camera.main.ScreenToWorldPoint(new Vector3(eventData.position.x, eventData.position.y,Mathf.Abs( Camera.main.transform.position.z)));
 
         thisTransRef.position = world_point_mousePos;
     }
 
 }
 







Comment
Add comment · Show 2 · 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 Brogan89 · Jun 28, 2017 at 12:33 AM 1
Share

@Piyush_Pandey thanks for you answer. This really cleared it up for me. I was thinking about it in the wrong way. I thought I could just call the OnDrag() from the child and it would take over from there, but actually need to be continuously calling through the parent. I did get a work around to this problem. but this solution is cleaner and I think is the intended use.

avatar image Piyush_Pandey Brogan89 · Jul 05, 2017 at 07:36 AM 0
Share

you are welcome

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

67 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

Related Questions

Get Global PointerEventData 1 Answer

How to drag gameobjects (other than ui elements) using the event trigger? 2 Answers

Can't fire a Custom Event on UI Button Touch OnClick 1 Answer

OnPointerEnter works only if enter from blank space. 0 Answers

Adding custom BaseEventData to UnityEngine.EventSystems NOT Unity.Events 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