I’m doing mobile control, but I still haven’t figured out how to do it, and so far I’ve decided to create a simple script to increase and decrease the button when pressing and releasing, but even here there are difficulties.
I am faced with such a problem, I need to do three buttons for pressing and releasing buttons in mobile control. I decided to make a separate script that increases the button when pressed and held by 1.2 and decreases it by 1.2 when unpinned, but I ran into a problem, the script only works if it is on the button. By adding the “public void Button Function()”, it started working only when releasing and the “public void OnPointerUp” stopped working. Here is the script:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ControlButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public Button button;
private bool isPressed = false;
public void OnPointerDown(PointerEventData eventData)
{
isPressed = true;
button.transform.localScale += new Vector3(1.2f, 1.2f, 1.2f);
}
public void OnPointerUp(PointerEventData eventData)
{
isPressed = false;
button.transform.localScale -= new Vector3(1.2f, 1.2f, 1.2f);
}
private void Update()
{
if (isPressed)
{
button.transform.localScale += new Vector3(0, 0 * Time.deltaTime, 0);
}
}
public void ButtonFunction()
{
OnPointerDown(null);
}
}
If everything works out and everything works, please explain how to make several buttons in this case.
Damlow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.