unity3D竖版菜单界面,以及音效音乐的分别控制

来源:互联网 发布:计算机编程好学吗 编辑:程序博客网 时间:2024/05/05 10:10

//////////////2015/09/17///////////////

/////////////by      XBW///////////////

////////////环境 Unity4.6.1/////////



先上图看效果,UI并不好,就是粗略的打个框架

/////////////////////////////////////////////////////////////
//此菜单的思路
//
/////////////////////////////////////////////////////////////
//进入游戏,游戏介绍,退出游戏没什么特别的,只是用了按钮事件,
//来说一下顶部的菜单栏,这个呢可以点击右箭头使几个按钮显示出来,类似抽屉一样
//实现思路:点击右箭头,向右依次显示出几个按钮,同时右箭头变为左箭头
//右上角的声音控制,点一下为整个游戏静音,这是两张声音UI
//一张有音量的,一张没有音量的,在设置中,当音乐与音效都不为关闭时,右上角显示有音量图,
//反之,显示没有音量的图;
//点击设置以及提示按钮,弹出菜单凸显出来,背景变暗,背景上的按钮失效
//实现思路:当点击设置按钮时,我们在GUI函数最顶端显示一张全屏的按钮,同时使用皮肤,
//皮肤的按钮按下与悬停的图案删除,这样我们点击时就不会闪烁了,同时这个按钮没有命令
//发现GUI的覆盖关系有点乱,可能是自己不够理解,之前把全屏按钮写在了GUI函数的末尾,
//结果背景按钮仍然起作用,后来移到最顶端反而使背景按钮失效,不知道谁遮挡了谁;
//设置菜单里的声音的开关也是,什么时候显示off跟on很清楚了;
//再就是这些音乐与音效的分离,
//实现思路:音效呢就是点击的声音片段,我用了AudioClip,
//然后初始化了AudioSource控件,定义一个音效的控件
//public AudioSource music;这个music用来控制音效的,
//因为两个音效是用声音片段AudioClip定义的,我并没有查到他的播放与暂停,
//所以定义了一个声音资源,来总的控制,
//music.PlayOneShot(beep);这样点击发出音效;
//当music.volume = 0;时,音效声音为0,
//实现了音效的关闭,
//而音乐呢,直接拖到了AudioSource的AudioClip上,
//这样呢,当audio.Stop();时,背景音乐停止,放弃了之前的GetComponent<AudioSource>().mute = true;
//这样已静音音乐音效都不响了,没法区分开了;
//这次呢,还发现了用皮肤使按钮透明,之前一直用的办法是用贴图遮挡住按钮,每次调整按钮与贴图的位置真是费劲;
//现在好了,把按钮放到合适的位置,不至于完好对齐,用皮肤使按钮透明就好了;
//////////////////////////////////////////////////////////////



看一下代码吧

