Harry Brand
Harry Brand

Reputation: 7

Unity run code on touch with a UI Image - c#

I'm a new user to Unity3D and need a hand.

I have some code which I want to be activated when the UI image is pressed and to end once the press has been released. The game is going to run on both Android and IOS so needs to be supported by both platforms.

Here is my code:

public class RPB : MonoBehaviour {

public Transform LoadingBar;
public Transform TextIndicator;
[SerializeField] private float currentAmount;
[SerializeField] private float speed;
[SerializeField] private float numSec;





// Update is called once per frame
void Update () {

    if (currentAmount < 100) {
        currentAmount += speed * Time.deltaTime;


    } else if (currentAmount > 100){
        currentAmount = 0;

        numSec += 1;


    }

        LoadingBar.GetComponent<Image>().fillAmount = currentAmount /100;
        TextIndicator.GetComponent<Text> ().text = ((int)numSec).ToString () + "s";


}
}

How would I be able to do this?

Thank you

Upvotes: 0

Views: 571

Answers (2)

Programmer
Programmer

Reputation: 125275

I have some code which I want to be activated when the UI image is pressed and to end once the press has been released

I would have have advised you to use the Button component too but it doesn't have click down and up events. It only has onClick event which means you can only detect when a there is a Button click not when pressed and released.

This can be done with the Image component. Implement IPointerDownHandler and IPointerUpHandler then override the OnPointerDown and OnPointerUp functions. These functions will be called when there is a mouse click and release on your Image. Attach the script to the Image and that should do the job assuming that the current code you have is logically correct.

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class YourScript : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public Transform LoadingBar;
    public Transform TextIndicator;
    [SerializeField]
    private float currentAmount;
    [SerializeField]
    private float speed;
    [SerializeField]
    private float numSec;


    bool isPressed = false;

    public void OnPointerDown(PointerEventData eventData)
    {
        //  someCode();
        Debug.Log("Mouse Down");
        isPressed = true;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        Debug.Log("Mouse Up");
        isPressed = false;
    }

    void Update()
    {
        if (isPressed)
        {
            someCode();
        }

    }
    void someCode()
    {
        if (currentAmount < 100)
        {
            currentAmount += speed * Time.deltaTime;


        }
        else if (currentAmount > 100)
        {
            currentAmount = 0;

            numSec += 1;
        }
        LoadingBar.GetComponent<Image>().fillAmount = currentAmount / 100;
        TextIndicator.GetComponent<Text>().text = ((int)numSec).ToString() + "s";
    }
}

Upvotes: 0

Okomikeruko
Okomikeruko

Reputation: 1173

In regards to your issue with the touch screen, I would recommend using Unity's UI button system. It works very well with both Android and iOS platforms. I recommend watching these videos to understand how to write a function that the interface will call for you: https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button

On another note, running GetComponent<>() every frame is incredibly taxing on a mobile system.

I recommend the following change:

using UnityEngine;
using UnityEngine.UI;

public class RPB : MonoBehaviour {

  public Image LoadingBar;
  public Text TextIndicator;
  // other variables redacted //

  void Start () {
    LoadingBar = GetComponent<Image>();
    TextIndicator = GetComponent<Text>();
  }

  void Update () {
    // other functions redacted //

    LoadingBar.fillAmount = currentAmount / 100;
    TextIndicator.text = ((int)numSec).ToString () + "s";
  }

  public void TouchFunction() {
    // Do the thing here. //
  }
}

Upvotes: 1

Related Questions