2017-09-22 112 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