///////////////////////////////////////////////////////////////此菜单的思路/////////////////////////////////////////////////////////////////进入游戏,游戏介绍,退出游戏没什么特别的,只是用了按钮事件,//来说一下顶部的菜单栏,这个呢可以点击右箭头使几个按钮显示出来,类似抽屉一样//实现思路:点击右箭头,向右依次显示出几个按钮,同时右箭头变为左箭头//右上角的声音控制,点一下为整个游戏静音,这是两张声音UI//一张有音量的,一张没有音量的,在设置中,当音乐与音效都不为关闭时,右上角显示有音量图,//反之,显示没有音量的图;//点击设置以及提示按钮,弹出菜单凸显出来,背景变暗,背景上的按钮失效//实现思路:当点击设置按钮时,我们在GUI函数最顶端显示一张全屏的按钮,同时使用皮肤,//皮肤的按钮按下与悬停的图案删除,这样我们点击时就不会闪烁了,同时这个按钮没有命令//发现GUI的覆盖关系有点乱,可能是自己不够理解,之前把全屏按钮写在了GUI函数的末尾,//结果背景按钮仍然起作用,后来移到最顶端反而使背景按钮失效,不知道谁遮挡了谁;//设置菜单里的声音的开关也是,什么时候显示off跟on很清楚了;//再就是这些音乐与音效的分离,//实现思路:音效呢就是点击的声音片段,我用了AudioClip,//然后初始化了AudioSource控件,定义一个音效的控件//public AudioSource music;这个music用来控制音效的,//因为两个音效是用声音片段AudioClip定义的,我并没有查到他的播放与暂停,//所以定义了一个声音资源,来总的控制,//music.PlayOneShot(beep);这样点击发出音效;//当music.volume = 0;时,音效声音为0,//实现了音效的关闭,//而音乐呢,直接拖到了AudioSource的AudioClip上,//这样呢,当audio.Stop();时,背景音乐停止,放弃了之前的GetComponent<AudioSource>().mute = true;//这样已静音音乐音效都不响了,没法区分开了;//这次呢,还发现了用皮肤使按钮透明,之前一直用的办法是用贴图遮挡住按钮,每次调整按钮与贴图的位置真是费劲;//现在好了,把按钮放到合适的位置,不至于完好对齐,用皮肤使按钮透明就好了;//////////////////////////////////////////////////////////////using UnityEngine;using System.Collections;public class Menu : MonoBehaviour {    public AudioClip beep;    public AudioClip beep1;    public AudioSource music=null;    public int open = 1;    public int kai = 1;    public int kai1 = 1;    public int kai2 = 1;    public int kai3 = 1;    public int kai4 = 1;    public GUISkin GUIskin;    public GUISkin GUIskin1;    public Texture2D img;    public Texture2D bt1;    public Texture2D bt2;    public Texture2D bt3;    public Texture2D p1;    public Texture2D p2;    public Texture2D p3;    public Texture2D p4;    public Texture2D p5;    public Texture2D p6;    public Texture2D p7;    public Texture2D p8;//问号提示    public Texture2D p9;//设置    public Texture2D p10;//音效开关// Use this for initializationvoid Start () {        music = gameObject.AddComponent<AudioSource>();}// Update is called once per framevoid Update () {}    void OnGUI()    {        GUIStyle backGround = new GUIStyle();        backGround.normal.background = img;        GUI.Label(new Rect(0, 0, Screen.width, Screen.height), "", backGround);                //GUIStyle go = new GUIStyle();        //go.fontSize = 35;        //go.normal.textColor = new Color(255, 255, 255);        if (open == 0 && kai1 == 0)        {            GUI.skin = GUIskin;            if (GUI.Button(new Rect(Screen.width * 0.36f, Screen.height * 0.53f, Screen.width * 0.24f, Screen.height * 0.07f), ""))            {                music.PlayOneShot(beep1);                kai1 = 1;            }            if (GUI.Button(new Rect(Screen.width * 0.73f, Screen.height * 0.28f, Screen.width * 0.1f, Screen.height * 0.05f), ""))            {                music.PlayOneShot(beep1);                kai1 = 1;            }            GUI.skin = GUIskin1;            if (GUI.Button(new Rect(0, 0, Screen.width, Screen.height), ""))            {            }            GUI.DrawTexture(new Rect(Screen.width * 0.08f, 0, Screen.width * 0.8f, Screen.height * 0.8f), p8);                    }        if (open == 0 && kai2 == 0)        {            GUI.skin = GUIskin;            if (GUI.Button(new Rect(Screen.width * 0.36f, Screen.height * 0.53f, Screen.width * 0.24f, Screen.height * 0.07f), ""))            {                music.PlayOneShot(beep1);                kai2 = 1;            }            if (GUI.Button(new Rect(Screen.width * 0.73f, Screen.height * 0.28f, Screen.width * 0.1f, Screen.height * 0.05f), ""))            {                music.PlayOneShot(beep1);                kai2 = 1;            }            if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.355f, Screen.width * 0.266f, Screen.height * 0.07f), ""))            {                music.PlayOneShot(beep1);                if (kai3 == 0)                {                    audio.Stop();                    kai3 = 1;                }                                    else                {                    audio.Play();                    kai3 = 0;                }                                }            if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.43f, Screen.width * 0.266f, Screen.height * 0.07f), ""))            {                music.PlayOneShot(beep1);                if (kai4 == 0)                {                    music.volume = 0;                    kai4 = 1;                }                                    else                {                    music.volume = 1;                    kai4 = 0;                }                            }                      GUI.skin = GUIskin1;            if (GUI.Button(new Rect(0, 0, Screen.width, Screen.height), ""))            {            }            GUI.DrawTexture(new Rect(Screen.width * 0.08f, 0, Screen.width * 0.8f, Screen.height * 0.8f), p9);                  }        GUI.skin = GUIskin;        if (GUI.Button(new Rect(Screen.width * 0.29f, Screen.height * 0.605f, Screen.width * 0.365f, Screen.height * 0.08f), ""))        {            music.PlayOneShot(beep);            //Application.LoadLevel("Loading");        }        GUI.DrawTexture(new Rect(Screen.width * 0.27f, Screen.height * 0.6f, Screen.width * 0.4f, Screen.height * 0.1f), bt1);        //GUI.Label(new Rect(Screen.width * 0.47f, Screen.height * 0.6f, Screen.width * 0.2f, Screen.height * 0.1f), "进入游戏", go);        if (GUI.Button(new Rect(Screen.width * 0.29f, Screen.height * 0.705f, Screen.width * 0.365f, Screen.height * 0.08f), ""))        {            music.PlayOneShot(beep);            kai1 = 0;            open = 0;            //print("111");        }        GUI.DrawTexture(new Rect(Screen.width * 0.27f, Screen.height * 0.7f, Screen.width * 0.4f, Screen.height * 0.1f), bt2);        if (GUI.Button(new Rect(Screen.width * 0.29f, Screen.height * 0.805f, Screen.width * 0.365f, Screen.height * 0.08f), ""))        {            music.PlayOneShot(beep);            Application.Quit();        }        GUI.DrawTexture(new Rect(Screen.width * 0.27f, Screen.height * 0.8f, Screen.width * 0.4f, Screen.height * 0.1f), bt3);        GUI.skin = GUIskin;        //GUI.Label(new Rect(Screen.width * 0.3f, Screen.height * 0.8f, 100, 100), "Loading.....", go);        if (open == 1)        {            if (GUI.Button(new Rect(0, 0, Screen.width * 0.15f, Screen.width * 0.15f), ""))            {                music.PlayOneShot(beep1);                open = 0;            }            GUI.DrawTexture(new Rect(0, 0, Screen.width * 0.15f, Screen.width * 0.15f), p1);        }        if (open == 0)        {            if (GUI.Button(new Rect(0, 0, Screen.width * 0.15f, Screen.width * 0.15f), ""))            {                music.PlayOneShot(beep1);                open = 1;                kai1 = 1;            }            GUI.DrawTexture(new Rect(0, 0, Screen.width * 0.15f, Screen.width * 0.15f), p2);            if (GUI.Button(new Rect(Screen.width * 0.15f, 0, Screen.width * 0.15f, Screen.width * 0.15f), ""))            {                music.PlayOneShot(beep1);                kai2 = 0;            }            GUI.DrawTexture(new Rect(Screen.width * 0.15f, 0, Screen.width * 0.15f, Screen.width * 0.15f), p3);            if (GUI.Button(new Rect(Screen.width * 0.3f, 0, Screen.width * 0.15f, Screen.width * 0.15f), ""))            {                music.PlayOneShot(beep1);                kai1 = 0;                                            }            GUI.DrawTexture(new Rect(Screen.width * 0.3f, 0, Screen.width * 0.15f, Screen.width * 0.15f), p4);            if (GUI.Button(new Rect(Screen.width * 0.45f, 0, Screen.width * 0.15f, Screen.width * 0.15f), ""))            {                music.PlayOneShot(beep1);                Application.Quit();            }            GUI.DrawTexture(new Rect(Screen.width * 0.45f, 0, Screen.width * 0.15f, Screen.width * 0.15f), p5);        }        if(kai3+kai4!=2)        {            kai = 1;        }        if(kai==1)        {            if (GUI.Button(new Rect(Screen.width * 0.85f, 0, Screen.width * 0.15f, Screen.width * 0.15f), ""))            {                music.PlayOneShot(beep1);                                kai = 0;                //GetComponent<AudioSource>().mute = true;                audio.Stop();                music.volume = 0;                kai3 = 1;                kai4 = 1;                //audio.Stop();            }            GUI.DrawTexture(new Rect(Screen.width * 0.85f, 0, Screen.width * 0.15f, Screen.width * 0.15f), p6);        }        if (kai == 0 || (kai3 == 1 && kai4 == 1))        {            if (GUI.Button(new Rect(Screen.width * 0.85f, 0, Screen.width * 0.15f, Screen.width * 0.15f), ""))            {                music.PlayOneShot(beep1);                kai = 1;                //GetComponent<AudioSource>().mute = false;                audio.Play();                music.volume = 1;                kai3 = 0;                kai4 = 0;                //audio.Play();            }            //if(kai3!=0||kai4!=0)            GUI.DrawTexture(new Rect(Screen.width * 0.85f, 0, Screen.width * 0.15f, Screen.width * 0.15f), p7);        }        if (open == 0 && kai2 == 0&&kai3 == 0)        {            GUI.DrawTexture(new Rect(Screen.width * 0.45f, Screen.height * 0.355f, Screen.width * 0.266f, Screen.height * 0.07f), p10);        }        if (open == 0 && kai2 == 0&&kai4 == 0)        {            GUI.DrawTexture(new Rect(Screen.width * 0.45f, Screen.height * 0.43f, Screen.width * 0.266f, Screen.height * 0.07f), p10);        }    }}


就是一些开关逻辑的运用,思路清晰就没问题,注意一些细节就好了,
互相交流学习,有错误请指正,有更好的实现方法请留言,谢谢啦



0 0
原创粉丝点击