游戏结束的动画,以及游戏结束后加载场景重新开始游戏
using UnityEngine.SceneManagement;//加载场景命名空间引用
public class GameManager : MonoBehaviour
{
private Transform StartPoint;//针的发射点
private Transform SpawnPoint;//针的生成点
public GameObject pinPrefab;//针的预设物,需要拖拽
private Pin currentPin;//获取当前针,以方便调用方法
private bool isGameOver = false;//判定游戏是否结束
private int score = 0;
public Text scoreText;//控制分数的显示
private Camera mainCamera;
public float speed = 3;//动画的速度
void Start ()
{
StartPoint = GameObject.Find("StartPoint").transform;
SpawnPoint = GameObject.Find("SpawnPoint").transform;
mainCamera = Camera.main;
SpawnPin();
}
private void Update()
{
if (isGameOver) return;//控制针的生成和发射
if (Input.GetMouseButtonDown(0))
{
score++;
scoreText.text = score.ToString();
currentPin.StartFly();//调用飞行的方法
SpawnPin();
}
}
void SpawnPin()
{
currentPin= GameObject.Instantiate(pinPrefab,SpawnPoint.position,pinPrefab.transform.rotation).GetComponent<Pin>();
}
public void GameOver()
{
if (isGameOver) return;//保证这个方法只执行一次
GameObject.Find("Circle").GetComponent<RotateSelf>().enabled = false;
StartCoroutine(GameOverAnimation());
isGameOver = true;
}
//利用协程控制动画
IEnumerator GameOverAnimation()
{
while (true)
{
mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor, Color.red, speed * Time.deltaTime);//差值进行改变
mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize, 4, speed * Time.deltaTime);
if (Mathf.Abs(mainCamera.orthographicSize-4)<0.01f)
{
break;
}
yield return 0;
}
yield return new WaitForSeconds(0.2f);//等待0.2秒重新加载
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);//游戏结束后,重新加载当前场景
}
}