在我的脚本中激活后,GameObject预制件不会显示在场景中

时间:2018-04-09 01:02:51

标签: c# unity3d unity3d-gui

我有2个类附加到2个不同的GUI,PlayerInfoGUI和DiplomacyGUI。

我正在尝试在PlayerInfoGUI中创建一个动态按钮列表,当点击它时会拉出DiplomacyGUI。 (GUI和动态按钮都有自己的预制件创建)

我的PlayerInfoGUI类使用PopulatePlayerList方法使用按钮动态填充面板。

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;


public class PlayerInfoGUI : MonoBehaviour
{
    public Image[] guiElements;
    public GameObject playerInfoButtonPrefab, canvasParent;
    public GameObject diplomacyMenu;

    void Awake ()
    {
        PopulatePlayerList (GameEngine.competingPlayers);
    }

    void PopulatePlayerList (List<CompetingPlayer> players)
    {

        for (int i = 0; i < players.Count; i++) {
            GameObject go = (GameObject)Instantiate 
            (playerInfoButtonPrefab);
            Button playerInfoButton = go.GetComponent<UnityEngine.UI.Button> 
                                                                         ();
            CompetingPlayer receivingPlayer = players [i];
            playerInfoButton.onClick.AddListener (() => handleDiplomacyMenu 
                                                         (receivingPlayer));
            go.transform.SetParent (canvasParent.transform, false);
        }
    }

    public void handleDiplomacyMenu (CompetingPlayer receivingPlayer)
    {
        diplomacyMenu.SetActive (true);
    }
}

PlayerInfo按钮上的侦听器在单击时触发,但是DiplocyMenu GameObject未显示在场景中。我读过的大部分研究都表明,这应该是一个简单的外交手册.SenActive(真实),或者是Diplocy.gameObject.SetActive(true),但这不起作用。

我已确认代码正在运行,但无法看到该对象。  提前谢谢!

PlayerInfoPrefab

PlayerInfoButtonPrefab

DiplomacyPrefab

1 个答案:

答案 0 :(得分:0)

当外交菜单已经是游戏对象时,你正在使用diplomacyMenu.gameObject,这可能会引发问题。还要确保由diplomacyMenu表示的预制件启用了其子项。我认为SetActive()只是将顶级父级设置为启用,而不是任何嵌套对象。