Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Save time by auto recompiling, exit and enter play mode

Discussion in 'Physics for ECS' 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.