如何在UI预制中获取嵌套游戏对象的图像组件?

时间:2015-01-19 00:15:47

标签: unity3d unityscript unity3d-gui

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

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

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

enter image description here

完整的代码是:

        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;


    }

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

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

答案 2 :(得分:0)

除了“Max Yankov”所说的。

Image中的

GetComponentsInChildren<Image>()是指GameObject上的脚本或组件,而不是GameObject本身。

因此,如果您需要查找Image的所有组件,则需要在您的子GameObject上添加名为Image的脚本或组件。命名GameObject“Image”不是必需的。