简体   繁体   English

带有UI图像的Unity触摸式运行代码-C#

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

I'm a new user to Unity3D and need a hand. 我是Unity3D的新用户,需要帮助。

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. 我有一些代码,希望在按下UI图像时激活这些代码,并在释放按键后结束代码。 The game is going to run on both Android and IOS so needs to be supported by both platforms. 该游戏将同时在Android和IOS上运行,因此需要两个平台都支持。

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 谢谢

In regards to your issue with the touch screen, I would recommend using Unity's UI button system. 关于您的触摸屏问题,我建议您使用Unity的UI按钮系统。 It works very well with both Android and iOS platforms. 它在Android和iOS平台上均能很好地工作。 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 我建议观看以下视频,以了解如何编写接口将要求您使用的功能: 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. 另一个需要注意的是,在移动系统上每帧运行GetComponent<>()非常大。

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. //
  }
}

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 我有一些要在按下UI图像时激活的代码,并在释放按键后结束

I would have have advised you to use the Button component too but it doesn't have click down and up events. 我本来建议您也使用Button组件,但是它没有单击和向上事件。 It only has onClick event which means you can only detect when a there is a Button click not when pressed and released. 它仅具有onClick事件,这意味着您只能检测何时有按钮单击, 而不能在按下和释放时检测到。

This can be done with the Image component. 这可以通过Image组件完成。 Implement IPointerDownHandler and IPointerUpHandler then override the OnPointerDown and OnPointerUp functions. 实现IPointerDownHandlerIPointerUpHandler然后重写OnPointerDownOnPointerUp函数。 These functions will be called when there is a mouse click and release on your Image . 当在Image上单击并释放鼠标时,将调用这些功能。 Attach the script to the Image and that should do the job assuming that the current code you have is logically correct. 将脚本附加到Image并假设您拥有的当前代码在逻辑上正确,该脚本就可以完成此工作。

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";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM