Unity 3D功能主界面C#脚本

来源:互联网 发布:梦幻2卖号网络异常 编辑:程序博客网 时间:2024/06/15 11:00

实现点击按钮进入该功能场景中,点击音乐播放器自动弹出window窗体,并实现音乐的播放和停止的切换、音量大小滚动条、音乐播放上一曲和下一曲的切换。

using UnityEngine;

using System.Collections;

 

public class HomePage : MonoBehaviour {

   //功能按钮

   public Texture2D angryBird;

   public Texture2D musicBtn;

   public Texture2D DrawBool;

   public Texture2D vediobtn;

   public Texture2D quitbtn;

   //歌手图片

   public Texture[] photos;

   //定义播放器按钮点击次数

   protected int i = 0;

   //定义是否点击音频播放器按钮,显示音乐播放器winform

   public bool result=false;

   //音乐播放器功能按钮

   public Texture2D musicVolumeSize;

   public Texture2D MusicStartorStop;

   public Texture2D MusicStart;

   public Texture2D MusicStop;

   public Texture2D MusicPause;

   public Texture2D MusicUp;

   public Texture2D MusicNext;

   public Texture2D MusicLoop;

   //音量

   public float musicVolume;

   //中文字体

   public GUISkin ChineseSkin;

   //音频文件

   public AudioSource[] MyAudio;

   //歌曲名称

   public string[] SongNames;

   //当前歌曲编号

   public string[] SongerNames;

   //当前歌曲编号

   int SongIndex = 0;

   //设定是否循环

   bool LoopPlay = true;

         //Use this for initialization

         voidStart () {

             //设置默认音量

       musicVolume = 0.5F;

         }

        

         //Update is called once per frame

         voidUpdate () {

       

         }

   void OnGUI() {

      

       //游戏按钮

       GUI.TextArea(new Rect(Screen.width * 0.13f, Screen.height * 0.15f,Screen.width * 0.12f, Screen.height * 0.04f), "奋斗的小鸟");

       if (GUI.Button(new Rect(Screen.width * 0.1f, Screen.height * 0.2f,Screen.width * 0.18f, Screen.height * 0.18f), angryBird))

       {

           Application.LoadLevel("welcome");

       }

       //播放器按钮

       GUI.TextArea(new Rect(Screen.width * 0.33f, Screen.height * 0.15f,Screen.width * 0.12f, Screen.height * 0.04f), "音频播放器");

       if (GUI.Button(new Rect(Screen.width * 0.3f, Screen.height * 0.2f,Screen.width * 0.18f, Screen.height * 0.18f), musicBtn))

       {

           result = true;

           //Application.LoadLevel("vedio");

       }

       //window播放器出现

       if (result==true)

       {

            GUI.Window(0, new Rect(Screen.width *0.3f, Screen.height * 0.4f, Screen.width * 0.37f, Screen.height * 0.57f),function1, "我的音乐盒");

           

       }

       //涂鸦按钮

       GUI.TextArea(new Rect(Screen.width * 0.73f, Screen.height * 0.15f, Screen.width* 0.12f, Screen.height * 0.04f), "涂鸦工具");

       if (GUI.Button(new Rect(Screen.width * 0.7f, Screen.height * 0.2f,Screen.width * 0.18f, Screen.height * 0.18f), DrawBool))

       {

           Application.LoadLevel("DrawTool");

       }

        //视频播放器按钮

       GUI.TextArea(new Rect(Screen.width * 0.53f, Screen.height * 0.15f,Screen.width * 0.12f, Screen.height * 0.04f), "视频播放器");

       if (GUI.Button(new Rect(Screen.width * 0.5f, Screen.height * 0.2f,Screen.width * 0.18f, Screen.height * 0.18f), vediobtn))

       {

           Application.LoadLevel("vedioPlayer");

       }

       //返回按钮

       //GUI.TextArea(new Rect(Screen.width * 0.84f, Screen.height * 0.3f,Screen.width * 0.05f, Screen.height * 0.04f), "返回");

       if (GUI.Button(new Rect(Screen.width * 0.94f, Screen.height * 0.05f,Screen.width * 0.05f, Screen.height * 0.08f), quitbtn))

       {           

           Application.LoadLevel("Background");

       }

            

    }

  

   void function1(int mywindow){

       GUI.skin = ChineseSkin;

       //创建一个用于动态修改音乐音量的横向滑动条

       musicVolume = GUI.VerticalScrollbar(new Rect(Screen.width * 0.04f,Screen.height * 0.21f, Screen.width * 0.18f, Screen.height * 0.18f),musicVolume, 0.01F, 1.0F, 0.0F);

       //音量图标

       GUI.Label(new Rect(Screen.width * 0.04f, Screen.height * 0.395f,Screen.width * 0.05f, Screen.height * 0.05f), musicVolumeSize);

       //将音量的百分比显示出来

       GUI.Label(new Rect(Screen.width * 0.005f, Screen.height * 0.16f,Screen.width * 0.3f, Screen.height * 0.05f), "当前音量:" +(int)(musicVolume * 100) + "%");

       if (MyAudio[SongIndex].isPlaying)

       {

           MyAudio[SongIndex].volume = musicVolume;

       }

       //歌手图片

       GUI.DrawTexture(new Rect(Screen.width * 0.14f, Screen.height * 0.04f,Screen.width * 0.225f, Screen.height * 0.455f), photos[SongIndex]);

       if (GUI.Button(new Rect(Screen.width * 0.08f, Screen.height * 0.5f,Screen.width * 0.05f, Screen.height * 0.05f), MusicStartorStop))

       {

           i++;

           if (i%2==1)

           {

                MyAudio[SongIndex].Play();

                MusicStartorStop = MusicPause;

           }

           else

           {

                MyAudio[SongIndex].Stop();

                MusicStartorStop =MusicStart;

           }                       

       }

       if (GUI.Button(new Rect(Screen.width * 0.02f, Screen.height * 0.5f,Screen.width * 0.05f, Screen.height * 0.05f), MusicUp))

       {

           ///方法一:停止播放当前音乐

           //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();

           }

           else

           {

                if (SongIndex > 0)

                    SongIndex--;

                else if (LoopPlay)

                    SongIndex = MyAudio.Length- 1;

                //播放当前音乐           

                MyAudio[SongIndex].Stop();

           }

                      

       }

       if (GUI.Button(new Rect(Screen.width * 0.14f, Screen.height * 0.5f,Screen.width * 0.05f, Screen.height * 0.05f), MusicNext))

       {

           //方法一:停止播放当前音乐

           //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();

           }

           else

           {

                //下一曲

                if (SongIndex < MyAudio.Length- 1)

                    SongIndex++;

                else if (LoopPlay)

                    SongIndex = 0;

                //播放当前音乐           

                MyAudio[SongIndex].Stop();

           }          

       }

       //选择是否进行循环

       GUI.contentColor = Color.white;       

      LoopPlay = GUI.Toggle(new Rect(Screen.width * 0.015f, Screen.height *0.45f, Screen.width * 0.05f, Screen.height * 0.05f), LoopPlay, MusicLoop);

       GUI.Label(new Rect(Screen.width * 0.065f, Screen.height * 0.46f,Screen.width * 0.1f, Screen.height * 0.1f),"循环");

       string[] mymusic = MyAudio[SongIndex].name.Split('-');

       //显示当前歌手和歌曲名称

       GUI.Label(new Rect(Screen.width * 0.04f, Screen.height * 0.06f,Screen.width * 0.1f, Screen.height * 0.2f), mymusic[0]);

       GUI.Label(new Rect(Screen.width * 0.03f, Screen.height * 0.1f,Screen.width * 0.1f, Screen.height * 0.2f), mymusic[1]);

       

    }

}

0 0
原创粉丝点击