2017-06-08 21 views
0

我正在写请求一些援助。我试图在我的游戏中编写两个按钮“主菜单”和“退出”按钮,以便在我的播放器死亡时才会出现。我一直在试图定义gameobject列表,试图编程两个按钮,当我的游戏中的玩家死亡时出现

GameManager脚本。

//Awake is always called before any Start functions 
void Awake() 
{ 
    //Check if instance already exists 
    if (instance == null) 

     //if not, set instance to this 
     instance = this; 

    //If instance already exists and it's not this: 
    else if (instance != this) 

     //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager. 
     Destroy(gameObject); 

    //Sets this to not be destroyed when reloading scene 
    DontDestroyOnLoad(gameObject); 

    //Assign enemies to a new List of Enemy objects. 
    enemies = new List<Enemy>(); 

    //Get a component reference to the attached BoardManager script 
    boardScript = GetComponent<BoardManager>(); 

    //Call the InitGame function to initialize the first level 
    //InitGame(); 
} 

// TODO: Uncommented Code 
/// <summary> 
/// Called when the level/scene has finished loading 
/// </summary> 
/// <param name="scene"></param> 
/// <param name="mode"></param> 
void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode) 
{ 
    // Don't initialize game on main menu. That would be silly 
    if (scene.buildIndex > 0) 
    { 
     InitGame(); 
    } 

    level++; // Increment after we have loaded level. Might be a better place to put this 
} 

// TODO: Uncomemnted code 
void OnEnable() 
{ 
    SceneManager.sceneLoaded += OnLevelFinishedLoading; 
} 

void OnDisable() 
{ 
    SceneManager.sceneLoaded -= OnLevelFinishedLoading; 
} 

//Initializes the game for each level. 
void InitGame() 
{ 
    //While doingSetup is true the player can't move, prevent player from moving while title card is up. 
    doingSetup = true; 

    //Get a reference to our image LevelImage by finding it by name. 
    levelImage = GameObject.Find("LevelImage"); 

    //Get a reference to our text LevelText's text component by finding it by name and calling GetComponent. 
    levelText = GameObject.Find("LevelText").GetComponent<Text>(); 

    //Set the text of levelText to the string "Day" and append the current level number. 
    levelText.text = "Day " + level; 

    //Set levelImage to active blocking player's view of the game board during setup. 
    levelImage.SetActive(true); 

    //Call the HideLevelImage function with a delay in seconds of levelStartDelay. 
    Invoke("HideLevelImage", levelStartDelay); 

    //Clear any Enemy objects in our List to prepare for next level. 
    enemies.Clear(); 

    //Call the SetupScene function of the BoardManager script, pass it current level number. 
    boardScript.SetupScene(level); 

    // TODO: Removed initGame() from here. No need to call it twice 
} 

//Hides black image used between levels 
void HideLevelImage() 
{ 
    //Disable the levelImage gameObject. 
    levelImage.SetActive(false); 

    //Set doingSetup to false allowing player to move again. 
    doingSetup = false; 
} 

//Update is called every frame. 
void Update() 
{ 
    //Check that playersTurn or enemiesMoving or doingSetup are not currently true. 
    if (playersTurn || enemiesMoving || doingSetup) 

     //If any of these are true, return and do not start MoveEnemies. 
     return; 

    //Start moving enemies. 
    StartCoroutine(MoveEnemies()); 
} 

//Call this to add the passed in Enemy to the List of Enemy objects. 
public void AddEnemyToList(Enemy script) 
{ 
    //Add Enemy to List enemies. 
    enemies.Add(script); 
} 

public void ShowButtons() 
{ 
    foreach (var Button in buttons) 
    { 
     var gameOver = GetComponent<Exit2>(); 
     Button.gameObject.SetActive(true); 
    } 
} 

//GameOver is called when the player reaches 0 food points 
public void GameOver() 
{ 
    //Set levelText to display number of levels passed and game over message 
    levelText.text = "After " + level + " days, you starved."; 

    //Shows buttons on player's death. 
    var gameOver = GetComponent<Exit2>(); 
    gameOver.ShowButtons("Main menu 2", "Exit2"); 

    //Enable black background image gameObject. 
    levelImage.SetActive(true); 

    //Disable this GameManager. 
    enabled = false; 
} 

//Coroutine to move enemies in sequence. 
IEnumerator MoveEnemies() 
{ 
    //While enemiesMoving is true player is unable to move. 
    enemiesMoving = true; 

    //Wait for turnDelay seconds, defaults to .1 (100 ms). 
    yield return new WaitForSeconds(turnDelay); 

    //If there are no enemies spawned (IE in first level): 
    if (enemies.Count == 0) 
    { 
     //Wait for turnDelay seconds between moves, replaces delay caused by enemies moving when there are none. 
     yield return new WaitForSeconds(turnDelay); 
    } 

    //Loop through List of Enemy objects. 
    for (int i = 0; i < enemies.Count; i++) 
    { 
     //Call the MoveEnemy function of Enemy at index i in the enemies List. 
     enemies[i].MoveEnemy(); 

     //Wait for Enemy's moveTime before moving next Enemy, 
     yield return new WaitForSeconds(enemies[i].moveTime); 
    } 
    //Once Enemies are done moving, set playersTurn to true so player can move. 
    playersTurn = true; 

    //Enemies are done moving, set enemiesMoving to false. 
    enemiesMoving = false; 
} 

void Awake() 
{ 
    // Get the buttons 
    menuButtonList(); 
    //buttons = GetComponentsInChildren<Button>(); 

    // Disable them 
    HideButtons(); 
} 

public void HideButtons() 
{ 
    foreach (var Button in buttons) 
    { 
     Button.gameObject.SetActive(false); 
    } 
} 

public void LoadByIndex(int sceneIndex) 
{ 
    SceneManager.LoadScene(sceneIndex); 
} 

public void Quit() 
{ 
#if UNITY_EDITOR 
    UnityEditor.EditorApplication.isPlaying = false; 
#else 
    Application.Quit(); 
#endif 
} 

我只是想使它所以在我的下GameOver功能GameManager脚本调用这两个按钮出现什么在游戏中请检查GameManager 162-194和所有的Exit2的线S已定义。

+0

当您尝试自己解决问题时,您能向我们展示一些您使用的代码吗?你发布的内容全部来自教程。 –

回答

2

首先你需要一个Canvas。然后在画布内创建两个按钮。在GameManager脚本中引用此画布的实例。

当游戏开始时,通过.SetActive(false)取消激活它,然后在GameOver通过.SetActive(true)发生时再次激活它。

向GameManager脚本添加2个方法,一个用于退出,另一个用于MainMenu。并且让你创建的两个按钮的OnClick事件触发这两种方法。