Hold and move a button
Hi guys, in the internet i looked very long for a solution for holding an UI-Button with finger/mouse, move it there where my finger while holding is and to stay there where i left my finger from the display but i do not found any solution. Sorry for my English.
Thanks for help!!!
Comment
Answer by singhlaxman9761 · Apr 09, 2021 at 05:49 AM
using UnityEngine;
using UnityEngine.EventSystems;
public class PrefabTest : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public bool canMove = false;
void Update()
{
if (!canMove)
return;
gameObject.transform.position = Input.mousePosition;
Debug.Log("Dragg");
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Down");
canMove = true;
}
public void OnPointerUp(PointerEventData eventData)
{
Debug.Log("Up");
canMove = false;
}
}
Your answer
Follow this Question
Related Questions
unity 2017 Problem with touch and ui button 0 Answers
How to change a button image with script? 1 Answer
Updating Button Text 0 Answers
UI Buttons Trigger displaced after build. Help Please 0 Answers
UI in World Space doesn't respond 1 Answer