0

我在 for loop pf OnGUI() 方法中打印 GUI 元素时遇到问题。在下面列出的代码中,我创建了用户单击 GUI.Box 的 GUI.Label:

public List<string> messages = new List<string>();     
int dialogueMsg = 0;
     void OnGUI()
         {
             Event currentEvent = Event.current;
             if (enabled)
             {
                 GUI.BeginGroup(new Rect(Screen.width / 8, Screen.height / 4 + Screen.height / 2, Screen.width - Screen.width / 4, Screen.height / 4));
                 GUI.Box(new Rect(0, 0, Screen.width - Screen.width / 4, Screen.height / 4), "");
                 if (currentEvent.button == 0 && currentEvent.type == EventType.mouseDown)
                 {
                     for (int i = 0; i < messages.Count; i ++)
                     {
                         if (dialogueMsg < messages.Count)
                         {
                             print(dialogueMsg);
                             print(messages[dialogueMsg]);
                             GUI.Label(new Rect(25, 25, Screen.width - Screen.width / 4, Screen.height / 4), messages[dialogueMsg]);
                             ++dialogueMsg;
                             break;
                         }
                         else
                         {
                             enabled = !enabled;
                             break;
                         }
                     }
                 }
                 GUI.EndGroup();
             }
         } 

当用户点击时 - 除了打印正确的值dialogMsgmessages[dialogueMsg]之外,什么都没有发生。我究竟做错了什么?有人熟悉这个问题吗?

4

1 回答 1

0

ifandelse语句中都有break关键字,因此for循环只会运行一次然后退出循环,无论是否i < messages.Count()if只需从您希望循环继续的代码块或或两者中取出 break 关键字,else那么您应该为该部分做好准备。

于 2015-02-04T14:12:37.740 回答