您可能需要查看 Selectable 事件系统。您可以实现 OnPonterDown 和 OnPointerUp 以便第一个设置触发,第二个停止它:
using UnityEngine.EventSystems;
public class Test : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
[SerializeField]private InputField userField;
#region IPointerUpHandler implementation
private bool running =false;
public void OnPointerUp (PointerEventData eventData)
{
running = false;
if(timer < holdTime){ Tap(); }
timer = 0.0f;
}
#endregion
#region IPointerDownHandler implementation
public void OnPointerDown (PointerEventData eventData)
{
running = true;
}
#endregion
float timer = 0.0f;
float holdTime = 0.25f;
// Update is called once per frame
void FixedUpdate ()
{
if(running){
timer += Time.deltaTime;
if(timer > holdTime)
BackSpace();}
}
private void BackSpace() {
string textEnter = userField.text;
string tempString = textEnter.Substring(0, textEnter.Length - 1);
userField.text = tempString;
}
}
固定更新使其在不同设备上保持一致。您可以设置自己的更新系统来控制频率。