如何重新加载统一级别

时间:2016-10-07 19:21:12

标签: c# unity3d

我创造了一个简单的2d摇滚剪刀游戏,在每个玩家按下一个按钮后,他们被带到一个有两个按钮的终局游戏屏幕:退出并再次播放。

问题是,当用户再次按下播放按钮时,我无法弄清楚如何实际重启游戏。这就是我尝试过的:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;

public class PlayAgain : MonoBehaviour ,IPointerClickHandler{

    public void OnPointerClick(PointerEventData eventData)
    {   
        GameObject.FindGameObjectWithTag("Player1").transform.localScale = new Vector3(1,1,1);
        GameObject.FindGameObjectWithTag("Player2").transform.localScale = new Vector3(0,0,0);
        GameObject.FindGameObjectWithTag("GameOverScreen").transform.localScale = new Vector3(0,0,0);
        SceneManager.LoadScene (SceneManager.GetActiveScene().name);
    }

}

带有“Player 1/2 / GameOverScreen”标签的对象是面板,GameOverScreen是当前的。这应该(如果我没有错)将所有变量重置为第一个脚本给出的值。应该在场景加载后播放。为了确保这一点,我已经创建了一个这样的场景执行命令:

变量 - >事件 - > OnClickRock / Paper / Scissors-> PlayAgain

以下是其他脚本:

变量:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class Variables :MonoBehaviour {
    static public string Player1Choice;
    static public bool Player1Turn=true;
    static public string Player2Choice;
    static public bool Player2Turn=true;
    static public bool GameOver=false;
    static public bool Player1Victory=false;
    static public bool Player2Victory=false;
    static public bool Draw=false;
}

活动

using UnityEngine;
using System.Collections;

public class Events : MonoBehaviour {

    void Start()
    {
        GameObject.FindGameObjectWithTag("Player1").transform.localScale = new Vector3(1,1,1);
        GameObject.FindGameObjectWithTag("Player2").transform.localScale = new Vector3(0,0,0);
        GameObject.FindGameObjectWithTag("GameOverScreen").transform.localScale = new Vector3(0,0,0);
    }

    void Update ()
    {
        if (Variables.Player1Turn == false) {
            GameObject.FindGameObjectWithTag ("Player1").transform.localScale = new Vector3 (0, 0, 0);
            GameObject.FindGameObjectWithTag ("Player2").transform.localScale = new Vector3 (1, 1, 1);
        }
        if (Variables.GameOver == true) 
        {
            if (Variables.Player1Choice == "rock") 
            {
                if (Variables.Player2Choice == "rock") 
                {
                    Variables.Draw = true;
                }
                else if (Variables.Player2Choice == "paper")
                {
                    Variables.Player2Victory = true;
                }
                else if (Variables.Player2Choice == "scissors")
                {
                    Variables.Player1Victory = true;
                }
            }
            else if (Variables.Player1Choice == "paper")
            {
                if (Variables.Player2Choice == "rock")
                {
                    Variables.Player1Victory = true;
                }
                else if (Variables.Player2Choice == "paper")
                {
                    Variables.Draw = true;
                }
                else if (Variables.Player2Choice == "scissors")
                {
                    Variables.Player2Victory = true;
                }
            }
            else if (Variables.Player1Choice == "scissors")
            {
                if (Variables.Player2Choice == "rock")
                {
                    Variables.Player2Victory = true;
                }
                else if (Variables.Player2Choice == "paper")
                {
                    Variables.Player1Victory = true;
                }
                else if (Variables.Player2Choice == "scissors")
                {
                    Variables.Draw = true;
                }
            }
            if (Variables.Player1Victory == true)
            {
                print("Player 1 won!");
            }
            else if(Variables.Player2Victory == true)
            {
                print("Player 2 won!");
            }
            else
            {
                print("It's a draw!");
            }
            print("Player one chose: " + Variables.Player1Choice);
            print("Player two chose: " + Variables.Player2Choice);

            GameObject.FindGameObjectWithTag ("Player2").transform.localScale = new Vector3 (0, 0, 0);
            GameObject.FindGameObjectWithTag("GameOverScreen").transform.localScale = new Vector3(1,1,1);
        }
    }
}

我错过了什么?

1 个答案:

答案 0 :(得分:0)

这将重新加载当前场景

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex,LoadSceneMode.Single);