android+unity游戏开发基础之场景的切换 ***

android+unity游戏开发基础之场景的切换

 
  快一个星期没写博客了,主要是我去学C#了,还有就是我发现我最基本的东西没学好,所以稍微补了补,呵呵~~~

 下面我们就来个 基础吧,就是场景的切换,类似于android中activity的切换。下面来看看吧。

切换必须具备的有:1.两个场景或以上, 2.Application.LoadLevel(x)//x可以是场景名或者是场景号。3.那就是一个事件触发。

 

场景menu

 

场景yaya

 

场景yaya1

 

下面就来贴贴代码了:

   

  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class menu : MonoBehaviour {  
  5.   
  6.      bool flag;  
  7.     void Start () {  
  8.       DontDestroyOnLoad(this);//这个作用是场景切换时,一下代码不撤销  
using UnityEngine;
using System.Collections;

public class menu : MonoBehaviour {

	 bool flag;
	void Start () {
	  DontDestroyOnLoad(this);//这个作用是场景切换时,一下代码不撤销
  1.         flag=true;  
  2.     }  
  3.       
  4.     void Update () {  
  5.      if(Input.GetKeyDown(KeyCode.Space)){  
  6.             if(flag){  
  7.                 flag=false;  
  8.             }  
  9.             else{  
  10.                 flag=true;  
  11.             }  
  12.         }  
  13.     }  
  14.     void OnGUI(){  
  15.         if(!flag){  
  16.             return;  
  17.         }  
  18.         if(GUI.Button(new Rect(Screen.width/2-50,Screen.height/2-30,40,60),"yaya1")){  
  19.             Application.LoadLevel(1);  
  20.         }  
  21.         if(GUI.Button(new Rect(Screen.width/2,Screen.height/2-30,40,60),"yaya2")){  
  22.             Application.LoadLevel(2);  
  23.         }  
  24.         if(GUI.Button(new Rect(Screen.width/2+50,Screen.height/2-30,40,60),"Quit")){  
  25.             Application.Quit();  
  26.         }  
  27.     }  
  28. }  
		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(); 退出

posted @ 2013-03-23 11:54  小薇林  阅读(431)  评论(0编辑  收藏  举报