对于unity中控制动画暂停的问题,一般是用AnimationState 控制播放状态,比如 AnimationState.speed 控制播放速度,好了,废话不多说,现在来用代码实例来说明问题。
- using UnityEngine;using System.Collections;
- public class MusicControlScript : MonoBehaviour {
- public AudioSource music;
- public float musicVolume;
- public Rect windowRect = new Rect(700, 20, 90, 225);
- bool showGUI;
- void Start() {
- showGUI=false;
- musicVolume = 1.0F;
- }
-
- void OnGUI() {
- if(GUI.Button(new Rect(Screen.width-60,5,50,50),"Music"))
- {
- showGUI=true;
- }
- if(showGUI)
- windowRect = GUI.Window(0, windowRect, DoMyWindow, "Music");
- }
- void DoMyWindow(int windowID){
- if(GUI.Button(new Rect(75,5,10,10),""))
- {
- showGUI=false;
- }
- if (GUI.Button(new Rect(5, 30, 45, 45), "Play")) {
- if (!music.isPlaying){
- music.Play();//播放
- }
- }
- if (GUI.Button(new Rect(5, 85, 45, 45), "Stop")) {
- if (music.isPlaying){
- music.Stop();//停止
- }
- }
- if (GUI.Button(new Rect(5, 140, 45, 45), "Pause")) {
- if (music.isPlaying){
- music.Pause();//暂停
- }
- }
- musicVolume = GUI.VerticalSlider (new Rect(65, 30, 30, 150), musicVolume, 0.0F, 1.0F);//声音调节
- GUI.Label(new Rect(5, 190, 90, 20), "Voice: " + (int)(musicVolume * 100) + "%");
- if (music.isPlaying){
- music.volume = musicVolume;
- }
- GUI.DragWindow(new Rect(0, 0, 100, 30));
- }
- }