unity 3D音乐播放器
生活随笔
收集整理的這篇文章主要介紹了
unity 3D音乐播放器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
using UnityEngine;
using System.Collections;
? ? //中文字體
? ? public GUISkin ChineseSkin;
? ? //音頻文件
? ? public AudioSource[] MyAudio;
? ? //歌曲名稱
? ? public string[] SongNames;
? ? //當前歌曲編號
? ? public string[] SongerNames;
? ? //當前歌曲編號
? ? int SongIndex = 0;
? ? //設定是否循環
? ? bool LoopPlay = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
? ? void OnGUI()
? ? {
? ? ? ? GUI.skin = ChineseSkin;
? ? ? ? if (GUI.Button(new Rect(10,10,50,30),"開始"))
? ? ? ? {
? ? ? ? ? ? MyAudio[SongIndex].Play();
? ? ? ? }
? ? ? ? ?if (GUI.Button(new Rect(70,10,50,30),"暫停"))
? ? ? ? {
? ? ? ? ? ? MyAudio[SongIndex].Pause();
? ? ? ? }
? ? ? ? ?if (GUI.Button(new Rect(130,10,50,30),"停止"))
? ? ? ? {
? ? ? ? ? ? MyAudio[SongIndex].Stop();
? ? ? ? }
? ? ? ? ?if (GUI.Button(new Rect(190,10,60,30),"上一首"))
? ? ? ? {
? ? ? ? ? ? ///方法一: 停止播放當前音樂
? ? ? ? ? ? //for (int i = 0; i < MyAudio.Length; i++)
? ? ? ? ? ? //{
? ? ? ? ? ? // ? ?if (MyAudio[i].isPlaying)
? ? ? ? ? ? // ? ?{
? ? ? ? ? ? // ? ? ? ?MyAudio[i].Stop();
? ? ? ? ? ? // ? ?}
? ? ? ? ? ? //}
? ? ? ? ? ? //方法二:停止當前播放音樂
? ? ? ? ? ? if (MyAudio[SongIndex].isPlaying)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MyAudio[SongIndex].Stop();
? ? ? ? ? ? }
? ? ? ? ? ? ?//上一曲
? ? ? ? ? ? if (SongIndex>0)
? ? ? ? ? ? ? ? SongIndex--; ? ? ? ? ? ?
? ? ? ? ? ? else if(LoopPlay)
? ? ? ? ? ? ? ? SongIndex = MyAudio.Length - 1;
? ? ? ? ? ? ?//播放當前音樂
? ? ? ? ? ? MyAudio[SongIndex].Play();
? ? ? ? }
? ? ? ? ?if (GUI.Button(new Rect(260, 10, 60, 30), "下一首"))
? ? ? ? ?{
? ? ? ? ? ? ?//方法一: 停止播放當前音樂
? ? ? ? ? ? ?//for (int i = 0; i < MyAudio.Length; i++)
? ? ? ? ? ? ?//{
? ? ? ? ? ? ?// ? ?if (MyAudio[i].isPlaying)
? ? ? ? ? ? ?// ? ?{
? ? ? ? ? ? ?// ? ? ? ?MyAudio[i].Stop();
? ? ? ? ? ? ?// ? ?}
? ? ? ? ? ? ?//}
? ? ? ? ? ? ?//方法二:停止當前播放音樂
? ? ? ? ? ? ?if (MyAudio[SongIndex].isPlaying)
? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ?MyAudio[SongIndex].Stop();
? ? ? ? ? ? ?}
? ? ? ? ? ? ?//下一曲
? ? ? ? ? ? ?if (SongIndex<MyAudio.Length-1)
? ? ? ? ? ? ? ? SongIndex++;
? ? ? ? ? ? ?else if (LoopPlay)
? ? ? ? ? ? ? ? SongIndex = 0;
? ? ? ? ? ? ?//播放當前音樂
? ? ? ? ? ? ?MyAudio[SongIndex].Play();
? ? ? ? ?}
? ? ? ? //選擇是否進行循環
? ? ? ? ?GUI.contentColor = Color.red;
? ? ? ? ?LoopPlay = GUI.Toggle(new Rect(10, 45, 80, 30), LoopPlay, "循環播放"); ? ? ? ??
? ? ? ? //顯示當前歌手和歌曲名稱
? ? ? ? ?GUI.Label(new Rect(10, 80, Screen.width, 30), "當前歌曲名稱:"+SongNames[SongIndex]);
? ? ? ? ?GUI.Label(new Rect(10, 115, Screen.width, 30), "歌手姓名"+SongerNames[SongIndex]);
? ? ? ? ?GUI.contentColor = Color.white;
? ? }
}
總結
以上是生活随笔為你收集整理的unity 3D音乐播放器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法,即剥削
- 下一篇: Symbian游戏编程入门 (一) Sy