LoginSignup
17
12

More than 5 years have passed since last update.

【Unity】スクリプトのコンパイル時間を大幅に短縮し、C# 7.2 の機能を使用できる「Unity Incremental Compiler」の導入方法を紹介

Posted at

備考

この投稿は自分のブログの記事の転載になります
http://baba-s.hatenablog.com/entry/2018/03/29/111500

はじめに

Unity 2018.1b12 以降で使用できる「Unity Incremental Compiler」が公開されました
これは、次世代コンパイラと呼ばれる「Roslyn」を Unity で使用し、
変更があった部分だけをリコンパイルすることで、
スクリプトのコンパイル時間を大幅に短縮する仕組みです
また、C# 7.2 の機能も使用できるようになります

現在はまだアルファ版の公開となりますが、導入方法を紹介していきます

導入方法

Unity 2018.1.0b12 のインストール

「Unity Incremental Compiler」は Unity 2018.1b12 以降で使用できるので、
上記のページから Unity 2018.1.0b12 をダウンロードしてインストールします

Visual Studio 2017 のバージョン 15.6.4 のインストール

「Unity Incremental Compiler」を使用するためには、
Visual Studio のバージョンが 15.6.4 以上である必要があります

もしも Visual Studio のバージョンが古い場合は、
上記のページからインストーラをダウンロードしてインストールします

manifest.json の編集

Unity 2018.1.0b12 で Unity プロジェクトを作成したら、
Unity プロジェクトの保存場所をエクスプローラで開き、

「Packages」フォルダ内の「manifest.json」をテキストエディタで開き、
下記のコードを貼り付けて保存します

{
    "dependencies": {
        "com.unity.incrementalcompiler": "0.0.27"
    },
    "registry": "https://staging-packages.unity.com"
}

Scripting Runtime Version の変更

Unity で Player Settings を開き、「Scripting Runtime Version」を
「Stable (.NET 4.x Equivalent)」に変更し、Unity プロジェクトを再起動します

「Library/ScriptAssemblies」フォルダの削除

Unity プロジェクトを一旦閉じて、「Library」フォルダ内の
「ScriptAssemblies」フォルダを削除してから、再度 Unity プロジェクトを開きます

導入できているかどうかの確認

Unity メニューの「Window>Package Manager」を選択して
表示されたウィンドウに「IncrementalCompiler」が表示されていれば導入完了です

コンソールウィンドウの表示設定

Unity メニューの「Edit>Preferences...」を選択して表示されたウィンドウで
「Compiler」を選択し、「Show Console Window」をチェックしておくと

次回から Unity プロジェクトを開いた時に
コンパイル時間が確認できるコンソールが起動するようになります

コンパイル時間確認

自分の Unity プロジェクトでコンパイル時間を確認してみたところ
下記のような結果になりました

  Assembly-CSharp.dll Assembly-CSharp-Editor.dll
初回 2.320 秒 0.277 秒
2回目以降 0.376 秒 0.143 秒

初回だけはすべてのスクリプトをコンパイルする必要があるため、
コンパイル時間が長くなりますが、
2回目以降は変更があった部分だけがコンパイルされており、
時間が短縮されていることが確認できました

C# 7.2 の機能の使用

using UnityEngine;

public class Example : MonoBehaviour
{
    readonly struct Point
    {
        public readonly int X;
        public readonly int Y;

        public Point( int x, int y ) => ( X, Y ) = ( x, y );
    }

    void Start()
    {
        var pt = new Point( 1, 2 );
        Debug.Log( pt.X + ", " + pt.Y );
    }
}

C# 7.2 から使用できる、readonly struct や

using UnityEngine;

public class Example : MonoBehaviour
{
    void Start()
    {
        var vec = new Vector3();
        vec.Hoge();
        Debug.Log( vec );
    }
}

public static class Vector3Ext
{
    public static void Hoge( ref this Vector3 self )
    {
        self.x = 1;
        self.y = 2;
        self.z = 3;
    }
}

参照渡しの拡張メソッドも問題なく使用できます

問題点

「Unity Incremental Compiler」はまだアルファ版なので、いくつか問題があります

Visual Studio

Visual Studio において、コードハイライトが一部適用されず、
また、インテリセンスが正常に動作しない状態のようです

関連記事

17
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
12