Search Unity

Detecting scene view change with editor scripting

Discussion in 'Scripting' started by Aedous, Jun 10, 2012.

  1. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
    Hey guys!

    I'm developing an editor script on procedural map generation, it uses handles to draw out rectangles on the screen which are then cut into smaller rectangles to create a random map.
    I'm using HandlesUtility at the moment to draw a visual representation of the map on the screen, however i've noticed that it slows that considerably depending of number of rectangles it needs to draw.
    I've had a look in the documentation to see if there was anything there, had a go at detecting events on the scene and then use that to judge when to draw the handles again, but no luck there.

    Here is a screenshot of what it looks like:

    In the picture each rectangle represents cuts made from one rectangle, which then shows the other cuts made to the newly created rectangles.
    The speed issue is because i'm drawing quite a lot of rectangles on the screen, which I can turn off by only drawing the final cut out of the rectangle, however I want to be able to see the other rectangles as well.



    Is there anyway to detect if the scene has changed so I can redraw the rectangles only when the scene has changed rather than every frame ?
    Also is there any speed issues with using Handle.Utitlity ?

    Any help or points to stuff to read up on will be great! :)
     
  2. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    anyone found a solution for this?
     
  3. Mikilo

    Mikilo

    Joined:
    Jan 29, 2013
    Posts:
    694
    Hello dudes!

    I stumbled upon this issue too and I wrote a solution.
    Code (CSharp):
    1.   [InitializeOnLoad]
    2.   public static class EditorApplicationExtended
    3.    {
    4.      public static Action   ChangeScene;
    5.  
    6.      private static string  currentScene;
    7.      private static int     frameCount;
    8.      private static float   realtimeSinceStartup;
    9.      private static int     renderedFrameCount;
    10.  
    11.      static   EditorApplicationExtended()
    12.      {
    13.        // Even if update is not the most elegant. Using hierarchyWindowChanged for CPU sake will not work in all cases, because when hierarchyWindowChanged is called, Time's values might be all higher than current values. Why? Because current values are set at the first frame. If you keep reloading the same scene, this case happens.
    14.        EditorApplication.update += EditorApplicationExtended.DetectChangeScene;
    15.      }
    16.  
    17.      private static void   DetectChangeScene()
    18.      {
    19.        if (EditorApplicationExtended.currentScene != EditorApplication.currentScene ||
    20.          // Detect change with the same scene, the real time should fill 99% of cases. Not tested but if you are able to load 2 scenes in a single frame, the time might not work.
    21.          EditorApplicationExtended.realtimeSinceStartup > Time.realtimeSinceStartup ||
    22.          EditorApplicationExtended.frameCount > Time.frameCount ||
    23.          EditorApplicationExtended.renderedFrameCount > Time.renderedFrameCount)
    24.        {
    25.          EditorApplicationExtended.currentScene = EditorApplication.currentScene;
    26.  
    27.          if (EditorApplicationExtended.ChangeScene != null)
    28.            EditorApplicationExtended.ChangeScene();
    29.        }
    30.  
    31.        EditorApplicationExtended.frameCount = Time.frameCount;
    32.        EditorApplicationExtended.realtimeSinceStartup = Time.realtimeSinceStartup;
    33.        EditorApplicationExtended.renderedFrameCount = Time.renderedFrameCount;
    34.      }
    35.    }
     
    astracat111 likes this.