2016-08-28 3 views
2

私は単一性に慣れていないし、ゲームを構築しているので、ボタンのコールバック機能が2回使用されるという問題があります。ボタンが押されているときは表示されません。押されたときにボタンを繰り返し呼び出させるにはどうすればよいですか?ボタンが押されているときにコードを実行する

コード

public void IncreaseBPM() 
{ 
    if (speed < 12) 
    { 
     speed += 0.05f; 
     bpmText.GetComponent<BeatTextControl>().beats += 1; 
     PlayerPrefs.SetFloat ("savedBPM", speed); 
    } 
} 

public void DecreaseBPM() 
{ 
    if (speed > 1.5) 
    { 
     speed -= 0.05f; 
     bpmText.GetComponent<BeatTextControl>().beats -= 1; 
     PlayerPrefs.SetFloat ("savedBPM", speed); 
    } 
} 
+0

ボタンが押されているかどうかをチェックするループに入れますか? –

答えて

0

いいので、まずあなたが2つのブール値を作成:

Void update(){ 
    If(increase){ IncreaseBPM(); } 
    Else if(decrease){ DecreaseBPM(); } 
} 

bool increase = false; 
bool decrease = false; 

を、あなたはあなたがチェックし、同じファイル内の更新機能に続いて関数内

public void WhenButtonClicked() 
    { increase = true; } 
public void WhenOtherButtonClicked() 
    { decrease = true; } 

それらを設定します

ボタンが解放されたとき、私は簡単な答えを見つけました: http://answers.unity3d.com/questions/843319/46-gui-button-onrelease.html

(イベントメニューの)ボタンにイベントトリガーコンポーネントを追加します。 そこから、OnPointerUpのリスナーを追加できます。 OnClickと同じだけ としてください。

さらに、増減をfalseに設定する2つの関数を作成します。

+0

カント私はちょうど更新を選択し、ポインタを使用して.. .. ?? –

1

ユニティのButtonコンポーネントは、この機能は、あなたがButtonとしてImageコンポーネントを使用して独自に展開する必要があります。でないを構築していません。 IPointerDownHandlerIPointerUpHandlerインターフェイスを実装し、OnPointerDownOnPointerUp関数をオーバーライドします。

OnPointerDownが呼び出されると、そのオブジェクトを格納するためにstructを使用/ボリュームImageListでクリックし、現在のクリックpointerIdされます。 Update機能でクリックされたImageを確認できます。

OnPointerUpが呼び出された場合は、そのpointerIdListに存在するかどうかを確認し、それがない場合は、それを削除し、その後解放されたpointerId確認する必要があります。

私はこれを行うには前に行った。

public class ButtonTest : MonoBehaviour 
{ 
    // Use this for initialization 
    void Start() 
    { 
     //Register to Button events 
     ButtonDownRelease.OnButtonActionChanged += ButtonActionChange; 
     Debug.Log("Registered!"); 
    } 

    // Update is called once per frame 
    void Update() 
    { 

    } 

    //Un-Register to Button events 
    void OnDisable() 
    { 
     ButtonDownRelease.OnButtonActionChanged -= ButtonActionChange; 
    } 


    //Will be called when there is a Button action 
    void ButtonActionChange(ButtonAction buttonAction) 
    { 
     //Check for held down 
     if (buttonAction == ButtonAction.DecreaseButtonDown) 
     { 
      Debug.Log("Decrease Button held Down!"); 
      DecreaseBPM(); 
     } 

     if (buttonAction == ButtonAction.IncreaseButtonDown) 
     { 
      Debug.Log("Increase Button held Down!"); 
      IncreaseBPM(); 
     } 

     //Check for release 
     if (buttonAction == ButtonAction.DecreaseButtonUp) 
     { 
      Debug.Log("Decrease Button Released!"); 
     } 

     if (buttonAction == ButtonAction.IncreaseButtonUp) 
     { 
      Debug.Log("Increase Button Released!"); 
     } 
    } 

    private void IncreaseBPM() 
    { 
     if (TempoController.GetComponent<Pendulum>().speed < 12) 
     { 
      TempoController.GetComponent<Pendulum>().speed += 0.05f; 
     } 
    } 

    private void DecreaseBPM() 
    { 
     if (TempoController.GetComponent<Pendulum>().speed > 1.5) 
     { 
      TempoController.GetComponent<Pendulum>().speed -= 0.05f; 
     } 
    } 
} 

読む慎重:以下は、あなたの質問に新たなスクリプトがあります。

