【问题标题】:How do you get the image component of a nested game object in a UI prefab?如何在 UI 预制件中获取嵌套游戏对象的图像组件?
【发布时间】:2015-01-19 00:15:47
【问题描述】:

我正在尝试更改已保存为预制件的对话框 UI(Unity 4.6 的 UI)中嵌套图像对象的精灵。我可以加载预制件,但是当我尝试这样做时:

Image[] imageComponents = conversationDialogNoChoice.GetComponentsInChildren<Image>();

我得到零件。层次结构是:

完整代码为:

        private GameObject conversationDialogNoChoice;
public void StartConversation(Conversation conversation)
    {
        if (!talking)
        {
            //StartCoroutine(DisplayConversation(conversation));
            StartCoroutine(DisplayConversation(conversation));
        }
    }

    IEnumerator DisplayConversationNewUI(Conversation conversation)
    {
        conversationDialogNoChoice = (GameObject)Resources.Load("Prefabs/ConversationDialogNoChoices");
        conversationDialogChoice = (GameObject)Resources.Load("Prefabs/ConversationDialogChoices");
        ConversationTest = (GameObject)Resources.Load("Prefabs/ConversationTest");
        bool nextPushed;

        foreach (var conversationLine in conversation.ConversationLines)
        {
            nextPushed = false;
            Image[] imageComponents = conversationDialogNoChoice.GetComponentsInChildren<Image>();
            Debug.Log(imageComponents.Length);
            //imageComponents[].sprite = currentCovnersationLine.DisplayPicture;

            Instantiate(conversationDialogNoChoice);

            while (!nextPushed)
            {
                if (Input.GetKeyDown(KeyCode.Return))
                {
                    nextPushed = true;
                }
                yield return null;
            }
        }

        talking = false; //talking is complete
        if (conversation.Repeatable == false)
        {
            conversation.CanOccur = false;
        }
        yield return null;


    }

【问题讨论】:

    标签: unity3d unityscript unity3d-gui


    【解决方案1】:

    您确定您的 Image 游戏对象处于活动状态吗? GetComponentsInChildren has includeInactive parameter, and it's set to false by default.

    【讨论】:

    • 我有一个游戏对象 obj:obj > Canvas > ImageMask > Image > text > Image(1) > image(2)。在一个 GameObject 中存在图像的“嵌套”或“分层”。假设我只想访问第二张图像,“图像 (1)”-of-3 图像。使用此图像,将其打开和关闭,以显示其他图像。与加载/卸载图像相比,关闭图像似乎更有效,也就是说,如果您想知道,为什么要这样做?在 Flash 中,它就像“Frame(2) 关闭”一样简单。
    【解决方案2】:

    也许您应该考虑在资源加载路径中包含“面板”。

    【讨论】:

      【解决方案3】:

      除了“Max Yankov”所说的。

      GetComponentsInChildren&lt;Image&gt;() 中的Image 指的是游戏对象上的脚本或组件,而不是游戏对象本身。

      因此,如果您需要查找 Image 的所有组件,您需要在您的子游戏对象上添加名称为 Image 的脚本或组件。无需将 GameObject 命名为“Image”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-26
        • 2021-02-13
        • 1970-01-01
        相关资源
        最近更新 更多