对于unity中控制动画暂停的问题,一般是用AnimationState 控制播放状态,比如 AnimationState.speed 控制播放速度,好了,废话不多说,现在来用代码实例来说明问题。


  1.  using UnityEngine;using System.Collections;
  2.  public class MusicControlScript : MonoBehaviour {
  3.  public AudioSource music;
  4.  public float musicVolume;
  5.  public Rect windowRect = new Rect(700, 20, 90, 225);
  6.  bool showGUI;
  7.  void Start() {
  8.   showGUI=false;
  9.   musicVolume = 1.0F;
  10.  }

  11. void OnGUI() {
  12.   if(GUI.Button(new Rect(Screen.width-60,5,50,50),"Music"))
  13.   {
  14.    showGUI=true;
  15.   }
  16.   if(showGUI)
  17.    windowRect = GUI.Window(0, windowRect, DoMyWindow, "Music");
  18.  }
  19.  void DoMyWindow(int windowID){ 
  20.   if(GUI.Button(new Rect(75,5,10,10),""))
  21.   {
  22.    showGUI=false;
  23.   }
  24.   if (GUI.Button(new Rect(5, 30, 45, 45), "Play")) {
  25.    if (!music.isPlaying){
  26.     music.Play();//播放
  27.    }
  28.   }
  29.   if (GUI.Button(new Rect(5, 85, 45, 45), "Stop")) {
  30.    if (music.isPlaying){ 
  31.     music.Stop();//停止
  32.    }
  33.   }
  34.   if (GUI.Button(new Rect(5, 140, 45, 45), "Pause")) {
  35.    if (music.isPlaying){
  36.     music.Pause();//暂停
  37.    }
  38.   }
  39.   musicVolume = GUI.VerticalSlider (new Rect(65, 30, 30, 150), musicVolume, 0.0F, 1.0F);//声音调节
  40.   GUI.Label(new Rect(5, 190, 90, 20), "Voice: " + (int)(musicVolume * 100) + "%");
  41.   if (music.isPlaying){
  42.    music.volume = musicVolume; 
  43.   }
  44.   GUI.DragWindow(new Rect(0, 0, 100, 30));
  45.  }
  46. }