いんでぃーづ

ゲームいろいろ、いろいろ自由

Unity : VSCode によく使うコードを登録して一発入力

いわゆる スニペット というやつで、定型文を登録しておいて数クリックで書ける機能です。

VSCodeスニペット登録用ファイルを開く

メニューから Code > 基本設定 > ユーザースニペット とクリック

f:id:sugar_affordance:20180821105826j:plain

C#で書くので csharp を選択します。

f:id:sugar_affordance:20180821110023j:plain

スニペット編集用の csharp.json が開きます

f:id:sugar_affordance:20180821110130j:plain

スニペットを登録する

書き方は csharp.json に書いてあるので一読するのがよいでしょう。

今回は私がよく使う DOTWeen での、ボタンクリック時の Scale アニメーションを登録しました。

{
    // Place your snippets for csharp here. Each snippet is defined under a snippet name and has a prefix, body and 
    // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
    // same ids are connected.
    // Example:
    // "Print to console": {
    //     "prefix": "log",
    //     "body": [
    //         "console.log('$1');",
    //         "$2"
    //     ],
    //     "description": "Log output to console"
    // }

    "UI Button Click - DOTwween" : {
        "prefix": "click",
        "body": [
            "transform.DOPunchScale(new Vector3(0.2f, 0.2f, 0.2f), 0.2f).OnComplete(() => {",
            "$0",
            "});"
        ]
    }
}

prefix が呼び出し時にタイプする文字、bodyスニペット本体です。

$ をbody内に入れることで、スニペット入力後のカーソルの位置を決められます。便利

スニペットを呼び出す

呼び出したい場所で prefix に登録した文字列をタイプします。
予測変換が出ている場合は Esc でキャンセルしてから。

f:id:sugar_affordance:20180821110913j:plain

出力したいスニペットが選択されていることを確認し、リターンキーで呼び出します。

f:id:sugar_affordance:20180821110956j:plain


“Unity” and Unity logos are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere, and are used under license.


免責事項

当サイトの広告バナー、リンクによって提供される情報、サービス内容について、当サイトは一切の責任を負いません。

また、当サイトの情報を元にユーザ様が不利益を被った場合にも、当サイトは一切の責任を負いません。

すべて自己責任でお願いします。