2014-10-10 140 views
0

我是Unity新手,我試圖使用2個四方形按鈕和一個腳本來播放停止音頻文件。我搜索了互聯網,我還沒有找到針對我的問題的單一解決方案。使用2個單獨按鈕播放和停止音頻Unity

這是我的代碼

using UnityEngine; 
using System.Collections; 

public class PlayStop : MonoBehaviour { 
    public GameObject Button1; 
    public GameObject Button2; 
    public AudioClip Clip; 

    void Start() 
    { 
     Button1 = GameObject.Find ("Fa1Play"); 
     Button2 = GameObject.Find ("Fa1Stop"); 
    } 
    void OnMouseDown() 
    { 
     if (Button1.GetComponent ("Fa1Play")) 
     { 
      if (!audio.isPlaying) 
      { 
       audio.clip = Clip; 
       audio.Play(); 
      } 
     } 

     if (Button2.GetComponent("Fa1Stop")) 
     { 
      audio.Stop(); 
     } 


    } 
} 
+0

那你試過嗎? – Asenar 2014-10-10 20:20:41

+0

我嘗試過HARDLY:D,所以我沒有采取任何行動。我想通過按一個按鈕播放音頻,並用另一個按鈕或甚至使用相同的按鈕停止播放。我有一個帶有2個孩子Fa1Play和Fa1Stop的gameobject PlayStop。我有點糊塗:( – 2014-10-10 20:27:40

+0

,我想提前!!!!! – 2014-10-10 20:34:49

回答

0

你不需要遊戲對象認定只是給你的按鈕對撞機和標籤讓你暫停的暫停標籤和玩一玩標籤

 public class PlayStop : MonoBehaviour { 

      public AudioClip aclip; 
      private bool stop; 

      void Start() 
       { 
        audio.clip=aclip; 
        stop=true; 
       } 


    void Update() 
    { 
     if(Input.touches.Length == 1) 
     { 
     Touch touchedFinger = Input.touches[0]; 

     if(touchedFinger.phase==TouchPhase.Began){ 
     Ray aRay = Camera.mainCamera.ScreenPointToRay(touchedFinger.position); 
      RaycastHit hit; 
      if(Physics.Raycast(aRay.origin, aRay.direction, out hit, Mathf.Infinity)) 
       { 

          if(hit.collider.tag=="stop" && !stop) 
            { 
              audio.Stop(); 
              stop=!stop 
            } 
         if(hit.collider.tag=="play" && stop) 
            { 
              audio.Play(); 
              stop=!stop 
            } 

         } 
        } 
       } 
     } 
} 
+1

我將音頻分配給gameobject上的腳本,並將音頻源添加到gameobject並將標記更改爲小寫,並且我在AR相機上有一個音頻監聽器! – 2014-10-11 14:44:25

+0

http://www.4shared.com/download/i1K2_oOJba/Capture.JPG?lgfp=3000 – 2014-10-11 14:46:51

+0

把調試如果內(hit.collider.tag ==「一站式」 &&!停止),看看它被調用 – 2014-10-11 14:47:17

相關問題