Android+Unity游戏开发基础之场景的切换 您所在的位置:网站首页 unity切换场景代码 Android+Unity游戏开发基础之场景的切换

Android+Unity游戏开发基础之场景的切换

#Android+Unity游戏开发基础之场景的切换| 来源: 网络整理| 查看: 265

下面我们就来个 基础吧,就是场景的切换,类似于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);//这个作用是场景切换时,一下代码不撤销             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(); 退出

linux


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有