我正在尝试更改我已保存为预制件的对话框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;
}
答案 0 :(得分:1)
你确定你的Image游戏对象是活跃的吗? GetComponentsInChildren
has includeInactive
parameter, and it's set to false
by default
答案 1 :(得分:0)
也许您应该考虑在资源加载路径中包含“Panel”。
答案 2 :(得分:0)
除了“Max Yankov”所说的。
Image
中的 GetComponentsInChildren<Image>()
是指GameObject上的脚本或组件,而不是GameObject本身。
因此,如果您需要查找Image
的所有组件,则需要在您的子GameObject上添加名为Image
的脚本或组件。命名GameObject“Image”不是必需的。