【问题标题】:How to reload a level in unity如何统一重新加载关卡
【发布时间】:2016-10-08 04:21:06
【问题描述】:

我创建了一个简单的 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);
        }
    }
}

我错过了什么?

【问题讨论】:

  • Application.LoadLevel (0);?
  • @kalamazoowho 不 - 值得注意的是 Application.LoadLevel() 已过时。
  • 那我该怎么办?这似乎很简单,但由于某种原因,我终生无法找到答案......

标签: c# unity3d


【解决方案1】:

这将重新加载当前场景

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

【讨论】:

  • 它肯定是在重新加载,但面板并没有按照他们应该的方式重塑,即使它的说明与重新加载场景的功能相同,而是我盯着残局screen.如何改变它?
  • @Radu Harabagiu 当你重新加载场景时,它会破坏你刚刚重塑的对象,用它们的默认值加载场景,你应该在场景加载到一个单独的函数后尝试重塑面板。 (也许是 Awake() )?
  • 天哪,我太笨了——非常感谢你!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
  • 2012-12-06
  • 1970-01-01
相关资源
最近更新 更多