如何在 Unity 中更改场景
How to change scenes in Unity
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
//Call What Happens Here
}
我想用一些将播放器发送到主菜单(场景 0)的代码替换评论。顺便说一句,这是JavaScript,我使用的是Unity 5.6。完整代码如下。
#pragma strict
var Player : Transform;
var MoveSpeed = 4;
var MinDist = 3;
var MaxDist = 20;
function Start()
{
}
function Update ()
{
transform.LookAt (Player);
if(Vector3.Distance(transform.position,Player.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
//Call What Happens Here
}
}
}
您可以使用 SceneManager.LoadScene,它可以采用构建索引或场景名称
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
SceneManager.LoadScene(0);
}
或
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
SceneManager.LoadScene("SceneName");
}
您只需确保在 构建设置 中添加所有场景。
不要忘记导入 SceneManagement
以便能够使用它。
using UnityEngine.SceneManagement;
我想这就是您要找的:SceneManager。以下是文档:https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html
#pragma strict
function Start() {
// Only specifying the sceneName or sceneBuildIndex will load the scene with the Single mode
SceneManager.LoadScene("OtherSceneName");
}
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
//Call What Happens Here
}
我想用一些将播放器发送到主菜单(场景 0)的代码替换评论。顺便说一句,这是JavaScript,我使用的是Unity 5.6。完整代码如下。
#pragma strict
var Player : Transform;
var MoveSpeed = 4;
var MinDist = 3;
var MaxDist = 20;
function Start()
{
}
function Update ()
{
transform.LookAt (Player);
if(Vector3.Distance(transform.position,Player.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
//Call What Happens Here
}
}
}
您可以使用 SceneManager.LoadScene,它可以采用构建索引或场景名称
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
SceneManager.LoadScene(0);
}
或
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
SceneManager.LoadScene("SceneName");
}
您只需确保在 构建设置 中添加所有场景。
不要忘记导入 SceneManagement
以便能够使用它。
using UnityEngine.SceneManagement;
我想这就是您要找的:SceneManager。以下是文档:https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html
#pragma strict
function Start() {
// Only specifying the sceneName or sceneBuildIndex will load the scene with the Single mode
SceneManager.LoadScene("OtherSceneName");
}