Rendering delay GUI

Hello!

I have a problem. There is Editor script that creates a button on mouse click.
But creating a button is accompanied by a long delay, especially noticeable during rapid mouse-clicking. All the buttons are drawn only after I stop clicking.

How I can solve this problem.

This is my code:

 List<Rect> keys = new List<Rect>();


        void OnGUI () {

                bool flag_KeyIsPressed=false;
                bool flag_MouseIsPressed=false;
               
               Rect KeyPressed=new Rect();

                Event e = Event.current;

                // кнопка очистки списка
                if(GUI.Button(new Rect(400,50,50,20),"Delete"))
                        keys.Clear();

               
                foreach(Rect key in keys)
                        if(GUI.Button(key,"Button"))
                        {
                                Debug.Log("KeyPressed");
                                flag_KeyIsPressed=true;
                                KeyPressed=key;

                        }
                               
       

       
                if(e.type==EventType.MouseUp)
                {
                       
                        flag_MouseIsPressed=true;
                }

                if(flag_MouseIsPressed)

                        if(!flag_KeyIsPressed)
                        {
                                Debug.Log("Add" +(e.mousePosition.x-25));
                                GUI.Button(new Rect(e.mousePosition.x-25,100,50,20),"Button");
                                keys.Add(new Rect(e.mousePosition.x-25,100,50,20));
                        }
                if(flag_KeyIsPressed)
                {
                        Debug.Log("Delete "+KeyPressed);
                        keys.Remove(KeyPressed);
                }

        }

Debug.Log() is slow. Have you tried the same code without the Debug.Log() lines?

Yes. I tried. Result is the same.

I make a video with problem

Ah, thanks for posting the video. I think I see what’s going on. Just call Repaint() after adding a button. This tells the window to redraw itself.

Thank you, TonyLi. It really works!

Glad I could help!