zoukankan      html  css  js  c++  java
  • Unity3D之主菜单

    1.新建一个名为MainMenu的C#脚本,修改编码后拖动到主摄像机,并给主摄像机添加一个AudioSource声音源作为背景音乐。将音乐文件赋值给Audio Clip属性。

    2.创建一个CommonHelper类,用于封装常用的方法,比如让窗口的位置居中显示:

     1 using System;
     2 using UnityEngine;
     3 namespace AssemblyCSharp
     4 {
     5     public class CommonHelper
     6     {
     7         public static Rect GetCenterRect(float width,float height)
     8         {
     9             float left =(Screen.width-width)/2;    
    10             float top =(Screen.height-height)/2;
    11             return new UnityEngine.Rect(left,top,width,height);
    12         }
    13     }
    14 }
    CommonHelper

    3.创建一个PrefsHelper类,用于存放公共变量,也是很多脚本都要用到。比如存放背景音量值,背景音效的值:

     1 using System;
     2 using UnityEngine;
     3 namespace AssemblyCSharp
     4 {
     5     public class PrefsHelper
     6     {
     7         //背景音乐
     8         public static float bgVolume
     9         {
    10             get
    11             {
    12                 if(PlayerPrefs.HasKey("bgVolume"))
    13                 {
    14                     return PlayerPrefs.GetFloat("bgVolume");
    15                 }
    16                 else
    17                 {
    18                     return 1f;//音量的最大值为 1    
    19                 }
    20             }
    21             set
    22             {
    23                 PlayerPrefs.SetFloat("bgVolume",value);
    24             }
    25         }
    26         //音效
    27         public static float  effectVolume
    28         {
    29             get
    30             {
    31                 if(PlayerPrefs.HasKey("effectVolume"))
    32                 {
    33                     return     PlayerPrefs.GetFloat("effectVolume");
    34                 }
    35                 else
    36                 {
    37                     return 1f;    
    38                 }
    39             }
    40             set
    41             {
    42                 PlayerPrefs.SetFloat("effectVolume",value);
    43             }
    44         }
    45     }
    46 }
    PrefsHelper

    3.编写MainMenu脚本:

    using UnityEngine;
    using System.Collections;  
    
    public class MainMenu : MonoBehaviour {
    	public Texture bgImg;//背景图片
    	
    	private Vector2 scrolPosition;//滚动条
    	
    	void Start () {
    		AudioSource bgVolume =gameObject.GetComponent<AudioSource>();
    		bgVolume.volume=AssemblyCSharp.PrefsHelper.bgVolume;
    		
    		//初始化滚动条位置
    		scrolPosition[0]=50;//表滚动视图横向滚动滑块位置
    		scrolPosition[1]=50;//纵
    		
    	}
    	
    	// Update is called once per frame
    	void Update () {
    	
    	}
    	
    	void OnGUI()
    	{
    		GUI.DrawTexture(new Rect(0,0,bgImg.width,bgImg.height),bgImg);//背景图
    		
    		#region 居中区域
    		GUILayout.BeginArea(AssemblyCSharp.CommonHelper.GetCenterRect(150f,300f));//居中
    		GUI.color=Color.yellow;
    		if(GUILayout.Button("文件读取(笑话)"))
    		{
    			Application.LoadLevel("SceneJoke");//讲笑话
    		}
    		if(GUILayout.Button("选  项 (音量调节)"))
    		{ 
    			Application.LoadLevel("SceneOptions");	//进入  设置选项
    		}
    		
    		if(GUILayout.Button("旋转移动(失败)"))//失败:在Scene视图里看到的东西Game视图里看不到
    		{
    			Application.LoadLevel("SceneMove");	
    		}
    		
    		if(GUILayout.Button("登录(密码问题)对齐"))//密码问题
    		{
    			Application.LoadLevel("SceneLogin");
    		}
    		if(GUILayout.Button("ToolBar(数组初始化)"))
    		{
    			Application.LoadLevel("SceneToolBar");
    		}
    		if(GUILayout.Button("群组视图",GUILayout.ExpandWidth(false)))//设置宽度不等于最宽宽度
    		{
    			Application.LoadLevel("SceneGroupView");	
    		}
    		if(GUILayout.Button("动态添加/关闭窗口"))
    		{
    			Application.LoadLevel("SceneAddCloseWindow");	
    		}
    		if(GUILayout.Button("绘制动画"))
    		{
    			Application.LoadLevel("SceneDrawAnima");	
    		}
    		GUILayout.EndArea();
    		#endregion
    		
    		#region 滚动条区域
    		scrolPosition =GUI.BeginScrollView(new Rect(0,0,200,200),scrolPosition,new Rect(0,0,Screen.width,300),true,true);
    		GUILayout.Label(@"   第一个参数:设置滚动显示视图的范围 
    第二个参数:设置滚动条的起始位置,
    第三个参数用于设置滚动整体显示范围(显示范围必须小于游戏视图整体范围),
    4、5表示超过显示范围后显示滚动条");
    		GUI.EndScrollView();
    		#endregion
    	}
    }
    

     4.回到Unity编辑界面,给在MainMenu中声明的Public变量赋值。

    效果如下:

  • 相关阅读:
    数据结构与算法分析-二叉堆
    数据结构与算法分析-AVL树
    数据结构与算法分析-二叉查找树
    优秀程序员应具备的15个特性
    2016年1月22日 收盘后美加的走势
    2016年1月8日 12月非农数据
    Replace into 与Insert into on duplicate key update的区别
    MYSQL视图的学习笔记
    postgresql创建用户
    连接postgresql数据库
  • 原文地址:https://www.cnblogs.com/huangzhen22/p/3258856.html
Copyright © 2011-2022 走看看