Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Save time by auto recompiling, exit and enter play mode

Discussion in 'DOTS Physics' started by ComputerKim, Apr 29, 2020.

  1. ComputerKim


    Jan 6, 2019
    I watched this video today about tools and saving time, and he shared a useful code snippet about automatically reloading a scene when Unity recompiles. I immediately tried it out and was disappointed to find out that DOTS physics seems to bug out, but I'm sure this will be fixed before DOTS is released. ;)

    The code snippet gave me an idea. What if it exited play mode and entered it again instead? That worked!

    Here is the code if you want to try it out:
    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3. #if UNITY_EDITOR
    4.     [UnityEditor.Callbacks.DidReloadScripts]
    5.     private static void OnScriptsReloaded() {
    6.         if (UnityEditor.EditorApplication.isPlaying) {
    7.             UnityEditor.EditorApplication.ExitPlaymode();
    8.             UnityEditor.EditorApplication.playModeStateChanged += EditorApplication_playModeStateChanged;
    9.         }
    10.     }
    12.     private static void EditorApplication_playModeStateChanged(UnityEditor.PlayModeStateChange obj) {
    13.         UnityEditor.EditorApplication.playModeStateChanged -= EditorApplication_playModeStateChanged;
    14.         UnityEditor.EditorApplication.EnterPlaymode();
    15.     }
    16. #endif
    17. }
    Note: You need to be in play mode and set Edit > Preferences > General > Script Changes While Playing: Recompile And Continue Playing.

    Hope you find this useful.

    Unity, if you're listening, please make this an option.
    thelebaron likes this.