android+unity游戏开发基础之场景的切换 ***
android+unity游戏开发基础之场景的切换
下面我们就来个 基础吧,就是场景的切换,类似于android中activity的切换。下面来看看吧。
切换必须具备的有:1.两个场景或以上, 2.Application.LoadLevel(x)//x可以是场景名或者是场景号。3.那就是一个事件触发。
场景menu
场景yaya
场景yaya1
下面就来贴贴代码了:
- using UnityEngine;
- using System.Collections;
- public class menu : MonoBehaviour {
- bool flag;
- void Start () {
- DontDestroyOnLoad(this);//这个作用是场景切换时,一下代码不撤销
using UnityEngine; using System.Collections; public class menu : MonoBehaviour { bool flag; void Start () { DontDestroyOnLoad(this);//这个作用是场景切换时,一下代码不撤销
- flag=true;
- }
- void Update () {
- if(Input.GetKeyDown(KeyCode.Space)){
- if(flag){
- flag=false;
- }
- else{
- flag=true;
- }
- }
- }
- void OnGUI(){
- if(!flag){
- return;
- }
- if(GUI.Button(new Rect(Screen.width/2-50,Screen.height/2-30,40,60),"yaya1")){
- Application.LoadLevel(1);
- }
- if(GUI.Button(new Rect(Screen.width/2,Screen.height/2-30,40,60),"yaya2")){
- Application.LoadLevel(2);
- }
- if(GUI.Button(new Rect(Screen.width/2+50,Screen.height/2-30,40,60),"Quit")){
- Application.Quit();
- }
- }
- }
flag=true; } void Update () { if(Input.GetKeyDown(KeyCode.Space)){ if(flag){ flag=false; } else{ flag=true; } } } void OnGUI(){ if(!flag){ return; } if(GUI.Button(new Rect(Screen.width/2-50,Screen.height/2-30,40,60),"yaya1")){ Application.LoadLevel(1); } if(GUI.Button(new Rect(Screen.width/2,Screen.height/2-30,40,60),"yaya2")){ Application.LoadLevel(2); } if(GUI.Button(new Rect(Screen.width/2+50,Screen.height/2-30,40,60),"Quit")){ Application.Quit(); } } }
把以上代码绑定在第一个场景(menu)里面;
我们解释一下代码
DontDestroyOnLoad(this);//这个作用是场景切换时,一下代码不撤销,而不是场景中的游戏对象不撤销
Application.Quit(); 退出