2017-09-22 117 views
0

我在Unity统一运行,只要一个按钮在检查按功能

新手使用Unity检查我有设置一个按钮,使一个回调函数(的OnClick),它工作正常,但只有一次,再次发起行动,我需要释放并再次点击按钮

我怎样才能让功能一直运行,只要按下按钮一遍又一遍? (如机枪)

public void MoveLeft () 
{ 
    transform.RotateAround(Camera.main.transform.position, Vector3.up, -rotation/4 * Time.deltaTime); 
    infopanel.RotateAround(Camera.main.transform.position, Vector3.up, -rotation/4 * Time.deltaTime); 
} 

问候......

+0

我的答案解决了您的问题吗? – Programmer

回答

1

OnClick无法做到这一点。使用OnPointerDownOnPointerUp。在这些功能的布尔变量设置为真/假分别然后检查布尔变量在Update功能

附加到UI按钮对象:

public class UIPresser : MonoBehaviour, IPointerDownHandler, 
    IPointerUpHandler 
{ 
    bool pressed = false; 

    public void OnPointerDown(PointerEventData eventData) 
    { 
     pressed = true; 
    } 

    public void OnPointerUp(PointerEventData eventData) 
    { 
     pressed = false; 
    } 

    void Update() 
    { 
     if (pressed) 
      MoveLeft(); 
    } 

    public void MoveLeft() 
    { 
     transform.RotateAround(Camera.main.transform.position, Vector3.up, -rotation/4 * Time.deltaTime); 
     infopanel.RotateAround(Camera.main.transform.position, Vector3.up, -rotation/4 * Time.deltaTime); 
    } 
} 

你可以找到其他事件功能here