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 
I have tested it in Unity with a sample project. It works fine.
-
The parent is the object on which we tap. There is no need of them to be in a parent child hierarchy.
-
The child is the objects which gets instantiated and starts taking touches.
-
Have tested it on UI object with canvas screen space as camera
-
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;
}
}