Giter Club home page Giter Club logo

unity-compile-in-background's Introduction

UnityCompileInBackground

You can start compiling without having to return focus to the Unity editor after changing the script.

News ( 2019/5/1 )

You do not need to use this repository as Visual Studio 2019 supports background compilation.

Version

  • Unity 2018.3.0f2

Usage

For example, if you edit and save the code in Visual Studio,
compilation will start without returning to the Unity editor.

For macOS user, please run "brew install fswatch".

unity-compile-in-background's People

Contributors

baba-s avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

unity-compile-in-background's Issues

Problem with removing extension

When I try to delete the entire folder with the extension, it is restored the next time the code is compiled.
I also tried to delete files when Unity is disabled but the extension is always restored.
I am using Windows 7 / Unity 2018.3

fix : sometimes watcher not killed.

stacked watcher burden cpu 100%.

this code try kill exist watchers when editor start.

thank you.

using System.Diagnostics;
using System.Linq;
using UnityEditor;
using UnityEditor.Compilation;
using UnityEngine;

namespace KoganeUnityLib
{
	/// <summary>
	/// バックグラウンドでコンパイルを開始するためのエディタ拡張
	/// </summary>
	public static class UnityCompileInBackground
	{
		//==============================================================================
		// 定数
		//==============================================================================
		private const string CONSOLE_APP_PATH = @"UnityCompileInBackground/Editor/UnityCompileInBackground-Watcher.exe";

		//==============================================================================
		// 変数
		//==============================================================================
		private static Process	m_process	;	// ファイル監視ツールのプロセス
		private static bool		m_isRefresh	;	// コンパイルを開始する場合 true

		//==============================================================================
		// 関数
		//==============================================================================
		/// <summary>
		/// Unity エディタが起動した時に呼び出されます
		/// </summary>
		[InitializeOnLoadMethod]
		private static void Init()
		{
			// ファイル監視ツールを起動します
			// ツールからメッセージを受信したらコンパイルを開始します
			var dataPath = Application.dataPath;
			var filename = dataPath + "/" + CONSOLE_APP_PATH;
			var path = Application.dataPath;
			var arguments = string.Format(@"-p ""{0}"" -w 0", path);
			var windowStyle = ProcessWindowStyle.Hidden;

			var info = new ProcessStartInfo
			{
				FileName = filename,
				UseShellExecute = false,
				RedirectStandardOutput = true,
				CreateNoWindow = true,
				WindowStyle = windowStyle,
				Arguments = arguments,
			};

			KillExistWatchers();
			StartNewWatcher(info);
		}

		private static void StartNewWatcher(ProcessStartInfo info)
		{
			m_process = Process.Start(info);
			m_process.OutputDataReceived += OnReceived;
			m_process.BeginOutputReadLine();

			UnityEngine.Debug.Log("[UnityCompileInBackground] Start Watching");

			EditorApplication.update += OnUpdate;
			EditorApplication.quitting += OnQuit;
			CompilationPipeline.assemblyCompilationStarted += OnCompilationStarted;
		}

		private static void KillExistWatchers()
		{
			var alreadyExistWatchers = Process.GetProcessesByName("UnityCompileInBackground-Watcher");
			if (alreadyExistWatchers.Any())
			{
				foreach (var watcher in alreadyExistWatchers)
				{
					watcher.Kill();
					watcher.Dispose();
				}
			}
		}

		/// <summary>
		/// Unity エディタが終了する時に呼び出されます
		/// </summary>
		private static void OnQuit()
		{
			Dispose();
		}

		/// <summary>
		/// コンパイルが開始した時に呼び出されます
		/// </summary>
		private static void OnCompilationStarted( string _ )
		{
			Dispose();
		}

		/// <summary>
		/// ファイル監視ツールを止めます
		/// </summary>
		private static void Dispose()
		{
			// すでに止まっている場合は何も行いません
			if ( m_process == null ) return;

			if ( !m_process.HasExited )
			{
				m_process.Kill();
			}
			m_process.Dispose();
			m_process = null;

			UnityEngine.Debug.Log( "[UnityCompileInBackground] Stop Watching" );
		}

		/// <summary>
		/// エディタの更新タイミングで呼び出されます
		/// </summary>
		private static void OnUpdate()
		{
			// コンパイルフラグが立っていない場合、コンパイル中の場合、
			// リフレッシュ中の場合はここで処理を止めます
			if ( !m_isRefresh ) return;
			if ( EditorApplication.isCompiling ) return;
			if ( EditorApplication.isUpdating ) return;

			// コンパイルを開始します
			UnityEngine.Debug.Log( "[UnityCompileInBackground] Start Compiling" );

			m_isRefresh = false;

			AssetDatabase.Refresh();
		}

		/// <summary>
		/// ファイル監視ツールからメッセージを受信した時に呼び出されます
		/// </summary>
		private static void OnReceived( object sender, DataReceivedEventArgs e )
		{
			var message = e.Data;

			// ファイルに変更があった場合もしくは
			// ファイルの名前が変更されたらコンパイルフラグを立てます
			//
			// この関数では AssetDatabase.Refresh を呼び出しても何も起きないので
			// フラグだけ立てておいて Refresh は EditorApplication.update で行います
			if ( message.Contains( "OnChanged" ) || message.Contains( "OnRenamed" ) )
			{
				m_isRefresh = true;
			}
		}
	}
}

use cpu too much!

image

watcher.exe use cpu too much.

do you have an idea drop? (i think infinite loop makes it)

当编译中报错时,无法开始下一轮的监控

当编译时报错,Unity 会中断编译,也就不会触发[InitializeOnLoadMethod] 标记的 Init() 。进而不能继续监控了
When compiling errors occurred, Unity will break the compilation and
as no new assembly loaded, the Init() Function which marked with [InitializeOnLoadMethod] Attribute will not be triggered. you extention Can't continue monitoring the next loop.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.