简体   繁体   English

Unity3D:重试菜单(场景管理)

[英]Unity3D : Retry Menu (Scene Management)

I'm making a simple 2D game for Android using the Unity3D game engine. 我正在使用Unity3D游戏引擎为Android开发一个简单的2D游戏。 I created all the levels and everything but I'm stuck at making the game over/retry menu. 我创建了所有关卡和所有内容,但是我仍然无法使游戏结束/重试菜单。 So far I've been using new scenes as a game over menu. 到目前为止,我一直在使用新场景作为菜单游戏。 I used this simple script: 我使用了以下简单脚本:

#pragma strict

var level = Application.LoadLevel;

function OnCollisionEnter(Collision : Collision)
{
    if(Collision.collider.tag == "Player")
    {
        Application.LoadLevel("GameOver");
    }
}

And this as a 'menu': 这是一个“菜单”:

#pragma strict

var myGUISkin : GUISkin;

var btnTexture : Texture;

function OnGUI() {
GUI.skin = myGUISkin;


if (GUI.Button(Rect(Screen.width/2-60,Screen.height/2+30,100,40),"Retry"))
    Application.LoadLevel("Easy1");

if (GUI.Button(Rect(Screen.width/2-90,Screen.height/2+100,170,40),"Main Menu"))
    Application.LoadLevel("MainMenu");
}

The problem stands at the part where I have to create over 200 game over scenes, obstacles (the objects that kill the player) and recreate the same script over 200 times for each level. 问题在于我必须在场景,障碍物(杀死玩家的物体)上创建200多个游戏,并为每个关卡重复创建200多次相同的脚本。 Is there any other way to make this faster and less painful? 还有其他方法可以使此过程更快,更轻松吗?

Edit : If possible,please when you suggest your ideas,use javascript only,I don't understand C#,not even a little bit.I know Im asking too much but it realy confuses me. 编辑:如果可能的话,请提出您的想法,仅使用javascript,我不懂C#,甚至一点也不懂。我知道我问的太多了,但确实使我感到困惑。 Thank you. 谢谢。

There are several different solutions, but I would recommend using PlayerPrefs. 有几种不同的解决方案,但是我建议使用PlayerPrefs。 This has the extra benefit of persisting even when the application is closed and then re-opened. 即使关闭应用程序然后重新打开,这也具有持久性的额外好处。

In your Awake() function of your Main Menu class, you can get the current level and store it in a static string of your Main Menu class. 在Main Menu类的Awake()函数中,您可以获取当前级别并将其存储在Main Menu类的静态字符串中。 If it is the player's 1st time, use the name for level 1. 如果这是玩家第一次,请使用等级1的名称。

Something like this: 像这样:

static string currentLevelName;
void Awake()
{
    currentLevelName = PlayerPrefs.GetString("CurrentLevel");
    if (currentLevelName == defaultValue)
    {
       currentLevelName = "Level1"
    }
}

Then, modify your button to do this instead: 然后,修改按钮以执行此操作:

if (GUI.Button(Rect(Screen.width/2-60,Screen.height/2+30,100,40),"Retry"))
    Application.LoadLevel(currentLevelName);

Whenever the player advances to the next level, set the string in PlayerPrefs to the new level name: 每当播放器前进到下一个级别时,请将PlayerPrefs中的字符串设置为新的级别名称:

PlayerPrefs.SetString("CurrentLevel", Application.loadedLevelName);

You can create a class with static properties. 您可以创建具有静态属性的类。 For example (in c#) 例如(在C#中)

public class GameOverInput
{
    public static string name;
    public static string retryLevel;
    //all the info you need
}

Then you can easily read the input in your game over scene (only one is needed) 然后,您可以轻松地从场景中读取游戏中的输入(只需一个)

public class GameOverMenu : MonoBehavior
{
    void Start()
    {
        Debug.Log("You were killed by " + GameOverInput.name);
        Application.LoadLevel(GameOverInput.retryLevel);
    }
}

And you set this info just before loading the game over scene 您可以在将游戏加载到场景之前设置此信息

if (Collision.collider.tag == "Player")
{
    GameOverInput.name = "Baddie";
    Application.LoadLevel("GameOver");
}

Another option would be to make something like a singleton LevelManager MonoBehavior and add it to an object named "Level Manager". 另一种选择是制作类似Singleton LevelManager MonoBehavior的东西并将其添加到名为“ Level Manager”的对象中。 Use the DontDestroyOnLoad function to make the object persist even when you load another level. 使用DontDestroyOnLoad函数即使在加载另一个级别时也可以使对象持久化。

class LevelManager : MonoBehavior
{
    static LevelManager _instance;
    public string currentLevelName;
    public string killedBy;

    function Awake ()
    {
       if (_instance == null)
       {
          _instance = this;
           DontDestroyOnLoad(gameObject);
       }
       else
       {
          Destroy(gameObject); // Make sure we never have more than 1 Level Manager.
       }
    }

    LevelManager Instance
    {
       get
       {
          if (_instance == null)
          {
             GameObject levelManagerObject = GameObject.Find("Level Manager");
             _instance = levelManagerObject.GetComponent<LevelManager>();
          }
          return _instance
       }
    }
}

Then, from the main menu class, you can always access the Level Manager like so: 然后,从主菜单类中,您始终可以像以下方式访问级别管理器:

Debug.Log("Killed by " + LevelManager.Instance.killedBy);

or 要么

LevelManager.Instance.currentLevelName = Application.loadedLevelName;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM