Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Performance issues related to "FlushDirty" in custom editor script

Discussion in 'Editor & General Support' started by Andrew900460, Jan 12, 2021.

  1. Andrew900460

    Andrew900460

    Joined:
    Aug 9, 2017
    Posts:
    39
    I did find another thread on a similar issue, but they did not come to a solution that helps me.

    I am making a custom editor script for a custom tile builder. So when I click somewhere, it places a tile, which is effectively a Prefab. And there is a grid of prefab instance references that is held and is properly serialized.

    The performance issue only arises when removing a tile (with right click, in my case)
    I have profiled my code, and it says that my code that does the tile removal only takes 0.27 ms, which is great. But then it seems like somewhere in my code, something triggers an internal function called "FlushDirty" that runs for approx. 84 ms. This kinda breaks my ability to remove tiles very quickly by right click and dragging. Because the entire frame is 139 ms.

    I'm wondering if I am using some API function wrong? Am I forgetting to flush some data?
    this is the code itself.
    Code (CSharp):
    1. public void RemoveBlock(Vector2Int blockPosition) {
    2.         UnityEngine.Profiling.Profiler.BeginSample("remove block");
    3.         ref var tile = ref levelDesigner.grid[blockPosition];
    4.         if(tile != null) {
    5.             DestroyImmediate(tile.gameObject);
    6.             tile = null;
    7.         }
    8.         UpdateTiles(blockPosition);
    9.         UnityEngine.Profiling.Profiler.EndSample();
    10. }
    Inside "UpdateTiles()" there is a line that calls "Undo.RecordObject()" a few times, but that was just to fix a serialization issue I had. And I don't know If there is anything else I need to do after that function. But it doesn't seem like the super long "FlushDirty" issue is coming from that.

    My next step to trying to understand this issue is to test out Unity's tilemap system and see how performance differs there. Because somehow they must've overcome this same issue somehow. Also, I am deliberately not using Unity's tilemap system because I need to make something custom for my game, because Unity's tilemaps are not designed to be "super dynamic".

    Lastly, I am using Unity 2020.2.1f1
    Any other general criticisms on the above code are also appreciated.
     
    Last edited: Jan 12, 2021