Search Unity

Unity freeze after doing time-consuming operation in OnGUI

Discussion in 'Scripting' started by zhutianlun810, Jul 2, 2020.

  1. zhutianlun810

    zhutianlun810

    Joined:
    Sep 17, 2017
    Posts:
    168
    Hello,
    I face a weird problem. I am doing some custom editor job like:
    Code (CSharp):
    1. On GUI(){
    2.     if (GUILayout.Button("search")){
    3.         some time-consuming job cost around 35s...
    4.     }
    5. }
    Then I find that after doing my "search", the next OnGUI call has been delayed. It is like that Unity is frozen for 10 sec, and manually call Repaint won't work. Why?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Because your code is still running. Unity is mostly single-threaded. If you do something on the main thread that takes 10 seconds, nothing else will work for that time. What is the work that you are doing that takes so much time? Can you do it in a background thread?
     
  3. zhutianlun810

    zhutianlun810

    Joined:
    Sep 17, 2017
    Posts:
    168
    I use the breakpoints to debug this. I find the lag is between two GUI calls. My heavy calculation has already finished.
     
  4. zhutianlun810

    zhutianlun810

    Joined:
    Sep 17, 2017
    Posts:
    168
    I found the problem. I guess it is due to GC. My time-consuming job is AssetDataBase.Load. I found that after loading all prefabs into memory. The memory increase 600+mb. Then the lag happens. Finally the memory drop back.