在用Unity开发项目时,每次编写或者修改C#脚本,Unity都会自动检查脚本,并进行自动编译。频繁的自动编译,无疑是会降低开发效率,基于这个出发点,我希望能够关闭自动编译功能。在Unity下有两个设置是和自动编译相关的,就是Preferences界面下的General里面的Auto Refresh和Script Changes While Playing。前者是自动刷新,每次刷新的时候就会检查脚本是否需要编译,这时候我们可以把自动刷新关掉,然后使用手动的方式进行刷新。后者主要是控制运行时脚本自动编译的时机,是否在运行时进行自动编译。
上图中的设置项的位置可能会因Unity的版本不同而有所改变,这里我用的Unity版本是Unity 2020.3.33f1c2 Personal 这里写了一个编辑器脚本,将上面的设置暴露在菜单栏中。
//kAutoRefresh has two posible values //0 = Auto Refresh Disabled //1 = Auto Refresh Enabled
//This is called when you click on the 'Tools/Auto Refresh' and toggles its value [MenuItem("Tools/Auto Refresh")] staticvoidAutoRefreshToggle() { var status = EditorPrefs.GetInt("kAutoRefresh"); if (status == 1) EditorPrefs.SetInt("kAutoRefresh", 0); else EditorPrefs.SetInt("kAutoRefresh", 1); }
//This is called before 'Tools/Auto Refresh' is shown to check the current value //of kAutoRefresh and update the checkmark [MenuItem("Tools/Auto Refresh", true)] staticboolAutoRefreshToggleValidation() { var status = EditorPrefs.GetInt("kAutoRefresh"); if (status == 1) Menu.SetChecked("Tools/Auto Refresh", true); else Menu.SetChecked("Tools/Auto Refresh", false); returntrue; }
//Script Compilation During Play
//ScriptCompilationDuringPlay has three posible values //0 = Recompile And Continue Playing //1 = Recompile After Finished Playing //2 = Stop Playing And Recompile
//The following methods assing the three possible values to ScriptCompilationDuringPlay //depending on the option you selected [MenuItem("Tools/Script Compilation During Play/Recompile And Continue Playing")] staticvoidScriptCompilationToggleOption0() { EditorPrefs.SetInt("ScriptCompilationDuringPlay", 0); }
[MenuItem("Tools/Script Compilation During Play/Recompile After Finished Playing")] staticvoidScriptCompilationToggleOption1() { EditorPrefs.SetInt("ScriptCompilationDuringPlay", 1); }
[MenuItem("Tools/Script Compilation During Play/Stop Playing And Recompile")] staticvoidScriptCompilationToggleOption2() { EditorPrefs.SetInt("ScriptCompilationDuringPlay", 2); }
//This is called before 'Tools/Script Compilation During Play/Recompile And Continue Playing' //is shown to check for the current value of ScriptCompilationDuringPlay and update the checkmark [MenuItem("Tools/Script Compilation During Play/Recompile And Continue Playing", true)] staticboolScriptCompilationValidation() { //Here, we uncheck all options before we show them Menu.SetChecked("Tools/Script Compilation During Play/Recompile And Continue Playing", false); Menu.SetChecked("Tools/Script Compilation During Play/Recompile After Finished Playing", false); Menu.SetChecked("Tools/Script Compilation During Play/Stop Playing And Recompile", false);
var status = EditorPrefs.GetInt("ScriptCompilationDuringPlay");
//Here, we put the checkmark on the current value of ScriptCompilationDuringPlay switch (status) { case0: Menu.SetChecked("Tools/Script Compilation During Play/Recompile And Continue Playing", true); break; case1: Menu.SetChecked("Tools/Script Compilation During Play/Recompile After Finished Playing", true); break; case2: Menu.SetChecked("Tools/Script Compilation During Play/Stop Playing And Recompile", true); break; } returntrue; } }