Unity教程2D入门:21 场景控制SceneManager
https://www.bilibili.com/video/BV1gJ411N7CZPart1:新增掉到底部 触发游戏重置
场景添加空物体DeadLine,添加一个boxcollider2d放在下面位置,并且给一个DeadLine的tag
Player脚本,创建一个Restart函数
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}在Player脚本里面的OntrigerEnter2D内添加Restart函数,并加入停用音乐的功能
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Collection")
{
Destroy(collision.gameObject);
Cherry += 1;
CherryNum.text = Cherry.ToString();//计分板功能;调用text需要.text, Cherry是整形,text是字符型,需要.ToString转换!!
life += 10;
if (life > 0)
{
lifebar.fillAmount = life / 100;
}else
if(life == 100 )
{
lifebar.fillAmount = 1;
}
}
//===============↓新增===========↓新增==========↓新增==========================
if (collision.gameObject.tag == "DeadLine")//<<<======================
{
GetComponent<AudioSource>().enabled = false;
Invoke("Restart",2f);
}
}这里Invoke("Restart",2f); 功能是在2秒后调用函数Restart
Part2:添加转场功能
因为需要在player走到门口处启用功能,给游戏物体EnterDialog添加一个脚本EnterHouse
首先启用场景管理的库
using UnityEngine.SceneManagement;using UnityEngine;
using UnityEngine.SceneManagement;
public class EnterHouse : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
} 上面SceneManager.GetActiveScene().buildIndex意思是获取当前激活场景的编号
接着打开BuildSettings编排好场景顺序
就完成了场景跳转了
页:
[1]