ButtonDownReleaseという名前の新しいスクリプトを作成し、それに以下のコードを挿入します。 ButtonDownReleaseスクリプトを/Buttons UI GameObjectsの親であるCanvasCanvasに貼り付けます。 Images(増加と減少)を2つ作成します。 increasedecreaseと呼ばれる2つのtagsを作成し、両方ともImagesを右tagとします。

あなたはまだあなたがImageコンポーネントを使用しない場合は、この作品を作るために代わりImageButtonを使用することができます。それぞれButtonを選択し、タグをincreasedecreaseに変更し、の下にあるTextコンポーネントを選択し、タグincreasedecreaseも変更します。 このスクリプトでButtonコンポーネントを使用する場合は、ButtonTextタグも変更する必要があります。また、のように、Canvasではなく、をそれぞれButtonに添付する必要があります。

あなたButtonDownReleaseスクリプト:あなたが唯一のこれを行うにはboolean変数を使用する場合

using UnityEngine; 
using System.Collections; 
using UnityEngine.EventSystems; 
using UnityEngine.UI; 
using System.Collections.Generic; 

public class ButtonDownRelease : MonoBehaviour, IPointerDownHandler, IPointerUpHandler 
{ 
    List<ButtonInfo> buttonInfo = new List<ButtonInfo>(); 

    public delegate void ButtonActionChange(ButtonAction buttonAction); 
    public static event ButtonActionChange OnButtonActionChanged; 

    // Update is called once per frame 
    void Update() 
    { 
     //Send Held Button Down events 
     for (int i = 0; i < buttonInfo.Count; i++) 
     { 
      if (buttonInfo[i].buttonPresed == ButtonAction.DecreaseButtonDown) 
      { 
       dispatchEvent(ButtonAction.DecreaseButtonDown); 
      } 

      else if (buttonInfo[i].buttonPresed == ButtonAction.IncreaseButtonDown) 
      { 
       dispatchEvent(ButtonAction.IncreaseButtonDown); 
      } 
     } 
    } 

    void dispatchEvent(ButtonAction btAction) 
    { 
     if (OnButtonActionChanged != null) 
     { 
      OnButtonActionChanged(btAction); 
     } 
    } 

    public void OnPointerDown(PointerEventData eventData) 
    { 
     //Debug.Log("Button Down!"); 
     ButtonInfo bInfo = new ButtonInfo(); 
     bInfo.uniqueId = eventData.pointerId; 
     if (eventData.pointerCurrentRaycast.gameObject.CompareTag("increase")) 
     { 
      bInfo.buttonPresed = ButtonAction.IncreaseButtonDown; 
      addButton(bInfo); 
     } 
     else if (eventData.pointerCurrentRaycast.gameObject.CompareTag("decrease")) 
     { 
      bInfo.buttonPresed = ButtonAction.DecreaseButtonDown; 
      addButton(bInfo); 
     } 
    } 

    public void OnPointerUp(PointerEventData eventData) 
    { 
     //Debug.Log("Button Down!" + eventData.pointerCurrentRaycast); 
     removeButton(eventData.pointerId); 
    } 

    void addButton(ButtonInfo bInfo) 
    { 
     buttonInfo.Add(bInfo); 
    } 

    void removeButton(int unqID) 
    { 
     for (int i = 0; i < buttonInfo.Count; i++) 
     { 
      if (unqID == buttonInfo[i].uniqueId) 
      { 
       //Send Release Button Up events 
       if (buttonInfo[i].buttonPresed == ButtonAction.DecreaseButtonDown) 
       { 
        dispatchEvent(ButtonAction.DecreaseButtonUp); 
       } 

       else if (buttonInfo[i].buttonPresed == ButtonAction.IncreaseButtonDown) 
       { 
        dispatchEvent(ButtonAction.IncreaseButtonUp); 
       } 
       buttonInfo.RemoveAt(i); 
      } 
     } 

    } 

    public struct ButtonInfo 
    { 
     public int uniqueId; 
     public ButtonAction buttonPresed; 
    } 
} 

public enum ButtonAction 
{ 
    None, 
    IncreaseButtonDown, IncreaseButtonUp, 
    DecreaseButtonDown, DecreaseButtonUp 
} 

最後に、あなたが問題に実行されます。モバイルデバイスのバグを避けるために、この回答に記載されているスクリプトのように、これをpointerIdで行う必要があります。このバグの良い例は、Imageをクリックして別のGameObjectで指を離したときに、にぶつかり、OnPointerUpが呼び出されないためです。また、モバイルデバイスでマルチタッチを使用すると問題が発生します。

関連する問題