LoginSignup
1
2

More than 3 years have passed since last update.

Unityのスクリプトテンプレートのカスタマイズとオーバーライド

Last updated at Posted at 2020-02-26

スクリプトテンプレートをカスタマイズしたい!

下記はUnityでスクリプトを生成すると出力されるものです。
暫く使っていると自分好みのテンプレートにカスタマイズしたくなりませんか?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}

カスタマイズ方法は2通り

  1. Unity Editorがインストールされたディレクトリ内のScriptTemplatesを修正する
  2. Assets/ScriptTemplatesを設置する

1は良い記事を見つけたのでこちらの記事をご覧下さい。 
今回は2の説明です。1との違いはインストールディレクトリを触らないので気軽に使えます。プロジェクトによって変更することもできるので汎用性が高いです。

2. Assets/ScriptTemplatesを設置する

スクリーンショット 2020-02-26 19.18.57.png
1. Assets/ScriptTemplatesディレクトリを作成
2. 下記テキストを 81-C# Script-NewBehaviourScript.cs.txt という名前で上記ディレクトリに設置
3. 自分好みに修正 [注意] #SCRIPTNAME#は変更しない
4. エディタを再起動

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class #SCRIPTNAME# : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        #NOTRIM#
    }

    // Update is called once per frame
    void Update()
    {
        #NOTRIM#
    }
}

最後に

ちなみに僕は下記のようにカスタマイズしました。 
IL2CPPで出力するiOS/Androidアプリなどはできる限りsealed classにすると良いですよ。

[追記]
新しいコンパイラの影響でCppコードの内容が変わりsealedをつけても直接メソッドを呼んでくれなくなったぽいです。今後どうなるか分からないのでとりあえず引き続きsealedはつけようと思います。

[il2cpp] Is sealed Not Worked As Said Anymore In Unity 2018.3?


using UnityEngine;

public sealed class #SCRIPTNAME# : MonoBehaviour
{

}

1
2
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
1
2