Search Unity

Save time by auto recompiling, exit and enter play mode

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

  1. ComputerKim

    ComputerKim

    Joined:
    Jan 6, 2019
    Posts:
    15
    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.     }
    11.  
    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.