Unity3d场景切换
发表于2018-03-25
场景间的切换需求在开发过程中算是比较常见的,下面就通过一个具体的场景切换案例给大家作解说,为了方便演示,在第一个场景内设置一个按钮,点击这个按钮,触发切换新场景时间。



在该场景内的按钮添加一个C#脚本组件Component

在Button控件创建的组件

在常见组件之前,我们先创建一个createNewScene.cs文件,用于单例模式
createNewScene.cs
private string warriorPath = "Prefabs/Character/Warrior"; GameObject obj = (GameObject)Resources.Load(warriorPath);//获取角色存放路径 warriorObj = GameObject.Instantiate(obj);//实例化 warriorObj.transform.position = Vector3.zero;//角色的位置转换
startGame.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class startGame : MonoBehaviour {
// Use this for initialization
void Start () {
Button btn = transform.GetComponent<Button>();//获取场景中的按钮
btn.onClick.AddListener(eventListener);//点击事件触发(回调)
}
// Update is called once per frame
void Update () {
}
void eventListener()
{
createNewScene.create.loadScene("createMan");//“createMan”是新场景名称
}
}
//运行第一个场景,点击按钮,场景发生切换
