Search Unity

Editor performance drop - FlushDirty?

Discussion in 'Editor & General Support' started by Freaking-Pingo, May 26, 2016.

  1. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    Hi, I am currently in the process of making my own Editor script, but for some reason I am getting huge performance drop when manipulating a simple Range slider on a GameObject which have an editor script associated.

    Ignore the performance drop in "DockArea.OnGUI()" as this is caused by the profiler itself.


    I can't seem to find any information on FlushDirty. My best lead is that it has something to do with some changes occuring after Unity 5.3: http://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html. I am using Unity 5.3.5f1. If anyone could enlighten me on what FlushDirty is, that would be much appreciated.
     
  2. Deleted User

    Deleted User

    Guest

    What was your slider doing? Flush is usually used when the cpu is sleeping. It forces the queued commands to the GPU.

    https://msdn.microsoft.com/en-us/library/windows/desktop/ff476425(v=vs.85).aspx

    Unity use could be different, the link you sent is to set dirty which is used when a object is modified by an editor script. Maybe the changes your doing to the object are put in a queue then are flushed to save?
     
  3. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    Alright, I'll try to explain it.

    Currently the slider does absolutely nothing. I use a [Range(x,x)] tag, on a float variable in a non-Monobehaviour serializable object class, lets call it "Enemy class". Another Monobehaviour class, lets call it "Path Class" is using the Enemy class as a field. The custom editor class is associated with the Path class and makes use of OnScreenGUI callback to create and draw a bunch lines (I am using the Velosity plugin). After the lines have been created, the lines will not be updated again until a transform has been manipulated.

    The strange part is, if I comment out the section that draws the line, manipulating the slider created no hiccups. If the lines are created only once, manipulating the sliders creates huge performance drops in the editor (up to 400-600ms hiccups).

    Pseudo code of my situation: Take note that "distance" is not used for anything, but if the slider is manipulated the hiccups are present.

    Code (CSharp):
    1. [System.Serializable]
    2. public class EnemySettings
    3. {
    4.     [Range(0, 1)]
    5.     public float distance;
    6. }
    7.  
    8. public class PathChunk : MonoBehaviour
    9. {
    10.     EnemySettings[] enemies;
    11.     PathLines meshFilterPaths;
    12. }
    13.  
    14. [CustomEditor(typeof(PathChunk))]
    15. public class PathChunkEditor : Editor
    16. {
    17.     void OnSceneGUI()
    18.     {
    19.         PathChunk path = (PathChunk)target;
    20.  
    21.         //Create Unity Handles to manipulate lines
    22.         Handles.DrawCoolHandles();
    23.  
    24.         //Create meshFilters that represents lines, but only once. Update if hasTransformChanged is true.
    25.         path.meshFilterPaths = GeneratePathOnlyOnce(hasTransformChanged);
    26.     }
    27. }
     
    Last edited: May 26, 2016
  4. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    -- Update --
    I have found out the following:

    I have a prefab that got a PathChunk script attached. In the project, it is empty like this:



    The second I add it to the scene. The "Lines" array is populated only once. This is done only once inside the OnScreenGUI and safeguards are placed so when a transform has been modified the Lines array is updated.


    I expanded one of the Lines just to show you that each Lines contains a good portion of information ranging from vertices to UV coordinates.


    The strange thing is, the second I add another EnemySettings, both EnemySettings and the "Lines" becomes marked as dirty.



    And so does each element in the Lines array:



    From this point, each manipulation inside the "PathChunk" component creates huge spikes, which appears to come from "FlushDirty". If I apply the prefab, the performance spikes are fixed and I can continue working as usual. Anybody knows what is causing this behaviour?
     
  5. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    I haven't really found a fix for this yet. The best solution I have come up with so far, is simply to remove serialization of Lines.
     
  6. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    Problem is reoccurring. Still looking for a solution.
     
  7. VoxelMatt

    VoxelMatt

    Joined:
    Apr 22, 2015
    Posts:
    42
    You helped my find a solution!! Basically, I just apply the prefab in Code after any manipulation that changes the data. Turned a 100sec operation for me into 100ms :)

    Code (CSharp):
    1.  
    2. if (UnityEditor.PrefabUtility.GetPrefabType(this.gameObject) == UnityEditor.PrefabType.PrefabInstance
    3.             || UnityEditor.PrefabUtility.GetPrefabType(this.gameObject) == UnityEditor.PrefabType.DisconnectedPrefabInstance)
    4. {
    5.     Object prefabParent = UnityEditor.PrefabUtility.GetPrefabParent(this.gameObject);
    6.     Debug.AssertFormat(UnityEditor.PrefabUtility.GetPrefabType(prefabParent) == UnityEditor.PrefabType.Prefab, prefabParent, "Expexcted {0} to be a prefab?", prefabParent.name);
    7.     UnityEditor.PrefabUtility.ReplacePrefab(this.gameObject, prefabParent, UnityEditor.ReplacePrefabOptions.ConnectToPrefab);
    8. }
     
    Chrisad likes this.