下面是我的统一切换场景的代码:
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class 触发场景转移 : MonoBehaviour
{
public string sceneName; //要转移到的场景的名称
public Text promptText; //提示文本
private bool isTriggered = false; //是否触发门
void Start()
{
promptText.enabled = false; //隐藏提示文本
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
promptText.enabled = true; //显示提示文本
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
promptText.enabled = false; //隐藏提示文本
}
}
private void Update()
{
if (promptText.enabled && Input.GetKeyDown(KeyCode.E) && !isTriggered)
{
string sceneToLoad = null;
switch (gameObject.name)
{
case "Door1":
sceneToLoad = "菠萝屋客厅";
break;
case "Door2":
sceneToLoad = "章鱼哥家";
break;
case "Door3":
sceneToLoad = "派大星家";
break;
}
if (sceneToLoad != null)
{
SceneManager.LoadScene(sceneToLoad);
}
}
}
}
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class 触发场景转移 : MonoBehaviour
{
public string sceneName; //要转移到的场景的名称
public Text promptText; //提示文本
private bool isTriggered = false; //是否触发门
void Start()
{
promptText.enabled = false; //隐藏提示文本
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
promptText.enabled = true; //显示提示文本
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
promptText.enabled = false; //隐藏提示文本
}
}
private void Update()
{
if (promptText.enabled && Input.GetKeyDown(KeyCode.E) && !isTriggered)
{
string sceneToLoad = null;
switch (gameObject.name)
{
case "Door1":
sceneToLoad = "菠萝屋客厅";
break;
case "Door2":
sceneToLoad = "章鱼哥家";
break;
case "Door3":
sceneToLoad = "派大星家";
break;
}
if (sceneToLoad != null)
{
SceneManager.LoadScene(sceneToLoad);
}
}
}
}