How to get touch's positon when I hold the button??
I have some ways to solve this problem but its too troublesome. Hope you guys can provide me with any solution. Thanks in advance.
public void OnPointerClick(PointerEventData eventData)
{
mouse_touching = false;
}
public void OnPointerEnter(PointerEventData eventData)
{
mouse_in_button = true;
}
public void OnPointerExit(PointerEventData eventData)
{
mouse_in_button = false;
}
public void OnPointerDown(PointerEventData eventData)
{
mouse_touching = true;
}
void Update () {
if(mouse_touching ==true||mouse_in_button == true)
{
holding();
}
}
void holding()
{
*//HOW CAN I GET --PointerEventData eventData.positon--*
}
Answer by dnwsaa58 · Feb 09, 2018 at 01:08 PM
I found a method a simple solution.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class 底 : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public void OnBeginDrag(PointerEventData eventData)
{
print(eventData.position);
}
public void OnDrag(PointerEventData eventData)
{
print(eventData.position);
}
public void OnEndDrag(PointerEventData eventData)
{
print(eventData.position);
}
}
All right, so I rewrote the script. Assign the Canvas to the "public RectTransform canvas" in the inspector. I hope it will work as you want. Give it a try:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.EventSystems;
public class 底 : $$anonymous$$onoBehaviour, IBeginDragHandler, IDragHandler
{
public RectTransform canvas;
private Vector2 offset;
public void OnBeginDrag(PointerEventData ped)
{
offset = GetCenteredPointerPosition(ped.position) - (Vector2) transform.localPosition;
}
public void OnDrag(PointerEventData ped)
{
transform.localPosition = GetCenteredPointerPosition(ped.position) - offset;
}
private Vector2 GetCenteredPointerPosition(Vector2 pointerPosition)
{
return new Vector2
(
(pointerPosition.x - Screen.width/2)/canvas.localScale.x,
(pointerPosition.y - Screen.height/2)/canvas.localScale.y
);
}
}
Answer by yummy81 · Feb 08, 2018 at 04:57 PM
I modified your script a little. So, when you drop it you'll see four public variables in the inspector. There are two bools, which can be set to true or false. Right next to each of them there are two corresponding Vector2 variables. While clicking or moving the appropriate values will be assigned to them. Simultaneously the position of the mouse will be shown in the console. Here's the code:
[SerializeField]
private bool mouse_touching;
[SerializeField]
private Vector2 mouseTouching;
[SerializeField]
private bool mouse_in_button;
[SerializeField]
private Vector2 mouseInButton;
public void OnPointerClick(PointerEventData eventData)
{
if (mouse_touching == false)
{
mouseTouching = eventData.position;
}
}
public void OnPointerEnter(PointerEventData eventData)
{
if (mouse_in_button == true)
{
mouseInButton = eventData.position;
}
}
public void OnPointerExit(PointerEventData eventData)
{
if (mouse_in_button == false)
{
mouseInButton = eventData.position;
}
}
public void OnPointerDown(PointerEventData eventData)
{
if (mouse_touching == true)
{
mouseTouching = eventData.position;
}
}
private void Update()
{
holding();
}
private void holding()
{
Debug.Log("mouse touching: " + mouseTouching);
Debug.Log("mouse in button: " + mouseInButton);
}
I appreciate your answer but perhaps I didn't make myself clear. I am after all a Chinese,Let me rephrase that. The effect that I want to show is "When I am holding the button and move my finger on my phone ,the gameobject's position change as the touch's position.It's just like drag the gameobject through holding the button." So I need to get the touch which is holding the button.
Your answer
Follow this Question
Related Questions
How can i make a hold method for addforce? 0 Answers
Hold touch button Unity Javascript 1 Answer
How to create a an invisible joystick that appears anywhere on the touch screen? 1 Answer
Tapping in the same place of touchscreen doesn't call OnPointerDown method 0 Answers
Assigning a GameObject variable to equal another GameObject variable via C# script. 0 Answers