unity如何长按鼠标左键,实现数字动态增加,每次增加0.1;松开鼠标左键,数字停止变化呢

IEnumerator UVZero() {
float x = float.Parse(objs2[0].GetComponent().text);
x += Time.deltaTime;
objs2[0].GetComponent().text = float.Parse(x.ToString("0.0")).ToString();
yield return null;
}
用了协程 但是数字每次增加不是0.1f

using UnityEngine;

public class Example : MonoBehaviour
{
    float x = 0f;

    private void Update()
    {
        //鼠标按住
        if (Input.GetMouseButton(0))
        {
            x += .1f;
        }
    }
}

在update函数里添加鼠标左键监听事件,以下是3种事件,按你的需求应该是需要GetMouseButtonDown和GetMouseButtonUp两个事件
// 按下鼠标左键
if (Input.GetMouseButtonDown(0)) {
Debug.Log ("你按下了鼠标左键");
}
// 按住鼠标左键
if (Input.GetMouseButton(0)) {
Debug.Log ("你按住了鼠标左键");
}
// 抬起鼠标左键
if (Input.GetMouseButtonUp(0)) {
Debug.Log ("你抬起了鼠标左键");
}

Time.deltaTime 是增量时间,大小为1/每秒帧数 ,不是固定的0.1f

望采纳,不懂可以私信问我!

img